Sri
Sri

Reputation: 293

Django template rendering under a wrong URL

Im working on an application (to learn django) where it has kind of master template.

enter image description here

and the 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})

and the 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"),
] 

The problem that Im trying to solve is,

After submitting a form and navigating to the next, the template diffrent under the previous URL. For example once a shipment created (home/createshipment/) I want to navigate to fill shipment (home/fillshipment/). The Html render fine under a wrong URL (home/createshipment/saveshipment/)

What am I doing wrong?

Upvotes: 0

Views: 481

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

After a successful POST you should always redirect, not render a template directly. Note also that if the form is invalid you should redisplay the current template. So:

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})

I've taken out that try/except because you definitely shouldn't need it; if you are getting either of those errors, there's something wrong with your form (which you should probably ask about in a separate question.)

And also also also note, it seems very odd that you need to prefix all the template paths with '../'. Again, you shouldn't need to do that, so something seems wrong with your TEMPLATES setting.

Upvotes: 2

Related Questions