Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 376
thedarklord47 Avatar asked Jun 04 '15 17:06

thedarklord47


People also ask

How do I serve static files in Django?

Serving the site and your static files from the same serverPush your code up to the deployment server. On the server, run collectstatic to copy all the static files into STATIC_ROOT . Configure your web server to serve the files in STATIC_ROOT under the URL STATIC_URL .

What is static root in Django?

The STATIC_ROOT variable in settings.py defines the single folder you want to collect all your static files into. Typically, this would be a top-level folder inside your project, eg: STATIC_ROOT = "/home/myusername/myproject/static" # or, eg, STATIC_ROOT = os. path.

Can Django serve static files in production?

Static Files in Development Mode During development, as long as you have DEBUG set to TRUE and you're using the staticfiles app, you can serve up static files using Django's development server. You don't even need to run the collecstatic command.

Where should I put static files in Django?

Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.


2 Answers

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

like image 187
Brandon Avatar answered Sep 18 '22 11:09

Brandon


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

like image 29
Glenn Avatar answered Sep 18 '22 11:09

Glenn