26๐
The solution @miki725 is absolutely correct. Alternatively, if you would like to use the args
attribute as opposed to kwargs
, then you can simply modify your code as follows:
project_id = 4
reverse('edit_project', args=(project_id,))
An example of this can be found in the documentation. This essentially does the same thing, but the attributes are passed as arguments. Remember that any arguments that are passed need to be assigned a value before being reversed. Just use the correct namespace, which in this case is 'edit_project'
.
- [Django]-Django: For Loop to Iterate Form Fields
- [Django]-Django filter JSONField list of dicts
- [Django]-How to assign items inside a Model object with Django?
4๐
This problems gave me great headache when i tried to use reverse for generating activation link and send it via email of course. So i think from tests.py it will be same.
The correct way to do this is following:
from django.test import Client
from django.core.urlresolvers import reverse
#app name - name of the app where the url is defined
client= Client()
response = client.get(reverse('app_name:edit_project', project_id=4))
- [Django]-Why there are two process when i run python manage.py runserver
- [Django]-Django Rest Framework model serializer with out unique together validation
- [Django]-How do I filter query objects by date range in Django?
3๐
Easiest way is by using kwargs
with reverse()
function:
from django.test import Client
from django.urls import reverse
url = reverse("edit_project", kwargs={"project_id": 4})
response = Client().get(url)
- [Django]-Convert Django Model object to dict with all of the fields intact
- [Django]-How to get the current URL within a Django template?
- [Django]-Django Footer and header on each page with {% extends }
2๐
The function resolve_url
is also more straightforward
from django.shortcuts import resolve_url
resolve_url('edit_project', project_id=4)
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
- [Django]-Automatic creation date for Django model form objects
- [Django]-How to revert the last migration?