问题
I am really new to Django. Problem is that i can't load my template which consists of two basic html files. Here is the Location for my template file:
/home/usman/Django Project/django-black/luckdrum/templates/
Here is my View function:
from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
def hello_template(request):
t=get_template('signup.html')
return HttpResponse(t)
Here is url.py file:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/','blog.views.hello'),
url(r'^signup.html/','blog.views.hello_template'),
]
I have also added the path in my settings.py as TEMPLATE_DIRS. The server shows an error as Template Doesnot Exist. Please help me out!
回答1:
Put your templates in templates/<app_name>/ or <app_name>/templates/<app_name>/. They will be automatically found by Django.
Django will look in the main templates folder templates/<app_name>/ and after <app_name>/templates/<app_name>/.
And then in your view :
from django.shortcuts import render
def hello_template(request):
return render(request, '<app_name>/signup.html')
Here is what your Django project should look like (as it's recommended by Django to write reusable apps) :
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
__init__.py
admin.py
migrations/
__init__.py
0001_initial.py
models.py
static/
polls/
images/
background.gif
style.css
templates/
polls/
detail.html
index.html
results.html
tests.py
urls.py
views.py
templates/
admin/
base_site.html
Read django documentation : Organise templates and How to write reusable apps
来源:https://stackoverflow.com/questions/34131385/basic-template-not-loading-in-django