如何从Django的另一页返回主页?我无法退出上次调用的定义。

huangapple go评论72阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月3日 19:19:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定