简体   繁体   中英

How to add path for the file today.html in views.py and settings.py in django

I have a html file in my home directory named today.html

I have to load that file on a click of a link using django.

How to add the file path in views.py and settings.py files.

You could add your home directory path to the TEMPLATE_DIRS setting in your project's settings.py file. Then when you try to render the template in your view, Django will be able to find it.

First of all you always have to keep your html files in template dir under django's project/root dir.

Than you have to configure template path in settings.py such as

from unipath import Path
PROJECT_DIR = Path(__file__).ancestor(3) #ancestor value depends on number parent dir.

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
PROJECT_DIR.child('templates')
)

now you can access that html file by interating into urls.py such as

from django.views.generic import TemplateView

 urlpatterns = patterns('',
        url(r'link_name_to_access^$', TemplateView.as_view(template_name="today.html")),
  )

Note:

Using TemplateView you don't need to write specific view to render the required html.

First copy the file today.html into your projects template folder.

Add the path in settings.py file. ie your project-path/your file-path(today.html).

Then create a function to open today.html file in views.py.

Now in urls.py file give the url.

ie. url(r'^today/$', 'your project-path.views.today', name='today'),

that's it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM