简体   繁体   中英

Using the Django url template tag between apps

I am using namespaced URLs in Django 1.8 with two apps. See minimalistic structure below:

Cart
- templates
- -cart
- - -cart-template.html
- urls.py
- views.py
Shop
- settings.py
- urls.py
Product
- templates
- -cart
- - -cart-template.html
- urls.py
- views.py

So I set up my routes, for the 'main' app:

#Shop/urls.py
urlpatterns = patterns(
    '',
    url(r'^cart/', include('cart.urls', namespace='cart')),
)

and the Cart app

#Cart/urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
]

This template tag works perfectly:

# Cart/templates/cart/cart-template.html
<form action= {% url 'cart:index' %} method="post">

However, when I am on the page of a product (handled by Product app) and click a button which should redirect me in the exact same way, it gives a 404:

# Product/templates/product/product-template.html
<form action= {% url 'cart:index' %} method="post">

In other words, I cannot use the namespaced url of the Cart app, in the template of another app, Product.How can I make this possible/what am I doing wrong? This is my output:

Page not found (404)
Request Method:     GET
Request URL:    http://0.0.0.0:8000/cart/cart.views.index

It seems to try to use the view that should be called, as a URL.

EDIT: It seems that the error occurs when I add a name attribute in a hidden field:

<!-- Product/templates/product/product-template.html -->

<form action= {% url 'cart:index' %} method="post">  
            {% csrf_token %}  
            <!-- this link works -->  
            <a href={% url 'cart:index'%}> Click</a>  
            <!-- Submit button goes to correct URL when name attribute of hidden field below is commented out, but I need it to know what to put in cart -->
            <input type="hidden" name="id" value="{{ article.id }}"> 
            <input type="submit" value="Bestellen" class="btn btn-default"/>
        </form>

When the name attribute of the hidden input field is removed I get the following error:

MultiValueDictKeyError at /cart/  

"'id'"

Thank you for your effort. I found the error. In the index view of cart I used to following line:

return redirect(cart.views.index)

Which I used to redirect the user if the cart was modified(article deleted, or added in quantity etc.) The redirect function uses the reverse() function internally so the URL matching that view would be resolved. See: https://docs.djangoproject.com/en/1.8/ref/urlresolvers/#reverse

With named views, the redirect function cannot use the reverse() function, thus it cannot resolve cart.views.index as input and just redirects to it without resolving.

I fixed it by reversing the named route to get the url and redirecting the user like so:

return redirect(reverse('cart:index'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM