Error while passing value to view from template using url
Error :
Error during template rendering
In template D:\TONO\DJANGO_TASK\user_auth\templates\auth_app\other-user-listing.html, error at line 114
Reverse for 'add_to_group' with arguments '(name: Administrator,)' not found. 1 pattern(s) tried: ['auth_app\\/<str:username>/']
This my urls.py
url(r'^<str:username>/', views.add_to_group, name="add_to_group"),
This is the call from template
<i class="icon-plus">Add</i>
In your call from template see if you do this if that solves the problem, I remember I was facing this issue and this is what I did:
The syntax for specifying url is {% url namespace:url_name %}. So, check if you have added the app_name in urls.py.
Your url defines a named argument for the url "add_to_group". You should pass this argument as a keyword argument
{% url 'add_to_group' username=username %}
You are mixing the new and old style urls. To use the new style you should use path
path('<str:username>/', views.add_to_group, name="add_to_group"),
Error corrected using :
url(r'^auth_app/add_to_group/(?P<username>[-\w]+)/$', views.add_to_group, name='add_to_group'),
Related
Basically trying to do
{% url dynamic_url_name dynamic_parameter_name=dynamic_parameter_value %}
Tried the simplest approach of
{{entry}}
{% include 'mainsite/children/title_template.html' with
the_title=title_text
is_title_page=True
entries_of="title"
belongs_to="profile"
belongs_to_url_arg="user"
belongs_to_url_arg_value="author"
%}
But unfortunately this resulted in utter failure of
From this I can tell that parameters can't be context variables, so what I can try to do next is to simply unpack a dictionary as I would do inside python with something like
{% url **{dynamic_parameter_name:dynamic_parameter_value} %}
But I have no idea if it is possible inside django templates, and if possible how?
My urls look like
re_path(r'^title/(?P<title_text>[a-zA-Z0-9-]+)/$', TitlePage.as_view(), name='title')
re_path(r'^user/(?P<user>[a-zA-Z0-9-]+)/$', ProfilePage.as_view() ,name='profile')
And I the url is choosen either by a context variable or in an include tag, hence it is a variable.
{% url url_variable xxx=value %}
Now, url_variable is already a part of django, url tag accepts variable as it's first argument. But the xxx is not always the same, rather it changes according to url_variable, in this particular case; if url_variable is title, I want xxx to be title_text and if it is profile I want it to be user.
The parameter name is held in belongs_to, so if this was a regular python function, I could've simply done
url(url_variable, **{belongs_to: value})
and it would've unpacked it with the correct parameter name. So I need some kind of equivalency of this in template processor
I think you're overcomplicating things. You haven't shown your views themselves, but I can't see why they couldn't all take a commonly-named parameter - say, param - that does the specific work. So the URLs could be:
re_path(r'^title/(?P<param>[a-zA-Z0-9-]+)/$', TitlePage.as_view(), name='title')
re_path(r'^user/(?P<param>[a-zA-Z0-9-]+)/$', ProfilePage.as_view() ,name='profile')
and now you can do
{% url dynamic_url_name param=dynamic_parameter_value %}
**kwargs are not specifically supported as a parameter for the {% url %} tag, but there are a few workwarounds.
If you have a path in urlpatterns defined as:
path('myurl/<str:some>/<str:thing>', name='myurl')
Then you could have a filetoinclude.html:
My Url
And then in your main.html:
{% include 'filetoinclude.html' with urlname='myurl' some="aaa" thing="bbb" %}
And when you render you will have something like:
My Url
But obviously the issue is that maybe you want to address specific parameters of the URL when you reverse it. For this reason you could create a templatetag like:
from django.urls import reverse
from django import template
register = template.Library()
#register.simple_tag
def dynamic_url(urlname, **kwargs):
return reverse(urlname, kwargs=kwargs)
and then in your filetoinclude.html you could do:
{% load url_extended %}
{% dynamic_url urlname some=some thing=thing %}
which will yield the same URL as before.
Everything was working fine when i have added fobi in my project but somehow while i have added namespace in my url it start giving me the error like below.
Main urls.py
re_path(r'^formbuilders/', include('apps.form_builders.urls'),),
Form_builders urls.py
url(_(r'^$'), view=dashboard, name='fobi.dashboard'),
Template
{% url 'fobi.dashboard' as fobi_dashboard_url %}
Reverse for 'fobi.dashboard' not found. 'fobi.dashboard' is not a valid view function or pattern name.
I have visited many site but did not received any quick fix.
Could you please help me out to fix this issue?
I've visited the documenation at https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#std:templatetag-url several times and i cant seem to get this syntax to work anywhere:
{% url 'view' obj.id %}
I have a view that takes one parameter so this should work but im only getting NoReverseMatch exception for some strange reason.
When doing it like this:
{% url 'view' obj.id as test %}
test
..im getting the correct url returned back and the link address displays correctly, but when using the above mentioned syntax without setting is as a variable it doesnt work, when i for example use it directly in an element.
When trying to do this which im trying to do:
{% url 'view' obj.id as test %}
<input type="hidden" value="{{ test }}">
im not getting any error but it doesnt seem like im getting any value in the value field because if there were a value in the field the code would do something, and when replacing the variable with a hard-coded string it does work.
When doing this:
{% url 'view' obj.id as test %}
{{ test }}
just to try to print the value it doesnt return anything which i find strange because when using it with the a element in html as shown at the first code line above it displays the correct url.
So basically, im only getting the {% url 'view' obj.id %} syntax to work with the a element of html and only if i define it as a variable.
I would like to use the {% url 'view' obj.id %} syntax in order to have a DRY code. According to the documenation this should work, does anyone have a clue about why this isnt working ? If you need more information then please let me know and i will update the question with the necessary information.
UPDATE:
I'm currently using django 1.6.
The typo in the second snippet has been corrected.
The exact line from urls.py is (im at this page, using the comment system at /comment/ which should do a reverse to the display_story view (it works without problems when hardcoding the value attribute of the input html tag but not with the url function):
url(r'^story/display/(?P<specific_story>\d+)/$', 'base.views.display_story', name='display_story'),
url(r'^comments/', include("django.contrib.comments.urls"))
I have also tried the url function just on the application i have created without going through the comments application but i get the same problem.
This is the error message:
NoReverseMatch at /story/display/1/
Reverse for 'display_story' with arguments '(1,)' and keyword arguments '{}' not found.
Even though i know that the view exists and takes one argument.
This is the html code:
<input type="hidden" name="next" value="{% url 'display_story' story_details.id %}">
I have named views with the name argument and i apply the namespace when necessary.
The django docs says:
exception NoReverseMatch
The NoReverseMatch exception is raised by django.core.urlresolvers when a matching URL in your URLconf cannot be identified based on the parameters supplied.
but that doesnt seem to be the case.
This urls.py:
url(r'^story/display/(?P<specific_story>\d+)/$', 'base.views.display_story', name='display_story')
should match this view code:
def display_story(request, specific_story):
""" Display details for a specific story. """
story_details = Story.objects.get(id=specific_story)
return render(request, "base/story/display_story.html", {
'story_details': story_details,
})
but for some reason django doesnt think the parameter sent is the the one the function receives when it clearly is stated so in the code.
Update 2:
When giving a keyword argument instead of a positional argument i get this error:
NoReverseMatch at /story/display/1/
Reverse for 'display_story' with arguments '()' and keyword arguments '{u'specific_story': 1}' not found.
This code is used:
{% url 'display_story' specific_story=story_details.id %}
Update 3:
I will update the question with the values of the local vars for the reverse function.
To add some additional infor i ran some code in the python shell:
>>> import base.views
>>> from django.core.urlresolvers import reverse
>>>
>>> test=1
>>> reverse("display_story", kwargs={'test': test})
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/exceed/code/projects/python-2.7/lib/python2.7/site-packages/django/core/urlresolvers.py", line 496, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/exceed/code/projects/python-2.7/lib/python2.7/site-packages/django/core/urlresolvers.py", line 416, in _reverse_with_prefix
"arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'display_story' with arguments '()' and keyword arguments '{'test': 1}' not found.
>>>
>>>
>>> reverse("display_story", args=[1])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/exceed/code/projects/python-2.7/lib/python2.7/site-packages/django/core/urlresolvers.py", line 496, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/exceed/code/projects/python-2.7/lib/python2.7/site-packages/django/core/urlresolvers.py", line 416, in _reverse_with_prefix
"arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'display_story' with arguments '(1,)' and keyword arguments '{}' not found.
Unless your view is named 'view' in the urlconf, you should use the full python dotted import path in the tag. For example:
{% url 'my_app.views.my_view' obj.id %}
If you want to test it properly, you can also use the django shell
python manage.py shell
and try the 'reverse' function:
from django.core.urlresolvers import reverse
reverse('my_app.views.my_view' args=[1])
>> '/my/awesome/url/1'
edit: also make sure that you didn't namespace your urls, if you did, you should include the namespace in the url tag:
{% url 'namespace:view' obj.id %}
another edit, because I have a feeling this might be it. I apologise for abusing the answer system instead of comments, but since I'm only starting out my reputation is too low.
Can you post the full urlconfig from your root urls.py up until the 'misbehaving' url? I have had cases where an url captured a group (say, an ID) and then included a bunch of other urls, leading to two required arguments for the reverse function.
As the title implies I've got the NoReverseMatch error, but my url.py has the corresponding named url. I feel pretty confident I've missed something simple, but being new I can't seem to find my own problem.
ERROR: (Generated in the below employee_edit.html file)
Caught NoReverseMatch while rendering: Reverse for ''employee_new''
with arguments '()' and keyword arguments '{}' not found.
\home\username\mysite\myapp\views.py:
class v_EmployeeCreate(CreateView):
model = Employee
template_name = 'employee/employee_edit.html'
def get_success_url(self):
return reverse('employee_list')
\home\username\mysite\url.py:
from myapp.views import v_EmployeeCreate, v_EmployeeList
urlpatterns = patterns('',
< ... snip ...>
url(r'^newEmployee$', v_EmployeeCreate.as_view(), name="employee_new"),
)
\home\username\mysite\myapp\templates\employee\employee_edit.html (line 7):
<form action="{% url 'employee_new' %}" method="POST">
I feel like there is a file path issue, but I'm not sure how I would resolve that. The named URL works to get me to the template, but then the template itself fails to generate a url.
For the sake of documentation so far I have:
reloaded django
checked spelling
confirmed non-template functionality (Same setup without the template tags is fine. page loads)
Working from tutorial: http://effectivedjango.com/tutorial/views.html#creating-contacts
I think you are using an old version of Django - before 1.5 (current is 1.6). The clue is that your error message has two single-quotes around the view name: in those older versions, you shouldn't put quotes around the name in the url tag.
You should (preferably) upgrade Django, or (if you really can't do that) use {% url employee_new %}
in views.py I have this:
def logout(request,key):
auth.logout(request)
return HttpResponseRedirect(reverse('airAgency.views.index',args=[key]))
in index template i wanna when user click on a link,logout view run:
logout
I wanna pass key parameter to logout view,Imagine I have an object named agn and it's WebSite field is going to pass to logout view, s.th like this:
<a href="{% url airAgency.views.logout agn.WebSite %}">
Imagine agn.WebSite has the value of mastane above code cause this error:
Caught NoReverseMatch while rendering: Reverse for 'airAgency.views.logout' with arguments '(u'mastane',)' and keyword arguments '{}' not found.
what's the right way to do this?
Caught NoReverseMatch - usually means that the url is being called by incorrect arguments, or that something else is wrong when {% url %} tries to call the url specified.
You need to define url pattern for that view function to be called in urls.py. And in the url pattern specify a view function.
urls.py
url(r'^logout/(?P<key>[a-zA-Z0-9\-]+)/$', "airAgency.views.logout", name="logout"),
index.html
<a href="{% url logout agn.WebSite %}">
This might not work if you just copy paste it because I don't know the exact project setup.
The key is to have urls.py where regex is used to create patterns for how the url looks like, then a view function that needs to be invoked, and finally a name, which you can then use in your templates.
This is all explained in the basic Django tutorial.