表单.html
<form action='/login/' method = 'post'>
{% csrf_token %}
<label>Email: (*)</label><input type='text' name='email' value='' /><br />
<label>Password: </label><input type='password' name='password' value='' /><br />
<input type='submit' name='submit' value='Log in' />
</form>
和 views.py 我使用 HttpResponse 而不是 render_to_response
def login(request):
success = False
message = ''
try:
emp = Employee.objects.get(email = request.POST['email'])
if emp.password == md5.new(request.POST['password']).hexdigest() :
emp.token = md5.new(request.POST['email'] + str(datetime.now().microsecond)).hexdigest()
emp.save()
info = serializers.serialize('json', Employee.objects.filter(email = request.POST['email']))
success = True
return HttpResponse(json.dumps({'success':str(success).lower(), 'info':info}))
else:
message = 'Password wrong!'
return HttpResponse(json.dumps({'success':str(success).lower(), 'message':message}), status = 401)
except:
message = 'Email not found!'
return HttpResponse(json.dumps({'success':str(success).lower(), 'message':message}), status = 401)
如果使用 render_to_response,我只添加 RequestContext 但 HttpResponse,我不知道该怎么做。
我使用 Django 1.4
我的问题在哪里
==========================
当我更改呈现 HTML 的函数时,我的问题得到了解决:
def homepage(request):
return render_to_response('index.html')
到
def homepage(request):
return render_to_response('index.html', context_instance=RequestContext(request))
这是一个愚蠢的错误...谢谢...