Adding two IDs to my urls dispatcher
I'm relatively new to python/django. I'm having an issue with sending to IDs through my urls.py.
I am trying to add an admin to a business profile page in my project.
My views.py:
@login_require开发者_Go百科d
def make_admin(request, bus_id, user_id):
user = request.user
u = get_object_or_404(User, pk = user_id)
b = get_object_or_404(Business, pk = bus_id)
b.admin.add(u)
followcount = b.followers.count()
photo = BusinessLogo.objects.all().filter(business_link = bus_id)[:1]
return render_to_response('business/followers.html',
{'user':user, 'b':b, 'followcount':followcount, 'photo':photo, 'u':u}, context_instance=RequestContext(request))
In my template I am trying to pass the bus_id as well as the user_id but I keep getting a syntax error, which I am assuming is related to my urls.
My template:
...
{% if follow in b.admin.all %}
[<a href="{% url remove_admin b.id u.id %}">Remove Admin</a>]
{% else %}
[<a href="{% url make_admin b.id u.id %}">Make Admin</a>]
{% endif %}
...
My urls.py at the moment:
url(r"^make/(?P<bus_id>\d+)/(?P<user_id>\d+)/$", make_admin, name="make_admin"),
url(r"^remove/(?P<bus_id>\d+)/(?P<user_id>\d+)/$", remove_admin, name="remove_admin"),
I am just having a hard time figuring out how to add the user_id to my urls. The above example does not work.
Thanks everyone,
Steve
EDIT: The error Im presented with is:
Caught NoReverseMatch while rendering: Reverse for 'remove_admin' with arguments '(1L, '')' and keyword arguments '{}' not found.
The only thing i can see that seems wrong is {% if follow in b.admin.all %}
there's no follow variable in your context in the code you posted.
If you posted more details of your error, or the stack trace, it would be most helpful.
EDIT: Ok, your error is helpful :)
Caught NoReverseMatch while rendering: Reverse for 'remove_admin' with arguments '(1L, '')' and keyword arguments '{}' not found.
This means that the url reversal function got two arguments 1L
and ''
.
1L
i just the integer 1 as a python long integer, the ''
means that you passed in None
or a blank string.
Since you called the url reversal in your template with {% url remove_admin b.id u.id %}
the second argument is the value of u.id
. Check the value of the u
variable, it seems to not have a valid id
attribute, so it's probably not what you expect (I'd guess it's not a User
object at all)
You're not referencing the user object in the way you pass it to the context - you pass it as user
, but in the template you use u.id
.
精彩评论