我正在学习django。呈现提供程序页面时出现上述错误,如果我删除了html代码,则该页面将加载而没有错误,但是使用以下代码,它将给出上述错误
预先感谢
html:
[ { "_id" : 11 }, { "_id" : 34 }, ... ]
视图:
<ul class="all-cat-list">
{% for category in category_list %}
<li><a href="{% url 'providers: provider_list_category' category.slug %}">{{category}}
<span class="num-of-ads">({{category.total_providers}})</span></a></li>
{% endfor%}
</ul>
</div>
<ol class="breadcrumb" style="margin-bottom: 5px;">
<li><a href="/">Home</a></li>
<li class="active"><a active href="{% url 'providers :provider_list' %}> All Categories </a> </li>
{% if category %}
<li class="active">{{category}} </li>
{% endif%}
</ol>
<div class="ads-grid">
<div class="side-bar col-md-3">
<div class="search-hotel">
<h3 class="sear-head">Search</h3>
<form method="GET" action="{url 'providers : provider_list' %}">
<input type="text" value="Product name..." name ="q" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Product name...';}" required="">
<input type="submit" value=" ">
</form>
</div>
网址:
def providerlist(request,category_slug = None):
category = None
providerlist = Provider.objects.all()
categorylist = Category.objects.annotate(total_providers=Count('provider'))
if category_slug:
category= get_object_or_404(Category, slug = category_slug)
providerlist = Category.filter(category=category)
search_query = request.GET.get('q')
if search_query :
providerlist = providerlist.filter(
Q(name__icontains=search_query) |
Q(description__icontains=search_query)|
Q(category__category__name__icontains = search_query)
)
template = 'provider/provider_list.html'
context = {'provider_list': providerlist ,'category_list' : categorylist}
return render(request,template,context)
def providerdetail(request,provider_slug):
#print(provider_slug)
providerdetail=get_object_or_404(Provider,slug=provider_slug)
providerimage = ProviderImages.objects.filter(provider = providerdetail)
template ='provider/provider_detail.html'
context = {'provider_detail': providerdetail, 'productimage' : providerimage}
return render(request, template, context)
答案 0 :(得分:1)
根据您在urls.py中的应用名称:
app_name = 'provider'
您必须更改所有url模板标记,并从提供程序的末尾删除s。它必须与您的app_name完全相同。如下所示:
<li><a href="{% url 'provider: provider_list_category' category.slug %}">
您还可以将应用名称更改为:
app_name = 'providers'
答案 1 :(得分:0)
您的html文件应为<a href="{% url 'provider: provider_list_category' category.slug %}">
,并确保您project_name文件夹中的urls.py具有
urlpatterns = [ path('provider', include('provider.urls', namespace='provider')),
答案 2 :(得分:0)
您的app_name provider
并将名称放在html页面providers
中,您更改了app_name='providers'
或在html页面中更改了
<li class="active"><a active href="{% url 'provider:provider_list' %}"> All Categories </a> </li>
和放置内容名称空间的主urls.py,其中包含您的应用程序网址
urlpatterns = [path('provider', include('provider.urls', namespace='provider'))],
如果您更改app_name ='providers'然后更改名称空间
urlpatterns = [path('provider', include('provider.urls', namespace='providers'))],