【问题标题】:Django template rendering under a wrong URL在错误的 URL 下渲染 Django 模板
【发布时间】: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


    【解决方案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 设置似乎有问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2018-11-27
    • 2017-07-03
    • 2020-11-18
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多