basic template not loading in Django

让人想犯罪 __ 提交于 2020-01-03 04:24:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!