Home > database >  Django redirect using an object id?
Django redirect using an object id?

Time:10-26

The main issue I'm having is using the {% url 'Name' %} feature

In my html file "productview.html"

{% extends "droneApp/base.html" %}

{% block content %}

<div>
    {{ objects.name }}<br>    
</div>

<button onclick="window.location.href='{% url 'Checkout/{{objects.id}}' %}'">
{% endblock %}

The productview.html page's url is

http://127.0.0.1:8000/Store/Product/1

Where 1 is the product id

I'm trying to redirect to the checkout page using a button. But I don't understand how to redirect to my html Checkout.html

In a previous html file we used

href='Checkout/{{item.id}}'

but if we use that in this file it just causes the urls to stack into

http://127.0.0.1:8000/Store/Product/Checkout/1

instead of

http://127.0.0.1:8000/Store/Checkout/1

How do I properly redirect to the right html file?

urls.py file

from django.urls import path
from  .import views

urlpatterns = [
    path('', views.Store, name= "Store"),
    path('Checkout/<id>', views.createlocation, name="Checkout"),
    path('Product/<id>', views.product_details, name="Product")
]

Which is imported from another urls.py file

path('Store/', include('storeApp.urls')),

CodePudding user response:

You should use the following syntax -

{% url 'view-name' param_1=value_1 %}

Thus it will be as,

<button onclick="window.location.href='{% url 'Checkout' id=objects.id %}'">
  • Related