当前,当我的索引页面(add_product.html)加载时,我有一个表,用于呈现数据库中的数据,首先,我的网址看起来像>> 127.0.0.1:8000/product/ 然后,一旦我点击添加按钮,URL就会更改为 127.0.0.1:8000/product/add_product/,但是没有问题,但是当我尝试再次添加数据时,我的网址转到 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_productCurrently when my index page(add_product.html) loads, I have a table that renders data from the database,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, butwhen 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 errorMy views.pyfrom models import Product,Categoryfrom django.shortcuts import render_to_response,get_object_or_404from django.http import HttpResponseRedirectdef 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.pyfrom django.conf.urls.defaults import patterns, include, urlurlpatterns = 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 thisfrom django.http import HttpResponseRedirectdef 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中提交表单后重定向到索引页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS!