I need to encode an URL created by the {% url %} template tag in order to pass it as an argument in an iframe src which generates a Facebook Like button.
What's an appropriate way to do this? The urlencode template filter doesn't seem to work here. My template code looks like this:
{% url blog_urls/blog_detail slug=p.slug year=p.published_on.year month=p.published_on.month day=p.published_on.day %}
The url tag takes another argument which allows you to create a variable with the value of the url:
{% url foo bar=baz as my_url %}
{{ my_url|filters }}}
Additionally, you can always use the filter tag itself to apply filters to more complex tags, eg:
{% filter urlencode %}{% url blog_urls/blog_detail slug=p.slug year=p.published_on.year month=p.published_on.month day=p.published_on.day %}{% endfilter %}
See https://docs.djangoproject.com/en/dev/ref/templates/builtins/#filter
Related
How do i pass a template variable to djangos url resolver as a parameter?
Something along these lines:
{% for group in groups %}
{{ group.name }}
MEMBER
<br>
{% endfor %}
What makes you think you need variable tags inside another tag? You don't: as the documentation clearly shows, once you're inside a tag, you have direct access to the context variables.
{% url 'group-view' group_code=group.group_code %}
I have this in urls.py:
urlpatterns = patterns('',
url(r'^add_to_cart/(?P<app_label>\w+)/(?P<model_name>\w+)/(?P<obj_id>\d+)/$', AddToCart.as_view(), name='add-to-cart'),
)
and i am using this to call AddToCart view in template:
{% for eg in eyeglasses %}
<p>{{eg}} <a href="{% url 'add-to-cart' eg|app_label eg|class_name eg.pk %}" >Buy</a> </p>
{% endfor %}
This ends up in having a url like this
"127.0.0.1/cart/add_to_cart/product/Sunglass/2/"
which i want to avoid. Is there any different way to pass these variables but without passing them as url parameters?
You can try passing them as querystring parameters instead of in url, so you can build url as
http://127.0.0.1/cart/add_to_cart?app_label=product&product=Sunglass&id=2
Build this in template as
{% for eg in eyeglasses %}
<p>{{eg}} <a href="{% url 'add-to-cart' %}?app_label={{eg.app_label}}&product={{eg.class_name}}&id={{eg.pk}} %}" >Buy</a> </p>
{% endfor %}
In view you can get it as
def add_cart_view(request):
....
product_name = request.GET.get('product')
...
Rather than having a list of links, create a form where you use buttons of type submit. For each button give it a value that you can retrieve from the request. When you submit the form set the method to post rather than get.
You may want to take a look part 4 of the Django tutorial.
I'm new to Django and puzzled. Using Django 1.4. Inside one of my templates, this code works:
{% for element0, element1 in menu.elements %}
<li class='menu_{{ name }}'>{{ element0 }}</li>
{% endfor %}
... but this code throws a "NoReverseMatch" error:
{% for element0, element1 in menu.elements %}
<li class='menu_{{ name }}'>{{ element0 }}</li>
{% endfor %}
... despite the fact that the "element1" variable holds 'users.views.home'. I'm thinking/hoping that the solution to this is really simple... that I've missed something obvious about variable handling inside Django templates?
I've consulted the documentation for the url built-in function to no avail. Any help will be appreciated.
I think you need to add this to your template:
{% load url from future %}
and change the first call to
{% url 'users.views.home' %}
see the forwards compatibility note in the docs you linked to
That's bad idea to write like this {% url 'users.views.home' %}, better use named url - {% url 'users_home' %}, it will be easy to maintain in the future. For example if you decide to move your def home(request) from users.views to account.views you will need to replace all urls occurrences in all your templates. But if you use named urls, you just need to change urls.py
I have url config based on https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.views.logout
Here it is:
url(r'^logout(?P<next_page>.*)$', 'logout', name='auth_logout_next'),
In template I use such code:
{% trans "Logout" %}
It works nice, yet I have possible GET value in some pages - ?page=2, so request.path drops those values. How should I pass not only the existing page but also GET values if possible.
{% trans "Logout" %}
<a href={% url home %}></a>
I can't find {% url %} in the Django API.
url
Returns an absolute URL (i.e., a URL
without the domain name) matching a
given view function and optional
parameters. This is a way to output
links without violating the DRY
principle by having to hard-code URLs
in your templates:
{% url path.to.some_view arg1,arg2,name1=value1 %}