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 %}
Related
I have three pages - homepage, http://127.0.0.1:8000/, displaying one paragraph sentence and two links in the header. and list of pizzas, http://127.0.0.1:8000/pizzas . Now i was trying to add links for each pizza on http://127.0.0.1:8000/pizzas page, so that one could click on them and see what toppings were available. I'm probably stuck because of my decision to use paths instead of url() for mapping urls, which the book i'm following uses.
Error : NoReverseMatch at /pizzas.
Reverse for 'pizza_w_toppings' not found. 'pizza_w_toppings' is not a valid view function or pattern name.
pizzas.html -
{% extends "pizzeria_app/base.html" %}
{% block content %}
<h1> Available Pizzas : </h1>
<ul>
{% for pizza in pizzas %}
<li> <a href = {% url 'pizza_w_toppings' %}> {{pizza}}</a><li>
{% empty %}
<p> We're outta Pizzas. next time bro! <p>
{% endfor %}
</ul>
{% endblock content %}
app/urls.py :
urlpatterns = [
#homepage
path('', views.index),
#show available pizzas
path('pizzas', views.pizzas),
path('pizzas/<int:pizza_id>', views.pizza_w_toppings, name="pizza_w_toppings")
Views:
I'm new to StackOverflow and can't figure out how to add my views.py. i attached a picture, sorry
views.py screenshot
Your url tag should be {% url 'pizza_w_toppings' pizza.id %}. If you check the documentation, you'll see all possible variations of url tag.
For example, suppose you have a view, app_views.client, whose URLconf
takes a client ID (here, client() is a method inside the views file
app_views.py). The URLconf line might look like this:
path('client/<int:id>/', app_views.client, name='app-views-client')
If this app’s URLconf is included into the project’s URLconf under a
path such as this:
path('clients/', include('project_name.app_name.urls'))
…then, in a template, you can create a link to this view like this:
{% url 'app-views-client' client.id %}
The template tag will output the string /clients/client/123/.
If you use namespaces, make sure to include namespace in your url tags like this:
{% url 'your-namespace:app-views-client' client.id %}
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.
<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 %}