【发布时间】:2018-12-15 20:05:04
【问题描述】:
我正在开发一个应用程序(学习 django),它有一种主模板。
和 view.py
@login_required
def home(request):
return render(request, '../templates/mainSection/home.html')
def createshipment(request):
if request.method == "GET":
# shipmentNumber is defined by 'SHN-000' + next Id in the shipment Table
try:
# trying to retrive the next primaryKey
nextId = Shipment.objects.all().count()
nextId += 1
except:
# if the next ID is null define the record as the first
nextId = 1
# creating the form with the shipment ID
form = CreateShipmentForm(initial={'shipmentNumber':'SHN-000' + str(nextId)})
return render(request, '../templates/mainSection/createshipment.html', {'form': form})
def saveshipment(request):
if request.method == 'POST':
form = CreateShipmentForm(request.POST)
if form.is_valid():
try:
form.save()
except (MultiValueDictKeyError, KeyError) as exc:
return HttpResponse('Missing POST parameters {}'.format(exc), status=400)
else:
messages.error(request, form.errors)
return render(request, '../templates/mainSection/fillshipment.html')
def viewshipment(request):
return render(request, '../templates/mainSection/viewshipment.html')
def fillshipment(request):
if request.method == "GET":
# creating the form
productForm = CreateProductForm()
# Retrieving The Product types for the ShipmentForm
productType_list = ProductTypes.objects.all()
shipment_list = Shipment.objects.all()
return render(request, '../templates/mainSection/fillshipment.html', {'productTypes': productType_list, 'shipments': shipment_list, 'productForm': productForm})
还有 urls.py
urlpatterns = [
path('home/', views.home,name="home"),
path('home/createshipment/',views.createshipment,name="createshipment"),
path('home/createshipment/saveshipment/',views.saveshipment,name="saveshipment"),
path('home/fillshipment/',views.fillshipment,name="fillshipment"),
path('home/viewhipment/',views.viewshipment,name="viewshipment"),
]
我要解决的问题是,
提交表单并导航到下一个表单后,模板与上一个 URL 下的模板不同。例如,一旦创建了一个货件 (home/createshipment/),我想导航以填充货件 (home/fillshipment/)。错误的 URL (home/createshipment/saveshipment/) 下的 Html 呈现正常
我做错了什么?
【问题讨论】:
标签: django django-templates django-views