Search code examples
djangopython-2.7django-templateshrefdjango-urls

Django urls accepting an object or Dictionary


As i am new to django i am not aware of the advanced django url concept. So i would like to know whether the following are possible or not in django urls.

My first Question is,

In my django template i am calling this url,

<a href="/product/mycart/{{val.price}}">

I used the below url for the above href

url(r'^mycart/(?P<val.price>\d+)/$', 'cartList')

But it shows the below error,

error at /product/list/

bad character in group name

My second question is in my django template i have the following

val.price=some value

val.id=some value

val.hieght=some value

now iam calling this below url

<a class="add_to_cart" href="/product/mycart/{{val}}" title="Product Cart">Add To Cart</a>

i used the below url for the above href

url(r'^mycart/(?P<val>\d+)/$', 'cartList')

but it shows the below error

Page not found (404)

please help me out these problems.....


Solution

  • for your first question

    You better name your url:

    url(r'^mycart/(?P<val.price>\d+)/$', 'cartList', name="cart_list)
    

    Then in your template you could use

    <a href="{% url cart_list val.price %}">
    


    For your second question

    both of the urls look the same to me and my guess is that django can't see the difference between the to. Try using different urls