Is it possible to serve a static html page in the root of a django project?

I have a django application hosted on pyhtonanywhere. The app is located at username.pythonanywhere.com/MyApp . I would like to put a static html page in username.pythonanywhere.com . Is it possible? Essentially this will serve as an index linking to /MyApp , /MyApp2 and other future applications.

I can not find any information on how to do this, but I assume that I need to change mysite/mysite/urls.py , since going to the root currently gives me 404 with messages about what could not be found match in urls.

 urlpatterns = [ url(r'^/$', ???), url(r'^admin/', include(admin.site.urls)), url(r'^MyApp/', indluce('MyApp.urls')). ] 

The previous one is my best guess (the bad one I know). This should (fix me if I'm wrong) matches the root URL, but I have no idea how to say β€œhey django, just look for the static file” or where this static html should live, or how to tell django where it is lives.

Try not to chop off my head. I am new to django.

PS I am using django 1.8 in virtualenv on PA

+7
python html django static pythonanywhere
source share
2 answers

Of course you can. In cases where I just need to display a template, I use TemplateView . Example:

 url(r'^$', TemplateView.as_view(template_name='your_template.html')) 

I usually order URL patterns from the most specific to the least specific to avoid unexpected matches:

 from django.views.generic import TemplateView urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^MyApp/', include('MyApp.urls')), url(r'^$', TemplateView.as_view(template_name='your_template.html')), ] 

As far as Django is looking for templates, you need to configure Django to see: https://docs.djangoproject.com/en/1.8/topics/templates/#configuration

+9
source share

In PythonAnywhere, you can use the static file tool of your web application to serve a static file before it gets into Django:

Place the file named index.html in the directory, and then point the static file to this directory. If the static URL of the file is /, and the directory is the one where the html file is located, the file will be sent to /.

Keep in mind that you do not want the directory to be above any of your code, or you will expose your code to static files, i.e. you don't want to use the / something / blah directory, if your code is in / somewhere / blah / code, you want to put it in / somewhere / no_code_here

+3
source share

All Articles