英文:
How can I go to homepage from another page in Django? I am unable to exit from last called definition
问题
def save(request, id):
if request.method == 'POST':
if request.POST.get('name'):
table = Userdb.objects.get(id=id)
table.name = request.POST.get('name')
table.url = request.POST.get('url')
table.phone = request.POST.get('phone')
table.dob = request.POST.get('dob')
table.save()
#messages.success(request, "record saved successfully")
#return redirect('userlist')
#return HttpResponseRedirect({%urls 'index'% })
return render(request, "index.html")
这里是最后一部分,我已经重定向到index.html。 但实际上我没有退出保存定义。 请参考下一个快照。
网页中的URL不显示index.html,而显示save。 还有index网页的CSS丢失。
我尝试过各种URL,但都没用。 也尝试更改setings.py,仍然无济于事。
我想导航到主页,不留下任何旧定义的痕迹
英文:
def save(request,id):
if request.method=='POST':
if request.POST.get('name'):
table=Userdb.objects.get(id=id)
table.name=request.POST.get('name')
table.url=request.POST.get('url')
table.phone=request.POST.get('phone')
table.dob=request.POST.get('dob')
table.save()
#messages.success(request,"record saved successfully")
#return redirect('userlist')
#return HttpResponseRedirect({%urls 'index'% })
return render(request,"index.html")
Here at last part,I have directed to index.html. But actually I am not exited the save definition. please refer next snapshot.
The url in webpage dont show index.html but shows save. Also css of index webpage is lost.
I tried various urls but didnt work. Also tried to change setings.py still no avail.
I want to navigate to home page without trace of any older definition
答案1
得分: 0
使用重定向以转到主页。
对于URL,我建议使用reverse_lazy。
导入以下内容:
from django.shortcuts import redirect
from django.urls import reverse_lazy
重定向代码:
return redirect(reverse_lazy("home"))
请注意,为了使reverse_lazy正常工作,在urls.py中URL的名称应为"home"。
英文:
You should use redirect to go to home page.
For URL I suggest to use reverse_lazy
import following:
from django.shortcuts import redirect
from django.urls import reverse_lazy
redirection code:
return redirect(reverse_lazy("home"))
Please note, for reverse_lazy to work, the name of url should be "home" in urls.py
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论