12

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

thedarklord47
  • 3,183
  • 3
  • 26
  • 55

3 Answers3

12

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

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
  • and the html file lives in `mysite/mysite/templates/`? – thedarklord47 Jun 04 '15 at 17:28
  • 1
    It lives in wherever you have specified in the `DIRS` value in the config, which is a list of directory paths. – Brandon Taylor Jun 04 '15 at 17:30
  • ah didn't see that last bit with the doc link. thank you sir! – thedarklord47 Jun 04 '15 at 17:43
  • I am not sure if this is a really _static_ file. Usually, static files are files that are server directly from the application server, for instance Apache, and not from the framework. Though I'm pretty sure the solution here was what OP wants, but not that it actually makes the file in question static. – user4052054 Mar 31 '17 at 15:53
  • @user4052054 I understand what you're saying, but it's also completely possible that the template file in the OP's question doesn't contain any Django template tags. At least that's the assumption I made when I answered this question. – Brandon Taylor Mar 31 '17 at 15:56
5

On PythonAnywhere, you can use the static files facility of your web app to serve the static file before it gets to Django:

Put a file called index.html in a directory and then point a static file entry to that directory. If the static file URL is / and the directory is the one with the html file in it, the file will be served at /.

Be aware that you don't want the directory to be above any of your code or you'll expose your code as static files i.e you don't want to use the directory /somewhere/blah if your code is in /somewhere/blah/code, you'll want to put it in /somewhere/no_code_here

Glenn
  • 7,262
  • 1
  • 17
  • 23
0

I have been tinkering with Django version 3.2, and I was able to serve a static HTML page at the root of the project after creating a Django app as in their official tutorial like the following in the root urls.py file:

from django.views.generic import TemplateView

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
    path('', TemplateView.as_view(template_name='home/base.html')),
]

This is given that home/base.html is placed where the default template folder is. If I simply re-use the polls templates directory I can create a folder like polls/templates/home/ to store base.html, for example.

Kris Stern
  • 1,192
  • 1
  • 15
  • 23