django redirect url concatenating to the previous url, help me to stop concatenating

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

django redirect url concatenating to the previous url, help me to stop concatenating

问题

这是我的先前的URL - http://127.0.0.1:8000/viewbook/8/viewchapter/57/

这是我想要重定向的URL - http://127.0.0.1:8000/viewbook/8/viewchapter/57/

但它将我重定向到这个URL - http://127.0.0.1:8000/viewbook/8/viewchapter/57/

项目 - urls.py

from django.contrib import admin
from django.urls import path , include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
]


home.urls.py

from django.urls import path
from . import views

app_name = 'home'
urlpatterns = [
path('',views.home,name='home'),
path('viewbook/<int:id>/', views.viewbook , name='viewbook'),
path('viewchapter/<int:id>/', views.viewchapter , name='viewchapter'),
path('viewpost/<int:id>/', views.viewpost , name='viewpost'),
]


views.py

def viewbook(response , id):
book = Book.objects.get(id = id)
allchapters = Chapter.objects.all()
chapters = []
for chapt in allchapters:
if chapt.Book.id == book.id:
chapters.append(chapt)
inputform = ChapterForm()
if response.method == 'POST':
form = ChapterForm(response.POST)
if form.is_valid():
chapt = Chapter(Book = book , chapterNum = response.POST['chapterNum'] , chapterText = "")
print(chapt)
chapt.save()
print(Chapter.objects.get(id= chapt.id))
return redirect('viewchapter/%i/' %chapt.id)
inputform.fields["chapterNum"].initial =len(chapters) + 1
context = {
'book' : book,
'form' : inputform,
'chapters' : chapters

}
return render(response , &#39;viewbook.html&#39; , context)

def viewchapter(response , id):
chapter = Chapter.objects.get(id = id)
context = {'chapter' : chapter}
return render(response , 'viewchapter.html', context)


我认为问题出在这一行

return redirect(&#39;viewchapter/%i/&#39; %chapt.id)

我将其更改为
return HttpResponseRedirect(&#39;viewchapter/%i/&#39; %chapt.id)

但它们都给我相同的结果,如下所示

当前路径viewbook/9/viewchapter/58/与任何路径都不匹配。
英文:

this is my previouse url - http://127.0.0.1:8000/viewbook/8/viewchapter/57/

this is the url I want to redirect - http://127.0.0.1:8000/viewbook/8/viewchapter/57/

but its redirect me to this url - http://127.0.0.1:8000/viewbook/8/viewchapter/57/

project - urls.py

from django.contrib import admin
from django.urls import path , include


urlpatterns = [
    path(&#39;admin/&#39;, admin.site.urls),
    path(&#39;&#39;, include(&#39;home.urls&#39;)),
]

home.urls.py

from django.urls import path 
from . import views

app_name = &#39;home&#39;
urlpatterns = [
    path(&#39;&#39;,views.home,name=&#39;home&#39;),
    path(&#39;viewbook/&lt;int:id&gt;/&#39;, views.viewbook , name=&#39;viewbook&#39;),
    path(&#39;viewchapter/&lt;int:id&gt;/&#39;, views.viewchapter , name=&#39;viewchapter&#39;),
    path(&#39;viewpost/&lt;int:id&gt;/&#39;, views.viewpost , name=&#39;viewpost&#39;),
]

views.py

def viewbook(response , id):
    book = Book.objects.get(id = id)
    allchapters = Chapter.objects.all()
    chapters = []
    for chapt in allchapters:
        if chapt.Book.id == book.id:
            chapters.append(chapt)
    inputform = ChapterForm()
    if response.method == &#39;POST&#39;:
        form = ChapterForm(response.POST)
        if form.is_valid():
            chapt = Chapter(Book = book , chapterNum = response.POST[&#39;chapterNum&#39;] , chapterText = &quot;&quot;)
            print(chapt)
            chapt.save()
            print(Chapter.objects.get(id= chapt.id))
            return redirect(&#39;viewchapter/%i/&#39; %chapt.id)
    inputform.fields[&quot;chapterNum&quot;].initial =len(chapters) + 1
    context = {
        &#39;book&#39; : book,
        &#39;form&#39; : inputform,
        &#39;chapters&#39; : chapters
        
    }
    return render(response , &#39;viewbook.html&#39; , context)


def viewchapter(response , id):
    chapter = Chapter.objects.get(id = id)
    context = {&#39;chapter&#39; : chapter}
    return render(response , &#39;viewchapter.html&#39;, context)

I think the problem is with this line

return redirect('viewchapter/%i/' %chapt.id)

I change it to
return HttpResponseRedirect('viewchapter/%i/' %chapt.id)

but bith of them are giving me same result like this

The current path, viewbook/9/viewchapter/58/, didn’t match any of these.

答案1

得分: 0

  • 相对URL,可以通过在redirect调用中添加前导/来修复
  • redirect函数的使用方式不符合Django的规范

重定向可以接受硬编码的URL,但在您的情况下,应传递视图名称和参数,如Django文档所述,重定向可以接受视图或URL模式名称和参数:

return redirect('some-view-name', foo='bar')

在您的情况下,应该是:

return redirect('viewchapter', chapt.id)

return redirect('viewchapter', id=chapt.id)

英文:

Two problems detected:

  • relative URL which can be fixed with leading / in redirect call
  • redirect function is used not in django way

Redirect can accept hardcoded URLs however in your case view name with arguments should be passed. As Django docs say redirect can accept view or URL pattern name and arguments:

return redirect(&#39;some-view-name&#39;, foo=&#39;bar&#39;)

URL will be built by reversing URL pattern you defined in urls.py

In your case it will be

return redirect(&#39;viewchapter&#39;, chapt.id)

or

return redirect(&#39;viewchapter&#39;, id=chapt.id)

huangapple
  • 本文由 发表于 2023年4月4日 14:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926026.html
匿名

发表评论

匿名网友

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

确定