django url tag problem - django

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.

Related

Url mismatch, django template link

I've got a prob with a link in the sidebar of my django site, in the template it's like that:
<li>Profile</li>
while in the urls.py:
url(r'^(?P<user_id>\d+)/profile/$', 'auth.views.show_profile', name='profile')
When i access it from the main page with url: e.g /1001/profile/ it loads fine but when I try to access it from another subpage with url: e.g /1001/forms/profile/ i get the error: The current URL, /1001/forms/profile/, didn't match any of these. How can i fix this?
It is because "profile" is a relative URL, and a relative URL is appended to the current URL - the resulting address is not valid across the whole site. Seems like you should use an absolute URL in your case.
At the template you can try something like:
Profile
UPDATE
To get request available in templates you have to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. I'm not sure if it is added by default.
You must have to add your second subpage url in urls like you did for /1001/profile/
url(r'^(?P<user_id>\d+)/form/profile/$', 'auth.views.show_profile', name='profile_form')
and also correct your code as #Paulo mentioned or you can also do it through reverse url.
Profile

Django: Why can I reverse a URL in the template but not in a view?

There's some weird stuff happing with the URL reversal code in django 1.4.
I have a view called settings.views.app_view. I have viewed the page by typing in the URL manually to verify that the basic URL pattern is working.
url(r'^app/$', 'settings.views.app_view', name='settings_app_view'),
I have reversed the URL in a template and it works.
{% url settings_app_view %}
So, the URL pattern works, and I can call get the URL in a template, click the link and view the correct page.
So why can't I get the URL in a view using reverse()? All the code is clearly there, and not only that, it's clearly configured and working correctly as I've seen the page and reversed the URL in a template.
I have to be missing something small; does anyone know what it is?
ViewDoesNotExist at /settings/app/
Exception Value: Could not import settings.views.app_view. View does not exist in module settings.views.
# The highlighted code
url = reverse("settings_app_view")
Where exactly in your code does reverse() get executed? If reverse() gets executed during importing the python file, you can get a recursive import. Unfortunately a recursive import can have different results: AttributeError can happen on modules that should have this attribute....
See: https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse-lazy

django pass extra option through url templatetag to view

I have the following line in my url file
url(r'^confirm/$', 'confirm', {'status':'live'}, name="confirm"),
As you can see I am passing the extra option status to the view which is described here
I would like to pass the status value through the template using the url templatetag. I tried
{% url confirm status='pending' %} but I get the following error:
Caught NoReverseMatch while rendering: Reverse for 'confirm' with arguments '()' and keyword arguments '{'status': u'pending'}' not found. Is it possible to do what I am trying to do?
How could this work? The url tag just outputs a URL that is valid in your urlconf and which maps the arguments into the URL. But your url has no place for alternative values for status - it's hard-coded.
If you want to pass parameters into a URL pattern, the pattern needs to have a space for the parameter.

View referenced by two urls and url tag

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.

Can I reference a named URL in an included URLConf using Django's "url" tag?

In my Django project, I used to have a single URLConf, urls.py at the root of the project. This URLConf included some named URLs using Django's url() function. In several templates, I reference these URLs with the url tag, à la {% url named_url %}. This worked fine.
The root urls.py became a bit unwieldy, so I split it off into a URLConf for each app, in app/urls.py. Some URLs still have names. Unfortunately, I get a TemplateSyntaxException when using the url tag in templates now. Specifically, the error message is:
Caught an exception while rendering: Reverse for 'myproj.myapp.new_test' with arguments '()' and keyword arguments '{}' not found.
Is there a way to reference the named URLs in the app-specific URLConfs using the url tag in Django?
You definitely can reference urls in included urlconfs via the url tag - that's in fact what you're supposed to do. However, I've always found the url tag and the reverse() function to be very flaky and error-prone, so these errors do sometimes occur.
My suggestion would be to give all your urls a name, no matter which urlconf they are in. Then you just need to refer to the actual name - you don't need to qualify it with the name of the app or urlconf or anything. See if that works.
Are you referencing each app's urls.py?
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^app1/', include('app1.urls')),
(r'^app2/', include('app2.urls')),
)
from Django Docs
Probably not useful to you but hopefully useful to the next bloke searching for an answer:
I am using Django 1.4 and what solved it for me was not using quotes in the template tag:
{% url 'name_of_view' some_var %}
became this:
{% url name_of_view some_var %}
And that cured it. I didn't see any mention of this in the django docs and the examples in the docs use quotes. So this seems a little buggy but hopefully will keep someone from pulling out their hair.
https://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns
I can recommend you two things:
Use name in url patterns
Do not do references to the project name inside a app(like you did with "myproj.myapp.new_test". If this was the "right way to do", you should only reference as "myapp.new_test"