在Django中提交表单后重定向到索引页面 [英] Redirect to index page after submiting form in django

查看:71
本文介绍了在Django中提交表单后重定向到索引页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了添加产品数据后重定向到索引页面之外,所有其他操作均正常工作,目前,在保存数据之后,该内容已重定向到 127.0.0.1:8000/product/add_product/add_product

当前,当我的索引页面(add_product.html)加载时,我有一个表,用于呈现数据库中的数据,

  1. 首先,我的网址看起来像>> 127.0.0.1:8000/product/
  2. 然后,一旦我点击添加按钮,URL就会更改为 127.0.0.1:8000/product/add_product/,但是没有问题,但是
  3. 当我尝试再次添加数据时,我的网址转到 127.0.0.1:8000/product/add_product/add_product ,但出现找不到页面"错误

我的views.py

 从模型导入产品,类别从django.shortcuts导入render_to_response,get_object_or_404从django.http导入HttpResponseRedirectdef索引(请求):category_list = Category.objects.all()product_list = Product.objects.all()返回render_to_response('product/add_product.html',{'category_list':category_list,'product_list':product_list})def add_product(request):帖子= request.POST.copy()category = Category.objects.get(name = post ['category'])产品=帖子['产品']数量=职位['数量']价格=帖子['price']new_product =产品(类别=类别,产品=产品,数量=数量,价格=价格)new_product.save()category_list = Category.objects.all()product_list = Product.objects.all()返回render_to_response('product/add_product.html',{'category_list':category_list,'product_list':product_list}) 

我的urls.py

来自django.conf.urls.defaults的

 导入模式,包括urlurlpatterns = pattern('product.views',url(r'^ $','index'),url(r'^ add_product/$','add_product'),) 

如何获取指向索引页(add_product.html)的URL?

解决方案

127.0.0.1:8000/product/add_product/的视图中返回此

从django.http中的

 导入HttpResponseRedirectdef add_product(请求)........................................................................返回HttpResponseRedirect('/') 

它将重定向到索引页面.另外,尝试提供 URL名称,以便您可以使用反向代替'/'

谢谢

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

Currently when my index page(add_product.html) loads, I have a table that renders data from the database,

  1. first my url looks like >> 127.0.0.1:8000/product/
  2. Then once I hit the add button url changes to 127.0.0.1:8000/product/add_product/ , no problem there, but
  3. when i try to add data again my url goes to 127.0.0.1:8000/product/add_product/add_product and i get a Page not found error

My views.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

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) ?

解决方案

In the view of 127.0.0.1:8000/product/add_product/ return this

from django.http import HttpResponseRedirect

def add_product(request)
    ...........................
    ...........................
    return HttpResponseRedirect('/')

It will redirect to index page. Also try to give url name so that you can use reverse instead of '/'

Thanks

这篇关于在Django中提交表单后重定向到索引页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆