render_to_response 或重定向更改 Django 1.8 中的模板元素 [英] render_to_response or redirect changes the template elements in Django 1.8
问题描述
我正在尝试检查用户输入的电子邮件 ID 是否存在于数据库表中,如果存在 - 我想路由到prof.html"模板,否则只在 login.html 模板中显示一条消息.
I'm trying to check if email id entered by user is existing in the database table, if existing - I would like to route to 'prof.html' template otherwise just show a message in the login.html template.
两个条件都工作正常.
然而,问题是当我使用 redirect() 或 render_to_response() 时 -目标模板元素,如 div、input 等,正在自动更改(在本例中为 prof.html)?
However, the problem is when I use redirect() or render_to_response() - the destination template elements like div, input etc., are being changed automatically (prof.html in this case) ?
我们是否也可以将上下文信息发送到目标模板?(在这种情况下,响应数据或来自数据库的任何对象并通过视图重定向到 prof.html 模板)
Can we also send the context information to destination template ? (response data or any object from the database and redirect to prof.html template via view in this case)
下面是我的代码:
Views.py
def verifyme(request):
if request.method == "POST":
emailid4loginV = request.POST['emailid4login_Aj']
else:
emailid4loginV = ''
response_data = ''
return HttpResponse(response_data, content_type="text/plain")
response_data = ''
if Employee.objects.filter(email = emailid4loginV).exists():
response_data='Thanks for waiting - login successful'
#return render_to_response('app/prof.html', { 'response_data':response_data},
# context_instance = RequestContext( request ) )
return redirect('/myprofile')
else:
response_data='Ouch! you are not a registered user!'
return HttpResponse(response_data, content_type="text/plain")
urls.py
url(r'^myprofile$', 'app.views.profile', name='profile'),
仅供参考,个人资料"视图确实会从表中返回一些对象并在模板 app/prof.html 中呈现.
Just for your info, 'profile' view does return some objects from the table and renders in the template app/prof.html.
我观察到目标模板在同一个 login.html 模板中呈现(如何?:在浏览器 url 中,我没有看到 myprofile - 但用于登录的那个)但是当我通过在网站中输入手动请求 myprofileurl (localhost:xxxxx/myprofile),它完美运行:(
I observed that the destination template is being rendered in same login.html template (How ? : In the browser url, I dont see myprofile - but the one for login) But when I request the myprofile manually by entering in the website url (localhost:xxxxx/myprofile), it works perfectly :(
在 login.html 中提交请求之前的 URL :
URL before submitting request in login.html :
在 login.html 中提交请求后的 URL - myprofile 在同一页面中呈现:
URL after submitting request in login.html - myprofile is rendered in the same page :
当我手动输入 url 时,模板完美运行..
When I manually type in the url, template just works perfectly..
你能告诉我可能是什么问题吗?
Could you please let me know what could be the problem ?
用一个小技巧解决了这个问题,贴在下面
Solved this issue with a little trick, posted in the below
推荐答案
1) 实际上有很多方法可以将数据传递到下一个视图...sessiontorage),它就像剪贴板……在一个视图中保存会话数据,稍后在另一个视图中获取.例如:
1) Actually there are many ways to pass data to next view ... generally in such cases like you have better way - using sessions (cookie|localstorage|sessionstorage), it is like clipboard ... save session data in one view and get it later in another one. For example:
第一次观看:
self.request.session['response_data'] = 'some text'
self.request.session.set_expiry(0) # user’s session cookie will expire when the user’s Web browser is closed.
其他观点:
response_data = self.request.session.get('response_data', '')
但是如果您打算只在模板中使用这些数据,Django 有一些更高级的接口,在您的情况下语义上正确使用它 - 消息框架 https://docs.djangoproject.com/en/1.8/ref/contrib/messages/
But if you planning just use this data in template Django has some kind more high-level interface for it and in your case semantically right to use it - The messages framework https://docs.djangoproject.com/en/1.8/ref/contrib/messages/
2) 如果你想重定向到另一个视图,最好使用 url 命名空间和 reverse
https://docs.djangoproject.com/en/1.8/ref/urlresolvers/#reverse
2) If you want redirect to another view better use url namespaces and reverse
https://docs.djangoproject.com/en/1.8/ref/urlresolvers/#reverse
return HttpResponseRedirect(reverse(app.views.profile)) # here I've passed callable object because you have not show your app url namespace, but generally use namespaces
https://docs.djangoproject.com/en/1.8/topics/http/urls/#url-namespaces
这篇关于render_to_response 或重定向更改 Django 1.8 中的模板元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!