在 views.py 中提交的表单在使用 Django 分页器时无法正常工作后重定向到同一页面
Redirect to same page after form submitted in views.py not working while using Django paginator
我想在提交从 views.py 获取视图的表单后重定向到同一页面。问题是分页器。提交 Django 表单页面后重新加载到第 1 页。我希望浏览器在提交表单后保持在同一页面上。我得到的错误:django.urls.exceptions.NoReverseMatch:'http' 不是已注册的命名空间。将不胜感激!
js中的代码调用路径:
let path = window.location.href
正在获取 api:
subedit[i].addEventListener('click', () => {
fetch(`/edit/${content[i].id}`,
{
method: 'POST',
headers: {'X-CSRFToken': csrftoken},
mode: 'same-origin',
body: JSON.stringify({
post: textarea[i].value,
page: path
})}).then(() => {
editdiv[i].style.display = 'none';
post[i].style.display = 'block';
})})
views.py:
def edit(request, post_id):
data = json.loads(request.body)
content = data.get("post", "")
post=Post.objects.get(id=post_id)
page = data.get("page", "")
if request.method == "POST":
if post.user == request.user:
post.post=content
post.save()
return HttpResponseRedirect(reverse(str(page)))
reverse(…)
[Django-doc] looks for a view with the given name, it does not take a URL as input. You can use this directly in the HttpResponseRedirect
[Django-doc], 所以:
def edit(request, post_id):
data = json.loads(request.body)
content = data.get("post", "")
post=Post.objects.get(id=post_id)
page = data.get("page", "")
if request.method == 'POST':
if post.user == request.user:
post.post=content
post.save()
return HttpResponseRedirect(<strong>page</strong>)
对于 GET 请求,您还应该 return HttpResponse
s,以防 post.user
与 request.user
不同。
Note: It is often better to use get_object_or_404(…)
[Django-doc],
then to use .get(…)
[Django-doc] directly. In case the object does not exists,
for example because the user altered the URL themselves, the get_object_or_404(…)
will result in returning a HTTP 404 Not Found response, whereas using
.get(…)
will result in a HTTP 500 Server Error.
Note: You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc].
我能够使用会话让它工作:
<a id="page" class="page-link">{{ num }}</a>
let page=document.querySelector("#page").innerHTML
fetch(`/edit/${content[i].id}`,
{
method: 'POST',
headers: {'X-CSRFToken': csrftoken},
mode: 'same-origin',
body: JSON.stringify({
post: textarea[i].value,
page: page
})}).then(() => {
editdiv[i].style.display = 'none';
post[i].style.display = 'block';
})})
def edit(request, post_id):
data = json.loads(request.body)
content = data.get("post", "")
post=Post.objects.get(id=post_id)
page = data.get("page", "")
request.session['page'] = page
if request.method == "POST":
if post.user == request.user:
post.post=content
post.save()
return HttpResponse(page)
def index(request):
posts = Post.objects.all().order_by('-date')
paginator = Paginator(posts,10)
page_number = request.GET.get('page')
if request.session.has_key('page'):
page_number = request.session['page']
del request.session['page']
我想在提交从 views.py 获取视图的表单后重定向到同一页面。问题是分页器。提交 Django 表单页面后重新加载到第 1 页。我希望浏览器在提交表单后保持在同一页面上。我得到的错误:django.urls.exceptions.NoReverseMatch:'http' 不是已注册的命名空间。将不胜感激!
js中的代码调用路径:
let path = window.location.href
正在获取 api:
subedit[i].addEventListener('click', () => {
fetch(`/edit/${content[i].id}`,
{
method: 'POST',
headers: {'X-CSRFToken': csrftoken},
mode: 'same-origin',
body: JSON.stringify({
post: textarea[i].value,
page: path
})}).then(() => {
editdiv[i].style.display = 'none';
post[i].style.display = 'block';
})})
views.py:
def edit(request, post_id):
data = json.loads(request.body)
content = data.get("post", "")
post=Post.objects.get(id=post_id)
page = data.get("page", "")
if request.method == "POST":
if post.user == request.user:
post.post=content
post.save()
return HttpResponseRedirect(reverse(str(page)))
reverse(…)
[Django-doc] looks for a view with the given name, it does not take a URL as input. You can use this directly in the HttpResponseRedirect
[Django-doc], 所以:
def edit(request, post_id):
data = json.loads(request.body)
content = data.get("post", "")
post=Post.objects.get(id=post_id)
page = data.get("page", "")
if request.method == 'POST':
if post.user == request.user:
post.post=content
post.save()
return HttpResponseRedirect(<strong>page</strong>)
对于 GET 请求,您还应该 return HttpResponse
s,以防 post.user
与 request.user
不同。
Note: It is often better to use
get_object_or_404(…)
[Django-doc], then to use.get(…)
[Django-doc] directly. In case the object does not exists, for example because the user altered the URL themselves, theget_object_or_404(…)
will result in returning a HTTP 404 Not Found response, whereas using.get(…)
will result in a HTTP 500 Server Error.
Note: You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc].
我能够使用会话让它工作:
<a id="page" class="page-link">{{ num }}</a>
let page=document.querySelector("#page").innerHTML
fetch(`/edit/${content[i].id}`,
{
method: 'POST',
headers: {'X-CSRFToken': csrftoken},
mode: 'same-origin',
body: JSON.stringify({
post: textarea[i].value,
page: page
})}).then(() => {
editdiv[i].style.display = 'none';
post[i].style.display = 'block';
})})
def edit(request, post_id):
data = json.loads(request.body)
content = data.get("post", "")
post=Post.objects.get(id=post_id)
page = data.get("page", "")
request.session['page'] = page
if request.method == "POST":
if post.user == request.user:
post.post=content
post.save()
return HttpResponse(page)
def index(request):
posts = Post.objects.all().order_by('-date')
paginator = Paginator(posts,10)
page_number = request.GET.get('page')
if request.session.has_key('page'):
page_number = request.session['page']
del request.session['page']