Question

The purpose of this section of code is to show all of the requests to join a group in a template similar to that shown below:

Request 1 | Add | Delete
Request 2 | Add | Delete
Request 3 | Add | Delete
....

What I have thought to do is to to make the 'add' and 'delete' button href's to a function in the view. However I am wondering what the proper way to pass a **kwarg from a template to a view. Else if there is any better way to accomplish this?

template

{% for asking in requested %}
    <a href={% url 'group_judge_request' group_profile.slug decision=0 %}>Cut {{ asking.user.username }}</a>
    <a href={% url 'group_judge_request' group_profile.slug decision=1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}

url

url(r'^judge_request/(?P<gslug>[\w-]+)$',
    group_judge_request,
    kwargs={'decision':'decision'},
    name='group_judge_request'),

view group_judge_restart

def group_judge_request(request, gslug, decision):

view group_requested_invites

def group_requested_invites(request, gslug):
  ....
  requested = GroupRequestToJoin.objects.filter(group=group_profile.group, checked=False)
  return render(request, "groups/group_requested_invites.html", {
    'requested' : requested,
    })

Error:

Don't mix *args and **kwargs in call to reverse()!
Was it helpful?

Solution

I don't think there is a way to pass *kwargs like this from the template using the built-in url template tag.

There are two ways you can do this, one is to create two separate urls and pass the decision as a kwarg to the view:

urls.py

url(r'^judge_request_cut/(?P<gslug>[\w-]+)$',
    group_judge_request,
    kwargs={'decision': 0},
    name='group_judge_request_cut'),
url(r'^judge_request_keep/(?P<gslug>[\w-]+)$',
    group_judge_request,
    kwargs={'decision': 1},
    name='group_judge_request_keep'),

template

{% for asking in requested %}
    <a href={% url 'group_judge_request_cut' group_profile.slug decision=0 %}>Cut {{ asking.user.username }}</a>
    <a href={% url 'group_judge_request_keep' group_profile.slug decision=1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}

Or you could pass the integer as a parameter:

urls.py

url(r'^judge_request/(?P<gslug>[\w-]+)/(?P<decision>\d{1})$',
    group_judge_request,
    name='group_judge_request'),

template

{% for asking in requested %}
    <a href={% url 'group_judge_request' group_profile.slug 0 %}>Cut {{ asking.user.username }}</a>
    <a href={% url 'group_judge_request' group_profile.slug 1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}

OTHER TIPS

I think you want to use a url query. So your url will be as follows:

<a href="{% url 'group_judge_request' group_profile.slug %}?decision=0">Cut {{asking.user.username }}</a>

You can then go on to list the queries using:

request.META['QUERY_STRING']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top