Can't reverse match url in Admin Changeform - django

I read lot's of docs, tried everything and still can't understand why my template returns Reverse for '' with arguments '(1,)' and keyword arguments '{}' not found error. Please see error dump here: http://dpaste.com/721187/
The tag I use in change_form.html template is {% url pdfview 1 %}
The class FVatAdmin(admin.ModelAdmin) has get_urls method which looks like this:
def get_urls(self):
urls = super(FVatAdmin, self).get_urls()
my_urls = patterns('',
url(r'^view/(?P<id>\d+)', self.admin_site.admin_view(self.pdf_view), name="pdfview"),
url(r'^js/calculate', self.admin_site.admin_view(self.calculate), name="calc"),
)
return my_urls + urls
The url and pdfview defined above work just fine, but somewhat don't resolve via {% url pdfview 1 %} in a template and via reverse('pdfview', args={1}) in a view or via shell.
I just can't understand what I'm doing wrong. I'm a newbie in Django... H E L P :)

Put url name in quotes.
{% url "admin:pdfview" 1 %}
UPDATE: this applies only for Django 1.3/1.4 if:
{% load url from future %}
is used.

Django admin urls are namespaced in order not to clash with other urls.
Try doing the following {% url admin:pdfview 1 %}
See this for details:
https://docs.djangoproject.com/en/1.4/topics/http/urls/#topics-http-reversing-url-namespaces

Related

django template url from another app

I have to be missing something silly. I have a {% url %} in a template where the action is from another app. It isn't working but I have no clue if there is something different about using view functions from other apps or if I am just doing something silly.
call/template/call/file.html
<form action="{% url 'upload_image' %}"></form>
picture/urls.py
from .views import PictureList, PictureCreate, PictureDetail, PictureUpdate, PictureDelete, upload_image
...
url(r'^upload_image/$', upload_image, name='upload_image'),
...
picture/view.py
def upload_image( request ):
print 'IN IMAGE UPLOAD'
print request
All I ever get is:
NoReverseMatch at /call/4/
Reverse for 'upload_image' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
When calling reverse() on an URL that comes from a different application, you should use its "namespaced" version, like so:
{% url 'app_name:app_url' %}
In your specific case, that translates to:
{% url 'picture:upload_image' %}

Django alter current URL name and arguments

In my Django URLs, I have many URL patterns that end with :
(redirect/(?P<redirect_to>\w+))
Which means that these URLs can be (or not) ending by /redirect/TARGET/. These URL patterns have other named arguments (mostly one : pk)
Now, I'd like, in the templates used by these URL patterns, to be able to alter the current page path, by just adding the redirect_to argument, and keeping the other arguments and URL reverse name untouched.
I was able to get the URL reverse name in the template, by adding resolve(path).url_name to the current context, and then to use that with the {% url %} template tag.
I'd like to know if there is any easy way to dynamically add the arguments (from resolve(path).kwargs) to the URL reverse tag ?
I think you should create a custom tag for this (replacing your {% url %} tag with {% url_redirect "your_new_destination" %}).
in your_app/templatetags/my_custom_tags.py:
from django.core.urlresolvers import reverse, resolve
#register.simple_tag(takes_context=True)
def url_redirect(context, new_destination):
match = resolve(context.request.path)
match.kwargs['redirect_to'] = new_destination
return reverse(match.url_name, args=match.args, kwargs=match.kwargs)
in your template:
{% load my_custom_tags %}
{% url_redirect "your_new_destination" %}
Please note that you need to add 'django.core.context_processors.request' to your TEMPLATE_CONTEXT_PROCESSORS in order for this snippet to work.

Django url template tag {% url %} error on django 1.4

I made this many times and it worked, but not this time.
I get this error when I try to use {% url path.to.view %} django's template tage:
AttributeError at /login/ 'str' object has no attribute 'regex'
urls.py (main)
urlpatterns= patterns('', (r'', include('authenticate.urls')), )
urls.py (my app)
urlpatterns= patterns('authenticate.views', url(r'^login/$','login'),)
login.html
{{ form }}
{% url authenticate.views.login %} < --- Error comes here
in the views:
return render_to_response('login.html',{'form':form},context_instance=RequestContext(request), )
Doesn't also work with:
{% url authenticate.views.login %}
{% url 'authenticate.views.login' %}
{% url "authenticate.views.login" %}
This is on django 1.4; what possibly I'm doing wrong, or what do I miss in that version of django?
Thanks in Advance!
Update:
I can also add that using reverse in my views doesn't work and gives me the same error above:
from django.core.urlresolvers import reverse
result = reverse('django.contrib.auth.views.password_reset')
HttpResponse(result)
Error:
AttributeError at /abc/ 'str' object has no attribute 'regex'
Check this
https://docs.djangoproject.com/en/1.4/topics/http/urls/
You can add a name to your URL to help the refer.
Also check how you are declaring the pattern. It should be done like this:
urlpatterns = patterns('',
url(r'^archive/(\d{4})/$', archive, name="full-archive"),
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, "arch-summary"),
)
I got it :))
The problem wasn't in the url template tag, it was in another urls file that's been included in the main one; the problem doesn't show till my app. hits url template tage or django's reverse method, otherwise, django doesn't complain about any url!!

Named route in Django cannot be reversed in template

I have a simple named route:
url(r'^$', 'appname.views.static.home', name='homepage'),
and am trying to use this in a view:
{% url 'homepage' %}
So far as I can tell, this is correct according to the docs on this - so why do I get a NoReverseMatch error?
Contrary to the example in the documentation, not using quotes in the url helper works:
{% url homepage %}

django {% url %} tag without parameters

I have a url defined as follows:
url(r'^details/(?P<id>\d+)$', DetailView.as_view(), name='detail_view'),
In my templates, I want to be able to get the following url: /details/ from the defined url.
I tried {% url detail_view %}, but I get an error since I am not specifying the id parameter.
I need the url without the ID because I will be appending it using JS.
How can I accomplish this?
Just add this line to your urls.py:
url(r'^details/$', DetailView.as_view(), name='detail_view'),
or:
url(r'^details/(?P<id>\d*)$', DetailView.as_view(), name='detail_view'),
(This is a cleaner solution - thanks to Thomas Orozco)
You'll need to specify that id is optional in your view function:
def view(request, id=None):