cybertextron
cybertextron

Reputation: 10961

Passing an id to a url link - Django

I need to pass a cluster_id in my show.html page, in case the user wishes to halt a cluster created. My urls.py looks like:

from django.conf.urls import url
from django.core.context_processors import csrf
from django.shortcuts import render_to_response

from clusters import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^register/', views.register, name='register'),
    url(r'^show/', views.show, name='show'),
    url(r'^login/', views.user_login, name='login'),
    url(r'^logout/', views.user_logout, name='logout'),
    url(r'^destroy_clusters/', views.destroy_clusters, name='destroy_clusters'),
    url(r'^halt_clusters/(?P<cluster_id>\d+)/$', views.halt_clusters, name='halt_clusters'),
    url(r'^check_log_status/', views.check_log_status, name='check_log_status'),
    url(r'^read_cluster_log/', views.read_cluster_log, name='read_cluster_log'),
]

and in my show.html I have:

{% for cluster in clusters %}
        <tr>
            <td><a href="#">{{ cluster.id }}</a></td>
            <td><a href="{% url halt_clusters cluster_id=cluster.id %}">Halt</a></td>
        <tr>
{% endfor %}

so when I execute the webpage, I have:

NoReverseMatch at /clusters/register/
Reverse for '' with arguments '()' and keyword arguments '{u'cluster_id': 19L}' not found. 0 pattern(s) tried: []

and this is driving me crazy!! Why can't I just pass the id there? I looked this example: Passing an id in Django url but it doesn't seem to work for me. Help? My Django version is 1.8.dev

Upvotes: 1

Views: 5459

Answers (1)

isagalaev
isagalaev

Reputation: 1213

In recent Django's you should quote literal URL names in the tag:

{% url "halt_clusters" cluster_id=cluster.id %}

Otherwise Django will look for halt_clusters variable in your context and use its value as the URL name.

Oh, and while you're at it, you don't have to specify the keyword parameter name, so this should work as well:

{% url "halt_clusters" cluster.id %}

Upvotes: 1

Related Questions