Python 在 Django 中提交表单后重定向到索引页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12671649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect to index page after submiting form in django
提问by shabeer90
Everything works correctly apart from redirecting back to the index page after adding the products data, currently after my data gets save it gets redirected to 127.0.0.1:8000/product/add_product/add_product
除了在添加产品数据后重定向回索引页面之外,一切正常,目前在我的数据保存后它被重定向到127.0.0.1:8000/product/add_product/add_product
Currently when my index page(add_product.html) loads, I have a table that renders data from the database,
目前,当我的索引页(add_product.html)加载时,我有一个表格可以呈现数据库中的数据,
- first my url looks like >> 127.0.0.1:8000/product/
- Then once I hit the add button url changes to 127.0.0.1:8000/product/add_product/, no problem there, but
- when i try to add data again my url goes to 127.0.0.1:8000/product/add_product/add_productand i get a Page not found error
- 首先我的网址看起来像 >> 127.0.0.1:8000/product/
- 然后一旦我点击添加按钮 url 更改为127.0.0.1:8000/product/add_product/,没问题,但是
- 当我再次尝试添加数据时,我的 url 转到127.0.0.1:8000/product/add_product/add_product并且出现页面未找到错误
My views.py
我的意见.py
from models import Product,Category
from django.shortcuts import render_to_response,get_object_or_404
from django.http import HttpResponseRedirect
def index(request):
category_list = Category.objects.all()
product_list = Product.objects.all()
return render_to_response('product/add_product.html', {'category_list': category_list, 'product_list':product_list})
def add_product(request):
post = request.POST.copy()
category = Category.objects.get(name=post['category'])
product = post['product']
quantity = post['quantity']
price = post['price']
new_product = Product(category = category, product = product, quantity = quantity, price = price )
new_product.save()
category_list = Category.objects.all()
product_list = Product.objects.all()
return render_to_response('product/add_product.html', {'category_list': category_list, 'product_list':product_list})
My urls.py
我的网址.py
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('product.views',
url(r'^$', 'index'),
url(r'^add_product/$', 'add_product'),
)
How do i get the URL pointing back to my index page(add_product.html) ?
如何让 URL 指向我的索引页(add_product.html)?
采纳答案by Paritosh Singh
In the view of 127.0.0.1:8000/product/add_product/return this
在127.0.0.1:8000/product/add_product/返回这个
from django.http import HttpResponseRedirect
def add_product(request)
...........................
...........................
return HttpResponseRedirect('/')
It will redirect to index page. Also try to give url nameso that you can use reverseinstead of '/'
它将重定向到索引页面。还尝试提供url 名称,以便您可以使用reverse而不是 '/'
Thanks
谢谢
回答by Alasdair
You may have set your form's actionincorrectly in your template.
您可能action在模板中错误地设置了表单。
Instead of a relative url,
而不是相对网址,
<form method="post" action="add_product">
the action should have the absolute url:
该操作应具有绝对网址:
<form method="post" action="/product/add_product">
As an improvement, you can use the urltemplate tag, so that you don't need to hardcode the url in the template.
作为改进,您可以使用url模板标签,这样您就不需要在模板中对 url 进行硬编码。
{% load url from future %}
<form method="post" action="{% url 'add_product' %}">
The snippet above uses the new url syntax, by loading the new url tag.
上面的代码段通过加载新的 url 标记使用新的 url 语法。

