简体   繁体   中英

Python templates

This is my views.py:

from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
def login(request):
    t = get_template('login.html')
    return HttpResponse(t.render) 

In my urls.py I put

(r'^login/', include('project.views.login')),

to show my templates

I set my templates directory in settings.py

But i got an error named

ImportError at /login/
No module named login

What's wrong ?

include is used to include other url configs from other apps. It shouldn't be used if you're trying to add a url pattern for one particular view. You should have something like

(r'^login/', 'project.views.login'),

The other problem is where you return your response. render is a method that takes a context (see the docs )

def login(request):
    t = get_template('login.html')
    c = Context({})
    return HttpResponse(t.render(c)) 

In practice, you wouldn't usually load the template, render it, then return a response. There are two shortcut functions, render and render_to_response , that cut down on repetition.

You've made a few basic mistakes here. I recommend you work through the Django tutorials (again, if you've already looked at them). Tutorial 3 in particular explains all this stuff.

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