url(r'^employee/create/(?P<employee_type>[\w-]+)$',
staff_member_required(EmployeeCreateView.as_view()),
name='employee-create'),
I am using the above url config for following url:
Create Product Engineer
Create Product Developer
But this gives me an error saying matching reverse url is not found. How may I fix this issue?
First you have to fix the url pattern, which is missing a slash, as pointed out by #Evert:
r'^employee/create/(?P<employee_type>[\w-]+)/$'
In your template you can call that url pattern as you have done it:
"{% url "myapp_app:employee-create" employee_type=product_eng %}"
Here it is important that myapp_app is the namespace, and employee-create the name of the url pattern.
From here we can't see whether you have this namespace specified.
For the part employee_type=product_eng is important that the variable product_eng actually exists in the template and holds a meaningful value.
You can simplify that by writing only:
"{% url "myapp_app:employee-create" product_eng %}"
Related
I know this is an armature question but here goes.
I have a url path as follows:
path('projects/<s>', PL.projects),
And I pass a string from the html template by putting it into an href tag like so projects/some_string. this works once but then the base url changes to <ip>/projects/some_string. so when I try to excite the path to pass a string in that domain then I get an error as the url is now <ip>/projects/projects/some_string.
How do I set it up so that I can pass as many strings as possible as many times as possible without having to clean my url in the browser everytime.
Django has inbuilt url lookup features
path("some_random_url_link_1/", views.Link1View.as_view(), name="url_link_1"),
path("some_random_url_link_2/<int:some_id>/<slug:some_slug>/", views.Link2View.as_view(), name="url_link_2"),
in your template you can use it like this, and pass variables/parameters like this. FYI you don't need to use {{variable}} tag here
<a href="{% url 'url_link_1' %}" >Link 1</a>
<a href="{% url 'url_link_2' some_id=id1 some_slug=random_slug %}" >Link 2</a>
Learn how to use the reverse() function and the url template tag and your problems will be gone.
Those functions are built-in with Django and can handle all of that nasty URL stuff.
Reverse: https://docs.djangoproject.com/en/3.2/ref/urlresolvers/
Url Template tag: https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#url
Using django1.9
my urls would look something like example.com/my_url/cachebuster
in my email.html template I have something like
{% url my_url cachebuster %}
Now cachebuster is actually "%%cachebuster%%"
This is a substitution string which my email api is parsing for, and whenever it finds "%%cachebuster%%" it replaces it with the specific users cachebuster.
My issue is that the django {% url %} function automatically encodes that string to "%25%25cachebuster%25%25"
Which means the api never finds "%%cachebuster%%" and never replaces it
So instead of a dynamic url, its always example.com/my_url/%25%25cachebuster%25%25
How can I fix this, without changing the substitution string? I've tried using mark_safe but haven't got it to work
The only way I can think of is to hardcode /my_url/{{cachebuster}} without using the django url tag, but this is bad practice because the url templatetag was created for a reason
I have problem with url tag. I want to redirect to a function that is in for eg
project_name.forum.views.function. Here is how i try to create url
{% url forum.views.function %}
it gives me this error:
Caught ViewDoesNotExist while rendering: Tried forum in module project_name.forum.views. Error was: 'module' object has no attribute 'forum'
I added this url in urls.py(I can access it directly) What am I doing wrong?
The url tag is used to reference named urls. E.g.
url(r'^$',
login_required(views.user_babies),
name='babystats_user_babies'),
Then you use {% url babystats_user_babies %} (the url pattern name, not the view name)
It sounds more like an incorrectly set up URL conf. You get that error when you specify a view that doesn't exist.
The url tag failing gives you a failure to reverse url with params... message.
What does your URL conf look like? Does project_name.forum.views.forum exist?
I mean, I find it odd you can visit the page at all, but that's the first place I'd look.
I have seen this error before with django url reversing stemming from the urlconf being set up with a root like projectname.app.views.view instead of app.views.view, so it chokes on the reverse without the name of the project.
Another common issue would be that the url takes an extra parameter that can be blank, and it needs you to pass an empty string or whatnot.
I am using url tag in my template for a view, that is used by two different urls. I am getting the wrong url in one place. Is there any way to force django to retrieve different url? Why it doesn't notify my, that such conflict occured and it doesn't know what to do (since python zen says, that is should refuse temptation to guess).
Code in template:
{% url djangoldap.views.FilterEntriesResponse Entry=entry.path as filter_url %}
Code in urls:
(r'^filter_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'filter_entries.html',
'results_template': 'filter_results.html'}),
(r'^choose_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'search_entries.html',
'results_template': 'search_results.html'}),
As you can see, those two urls use the same view, but with different templates. How I can force django to retrieve former url, rather than latter?
Name your URLs by adding another item to the tuple:
(r'^choose_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'search_entries.html',
'results_template': 'search_results.html'},
'sensibleprefix-choose_entries') # <-- this is the name
Then you can use the name in the URL tag.
My URLconf contains this pattern:
url(r'^accounts/logout/$','django.contrib.auth.views.logout', name="logout"),
And I've trying to reverse that in a template with the URL tag like this:
logout
But I keep getting the following error:
Reverse for 'logout' with arguments '()' and keyword arguments '{'next_page': u'/first-page/child/'}' not found
I thought django.contrib.auth.views.logout is supposed to take an option next_page parameter. I'm sure I'm missing something obvious, but I'm not sure what it is.
Yes you're right, django.contrib.auth.views.logout does accept an optional "next_page" parameter, but don't forget that the "url" tag matches to urlconf patterns, not views, so it's not aware of what is or isn't a parameter of a view. So this suggests that you need to make "next_page" a named group in the regexp for the above pattern, which you could do, but there's an easier way to handle redirects...
Looking at django.contrib.auth.views.logout, you can see that in the absence of a "next_page" parameter, the view redirects to whatever url is provided in either request.GET or request.POST with the key "redirect_field_name", a parameter which defaults to "REDIRECT_FIELD_NAME" which in turn defaults to the string "next". So leaving your urlconf the way it is, you can do something like this in your template:
<a href='{% url logout %}?next={{ request.path }}'>logout</a>
Basically Django's URL dispatcher is looking at the urlconf and that argument and saying "I don't know where to put this argument" because it doesn't look at the view functions the urls point to, only the urlconf and the patterns in it.
Right now there's no place in your url pattern for that argument.
i.e. you can call django.contrib.auth.views.logout with the extra arguments if you write your own pattern for it or if you call it from your own view, but not from its default url pattern.
One of these url patterns might work for you (not tested):
url(r'^accounts/logout/(?P<next_page>.*)?$','django.contrib.auth.views.logout', name="logout"),
url(r'^accounts/logout/$','django.contrib.auth.views.logout', kwargs={'next_page':None}, name="logout"),
Hope that helps!