除了在添加產品數據後重定向回索引頁之外,所有工作都正常,當前我的數據獲取保存後它被重定向到127.0.0.1:8000/product/add_product/ add_product在django提交表單後重定向到索引頁
目前在我的索引頁(add_product.html)負載,我有一個從數據庫中呈現的數據表,
- 第一我的網址看起來像>>127.0.0.1:8000/product/
- 然後,一旦我打的添加按鈕網址更改爲127.0.0.1:8000/product/add_product/,有沒有問題,但
- 當我嘗試添加數據再次我的網址去127.0.0.1:8000/產品/ add_product/add_product,我得到一個找不到網頁錯誤
我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})
我的urls.py
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('product.views',
url(r'^$', 'index'),
url(r'^add_product/$', 'add_product'),
)
如何獲取指向我的索引頁(add_product.html)的URL?
是否奏效? –
它的工作,我不得不添加「返回HttpResponseRedirect('/產品')」,因爲它的產品模板文件夾內,謝謝很多@Paritosh辛格 – shabeer90
好吧嘗試使用URL反向命名爲URL,PLZ不硬編碼網址,會影響代碼的可維護性。 –