英文:
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 , 'viewbook.html' , context)
def viewchapter(response , id):
chapter = Chapter.objects.get(id = id)
context = {'chapter' : chapter}
return render(response , 'viewchapter.html', context)
我认为问题出在这一行
return redirect('viewchapter/%i/' %chapt.id)
我将其更改为
return HttpResponseRedirect('viewchapter/%i/' %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('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 , 'viewbook.html' , context)
def viewchapter(response , id):
chapter = Chapter.objects.get(id = id)
context = {'chapter' : chapter}
return render(response , 'viewchapter.html', 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
/
inredirect
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('some-view-name', foo='bar')
URL will be built by reversing URL pattern you defined in urls.py
In your case it will be
return redirect('viewchapter', chapt.id)
or
return redirect('viewchapter', id=chapt.id)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论