Django中没有加载基本模板

Django中没有加载基本模板

本文介绍了Django中没有加载基本模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢Django。问题是我无法加载由两个基本的html文件组成的模板。
这是我的模板文件的位置:

这是我的查看功能:

 <$从django.shortcuts导入render 
从django.http导入hbpResponse
从django.template.loader导入get_template
从django.template导入上下文
def hello_template(请求)
t = get_template('signup.html')

返回HttpResponse(t)

这是url.py文件:

 从django.conf.urls import include,url 
从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'),

]

我还将我的settings.py中的路径添加为TEMPLATE_DI RS。
服务器显示一个错误,因为Template Doesnot Exist。请帮助我!

解决方案

将您的模板放在 templates /< app_name> / < app_name> / templates /< app_name> / 。他们将被Django自动找到。



Django将查看主模板文件夹 templates /< app_name> / < app_name> / templates /< app_name> / 之后。



然后在您的视图中:

  from django.shortcuts import render 

def hello_template(request) :
return render(request,'< app_name> /signup.html')

这是Django项目应该是什么样子(由于Django推荐使用Django编写可重复使用的应用程序):

  mysite / 
manage.py
mysite /
__init__.py
settings.py
urls.py
wsgi .py
polls /
__init__.py
admin.py
migrations /
__init__.py
0001_initial.py
模型s.py
static /
polls /
images /
background.gif
style.css
templates /
polls /
detail.html
index.html
results.html
tests.py
urls.py
views.py
templates /
管理员/
base_site.html

阅读django文档: a href =https://docs.djangoproject.com/en/1.8/intro/tutorial03/#write-views-that-actually-do-something =nofollow>组织模板和如何编写可重复使用的应用程序


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:

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!

解决方案

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

这篇关于Django中没有加载基本模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 15:50