<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 %}
Related
here is my path
path('<slug:category_slug>/<slug:city_slug>/<slug:slug_text>/', views.category_view_detail,
name='post_detail'),
how to write URL for this example {% url 'post_delete' posts.slug %} but i want to add all three slug inside the URL how to do something like this...
({% url 'post_detail' posts.category posts.city posts.slug %})
but this not work..
I think it is not working because you are referring to a different URL pattern name
You are using post_delete the one in the code is post_detail
{% url 'post_detail' posts.slug posts.city posts.slug %}
# or
{% url 'post_detail' category_slug=posts.category city_slug=posts.city slug_text=posts.slug %}
The approach of doing {% url 'view_name' object.pk object.parent.slug %} isn't really flexible when switching to completety different url patterns. I'm looking for a way to do {% url 'view_name' object %} and to transcribe myself from object to object.pk and object.parent.slug in the url.
Like that
- template.html
{% url 'view_name' object %}
- urls.py [not real regex regex]
url('<object__parent__slug>/<object__pk>', views.view_name, name="view_name")
I know this is not at all possible with this syntax, but it's just to give an idea of what I'm looking for.
I will just add url methods inside my models:
class House(model.Models):
name = models.TextField()
....
def get_edit_url(self):
return reverse('view_name', {'pk':self.pk, 'name':self.name, 'owner_pk' : self.owner.pk })
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.
Hello I am trying to pass different urls from my site into a template.
I thought it would be done by using this in the template
{% url myappname.module.views.urlfunction %}
but that returns an error, saying that "Caught AttributeError while rendering: 'str' object has no attribute 'regex'"
I am not sure what that means. I followed this example url template tag in django template
urlpatterns = patterns('',
url(r'^$', 'myappname.module.views.start'),
and this is what I have entered in my template
{% url myappname.module.views.start %}
I have also tried this url pattern and template combo to no avial:
urlpatterns = patterns('',
url(r'^$', 'myappname.module.views.start', name="home"),
{% url "home" %}
What am I doing wrong?
Thanks.
In last example instead of
{% url "home" %}
try
{% url home %}
without quotes
add the following line in the top of the template:
{% load url from future %}
It will support the pattern
{% url "VIEW_NAME" args... %}
In all seriousness, I would consider not using the {% url %} scheme in django for simple cases. They are designed to provide some sort of flexibility that you will likely never take advantage of, and they are almost guaranteed to waste hours of your time trying to work out how to do it 'correctly'.
Just use action="/login/" or action ="/home/" and spend the hours you saved doing useful things, like writing code.
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