{%url'something:somthing'%}有问题

时间:2019-07-06 08:02:14

标签: django django-templates

我想使该网址成为{%url'namespace:name'%},但它不起作用!我按照文档进行了所有操作,但所有尝试都是徒劳的。

我的CartDetail.html

..........
        </tr>
        </tbody>
    </table>
    <p class="text-right">
    <button href="{% url 'shop:ProductList'%}" class="btn btn-info">Продолжить Шопинг</button>
    <button href="#" class="btn btn-danger">Оформить заказ</button>
  </p>
</div>
{% endblock %}

我的商店/urls.py

from django.contrib import admin
from django.urls import path,re_path
from . import views

urlpatterns = [

    re_path(r'^(?P<category_slug>[-\w]+)/$', views.ProductList, name='ProductListByCategory'),
    re_path(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$', views.ProductDetail, name='ProductDetail'),
    re_path(r'^$', views.ProductList, name='ProductList'),

]


还有我的主要url.py


from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static




urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^cart/', include(('cart.urls', 'cart'), namespace='cart')),
    path('',  include(('shop.urls', 'shop'), namespace='shop')),
 ]



if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我该怎么办

2 个答案:

答案 0 :(得分:0)

在主urls.py

path('',  include('shop.urls', namespace='shop')),

shop/urls.py中添加

app_name = 'shop'

现在,您可以使用

"{% url 'shop:ProductList' %}" # spaces are necessary

答案 1 :(得分:0)

来自文档:

path('publisher-polls/', include('polls.urls', namespace='publisher-polls'))

来自您的代码:

path('',  include(('shop.urls', 'shop'), namespace='shop'))