Search code examples
djangodjango-templatesanchordjango-urls

How can I change the sites url depending on the anchor tags?


urls.py:

app_name='listing'

urlpatterns = [
    url(r'^listing/select/$',views.platformselect,name='listingselect'),
    url(r'^listing/',views.listingpage,name='listingpage'),

]

views.py:

def platformselect(request):
    
    return render(request,'listing/platform_select.html')

platform_selection.html:

<a href="{% url 'listing:listingpage' %}">amazon</a>
<a href="{% url 'listing:listingpage' %}">ebay</a>
<a href="{% url 'listing:listingpage' %}">google</a>

Greetings. How can I change the urls according to the <a> options. For example when I click the 'amazon' anchor tag I want it go to "mysite.com/listing/amazon". Same for the other options. How can I change the sites url depending on the anchor tags?


Solution

  • You may need to make slight changes according to your project:

    In your urlpatterns:

    path("listing/<int:id>/", views.listing_detail, name="listing-detail"),
    

    In your template:

    <a href="{% url 'listing:listing-detail' listing.id %}">{{ listing.name }}</a>
    

    In your view function:

    def listing_detail(request, id):
        listing = Listings.objects.get(id=id)
        return render(request, "listing/listing_detail.html", {
            "listing": listing
        })