I'm getting an error:
GenerationException: url_for could not generate URL. Called with args: () {}
from this line of a mako template:
<p>Your url is ${h.url_for()}</p>
Over in my helpers.py, I do have:
from routes import url_for
Looking at the Routes-1.12.1-py2.6.egg/routes/util.py, I seem to go
wrong about line it calls _screenargs().
This is simple functionality from the Pylons book. What silly thing am I doing wrong? Was there a new url_current()? Where?
I didn't know url_for() (no arguments) was ever legal, but if it was and this is what you're referring to as "url_current", I believe the new approach is to use the url object, calling a method on it as url.current().
Related
I cannot for the life of me find a way to redirect to another view with arguments for that view in Django. I've tried the django.shortcuts redirect method, but that doesn't seem to work (it seems to want to take patterns for the url pattern and not the view function. The HttpResponseRedirect function obviously doesn't work because that function just redirects to another url. What other options are there?
Seems you want to give use redirect and want to give a parameter thats so easy you can do that
x.order_total= total+shipping_total
x.save()
return redirect(f'/order/payment/{order_number}')
here i have pasted just a snippet of my one view which is doing that you can insert the parameter using f outside the string if you have matching url it will work fine
You can redirect to other view with parameter(s) using reverse that doesn't use hard coded url instead the name of url infact redirect uses that internally
def view_one(request):
return redirect('view_two', arg=arg)
By default redirect will do temporary redirect for perminant redirect you have to specify it as to know the difference between the two take a look here
You can use the reverse function to fill your URL pattern
You can import the reverse function with the following line:
from django.core.urlresolvers import reverse
If you have URLs like this you can use reverse:
url(r'^project/(?P<project_id>\d+)/$','user_profile.views.EditProject',name='edit_project'),
path('project/<str:project_id>/', name='edit_project'),
usage:
redirect(reverse('edit_project', kwargs={'project_id':4}))
or:
redirect(reverse('app_name:edit_project', kwargs={'project_id':4}))
Doc here
I'm encountering an odd problem with namespaced url in Django, and I cannot find what I am doing wrong while it is working on simpler examples and using the
Basically, my project is made of two apps, user and model. In my general urls.py, I defined :
url(r'^model/', include('model.urls', namespace="model")),
url(r'^user/', include('user.urls', namespace="user")),
In the user.urls.py file, I defined the following url:
url(r'^duo/(?P<pseudo>[a-z]+)/$',views.duo, name='duo'),
The duo view is rather simple :
def duo(request,pseudo):
print pseudo
return render(request,"user/duo.html",locals())
Therefore, when I use in my templates:
{% url 'user:duo' lena %}
I expect the url to be resolved as /user/duo/lena. Instead, I got the following NoReverseMatch:
Reverse for 'duo' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'user/duo/(?P<pseudo>[a-z]+)/$']
I take any guess to fix my mistake !
Ok, Pynchia and Bernhard comments help me to fix my bug ! Now, I am using
"{% url 'user:duo' "lena" %}"
in my template and it's running smoothly ! Many thanks to you !
Edited according to the dicussion:
You need to quote your paramater. Also keyword parameters might make things clearer:
{% url 'user:duo' pseudo='lena' %}
If you don't quote the value, Django assumes this is a context variable and as lena is not a context variable in your case this evaluates to None. The error actually tells you, you didn't provide args or kwargs for the reverse lookup.
I know jinja2 is the default built-in template engine for Flask. My question is:
Is it possible to integreate Chameleon with Flask ?
If so, how can I do that ?
This is how I did. I wrap the response string in a method..
from chameleon import PageTemplate
def execute_page(template_str, page):
# Irrelevant
template = PageTemplate(template_str)
return template.render(page=page)
then consume the method and return the HTTPResponse. Hence, I completely skip render_template(template_file_name) approach.
My question is: Is it the proper (Pythonic, Flask) way to do?
I want to use the {% url %} tag to get the url of a named view that takes a parameter.
I want to get the URL without the parameter and pass the parameter later using javascript. So I don't know the value of the parameter when the template renders ! If I try the url without parameters then it says "reverse not found"... I know I can hard code the url in my javcascript code but that's not DRY !!!
Thanks !
When I've had to do this in the past, I've used a sentinel value for the parameters, ie one that I know will never be passed for real, and used a regex in Javascript to replace it with the real ones.
{% url my_view "anobviouslyfakeparameter1" "anotherparamthatwillneverhappenforreal" %}
You can try the following:
url(r'^test/(\d+)/$', 'test_view', name='test-view'),
url(r'^test/$', 'test_view', name='test-view'),
And return a 404 if the parameter is not provided (in case someone would actually access this uri).
In my urls.py file, I have:
from myapp import views
...
(r'^categories/$', views.categories)
Where categories is a view function inside myapp/views.py. No other URLconf lines reference views.categories.
In a unit test file, I’m trying to grab this URL using django.core.urlresolvers.reverse(), instead of just copying '/categories/' (DRY and all that). So, I have:
from django.core.urlresolvers import reverse
from myapp import views
...
url = reverse(views.categories)
When I run my tests, I get a NoReverseMatch error:
NoReverseMatch: Reverse for '<function categories at 0x1082f30>' with arguments '()' and keyword arguments '{}' not found.
It matches just fine if I make the URL pattern a named pattern, like this:
url(r'^categories/$', views.categories, 'myapp-categories')
And use the pattern name to match it:
url = reverse('myapp-categories')
But as far as I can tell from the reverse documentation, I shouldn’t need to make it a named URL pattern just to use reverse.
Any ideas what I’m doing wrong?
Jack M.'s example is nearly correct.
It needs to be a url function, not a tuple, if you want to use named urls.
url(r'^no_monkeys/$', 'views.noMonkeys', {}, "no-monkeys"),
After futher investigation, turns out it was an issue with how I was importing the views module:
How do I successfully pass a function reference to Django’s reverse() function?
Thanks for the help though, guys: you inspired me to look at it properly.
This does work, and all the code that you've pasted is correct and works fine (I just copied it into a clean test/project app and it reversed the URL without any problem). So there's something else going on here that you haven't showed us. Simplify down to the bare-bones basics until it works, then start adding complexity back in and see where it's breaking.
Also, you can do "./manage.py shell" and then interactively import the reverse function and your view function and try the reverse. That'll remove the test setup as a possible cause.
The reverse function actually uses the "name" of the URL. This is defined like so:
urlpatterns = patterns('',
(r'^no_monkeys/$', 'views.noMonkeys', {}, "no-monkeys"),
(r'^admin/(.*)', admin.site.root),
)
Now you would call reverse with the string "no-monkeys" to get the correct url.
Ninja Edit: Here is a link to the django docs on the subject.