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

thedarklord47 picture thedarklord47 · Jun 4, 2015 · Viewed 9.5k times · Source

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

I can't seem to find any info on how to do this, but I assume I have to modify mysite/mysite/urls.py as navigating to root currently gives me a 404 with messages about failing to find a match in urls.

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

The previous is my best guess (a bad one i know). That should (correct me if i'm wrong) match the root URL, but I have no idea how to say "hey django just look for a static file", or where that static html should live, or how to tell django where it lives.

Try not to cut my head off. I'm brand new to django.

P.S. I'm using django 1.8 in a virtualenv on PA

Answer

Brandon picture Brandon · Jun 4, 2015

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

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

I usually order my URL patterns from most specific to 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 where Django looks for templates, it's up to your configuration to tell Django where to look: https://docs.djangoproject.com/en/1.8/topics/templates/#configuration