网址错误的Django模板渲染

时间:2018-12-15 20:05:05

标签: django django-templates django-views

我正在开发具有某种主模板的应用程序(学习django)。

enter image description here

和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 /)。 HTML在错误的URL(home / createshipment / saveshipment /)下可以正常渲染

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

POST成功后,您应该始终重定向,而不是直接呈现模板。还要注意,如果表单无效,则应重新显示当前模板。所以:

def saveshipment(request):
    if request.method == 'POST':
        form = CreateShipmentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('fillshipment')

        else:
            messages.error(request, form.errors)
        return render(request, '../templates/mainSection/createshipment.html', {'form': form})

我选择了try / except,因为您绝对不需要它。如果您遇到这些错误之一,则说明您的表单存在问题(您可能应该在另一个问题中提出该问题)。

也要注意的是,所有模板路径都必须以'../'作为前缀似乎很奇怪。同样,您不需要这样做,因此TEMPLATES设置似乎有些问题。