如何在我的category_list.html中传递{% url ‘category’ %}?

huangapple go评论66阅读模式
英文:

How to pass {% url 'category' %} in my category_list.html?

问题

I have a detail view for categories and if I pass it in the URL directly, it works just fine. But if I want to pass it as a link in my template, it gets an error. Here is my models:

class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
         return reverse("category", args=[str(self.name).replace(' ', '-')])

Here is my views:

class CategoryClassView(TemplateView):
    model = Category
    template_name = "categories.html"
    
    def get_context_data(self, **kwargs):
        cats = kwargs.get("cats")
        category_posts = Category.objects.filter(name=cats.replace('-', ' '))
        context = {'cats': cats.replace('-', ' ').title(), 'category_posts': category_posts}
        return context

Here is my URLS:

path('category/<str:cats>/', CategoryClassView.as_view(), name='category')

Here is my template:

{% extends "base.html" %}

{% block content %}
{% for category in category_posts %}
  <div class="card">
    <div class="card-header">
      <span class="font-weight-bold"><a href="{{ category.get_absolute_url }}">{{ category.name }}</a></span> &middot;
    </div>
  </div>
{% endfor %}
{% endblock content %}

Could you write a more detailed explanation for your answer? Why is just passing {% url 'category' %} not working? A link to a list of useful materials would also be acceptable for better understanding.

你提供了一段关于类别(Category)的代码,主要涉及模型(models)、视图(views)、URL配置(URLs)以及模板(template)。你想知道为什么在模板中直接使用{% url 'category' %}无法正常工作,并且希望获得更详细的解释以及可能有助于更好理解的相关资料链接。

解释:

在你的模板中,你使用了{% url 'category' %}来创建链接到CategoryClassView视图的URL。然而,在你的Category模型中,get_absolute_url方法返回了一个与视图URL不匹配的URL。具体来说,它返回了一个带有空格的URL片段,而视图中的URL配置需要带有短横线而非空格。这导致了链接生成错误。

为了解决这个问题,你需要在get_absolute_url方法中返回正确格式的URL,即将空格替换为短横线。我在代码示例中已经做了相应修改。

另外,你可能需要确保在模板中使用{% for category in category_posts %}而不是{% for category in category_list %},因为你的视图中定义了category_posts变量来传递类别列表。

相关资料:

  1. Django官方文档:https://docs.djangoproject.com/
  2. Django URL Dispatcher文档:https://docs.djangoproject.com/en/3.2/topics/http/urls/
  3. Django模板语言文档:https://docs.djangoproject.com/en/3.2/topics/templates/
  4. Django视图文档:https://docs.djangoproject.com/en/3.2/topics/class-based-views/
英文:

I have a detail view for categories and if i pass in the URL directly it works just fine. But if i want to pass it as a link in my template it gets an error. Here is my models:

class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return (self.name)

    def get_absolute_url(self):
         return reverse(&quot;home&quot;)

Here is my views:

class CategoryClassView(TemplateView):
    model = Category
    template_name = &quot;categories.html&quot;
    
    def get_context_data(self, **kwargs):
        cats = kwargs.get(&quot;cats&quot;)
        category_posts = Category.objects.filter(name=cats.replace(&#39;-&#39;, &#39; &#39;))
        context = {&#39;cats&#39;:cats.replace(&#39;-&#39;, &#39; &#39;).title(), &#39;category_posts&#39;:category_posts}
        return context

Here is my URLS:

path(&#39;category/&lt;str:cats&gt;/&#39;, CategoryClassView.as_view(), name=&#39;category&#39;)

Here is my template:

{% extends &quot;base.html&quot; %}

{% block content %}
{% for category in category_list %}
  &lt;div class=&quot;card&quot;&gt;
    &lt;div class=&quot;card-header&quot;&gt;
      &lt;span class=&quot;font-weight-bold&quot;&gt;&lt;a href=&quot;&quot;&gt;{{ category.name}}&lt;/a&gt;&lt;/span&gt; &amp;middot;
    &lt;/div&gt;
  &lt;/div&gt;
{% endfor %}
{% endblock content %}

Could you write a more detailed explanation for your answer? Why is just passing {% url 'category' %} is not working? A link to a list of useful materials would be also acceptable for better understanding.

答案1

得分: 1

你应该这样做,因为你在URL中有参数:

{% url 'category' cats=category.name %}

另外你也可以这样使用:

{% url 'category' %}?cats={{ category.name }}
英文:

You should be doing this way, since you are having parameters in the url:

 {% url &#39;category&#39; cats=category.name %}

Also you can use as:

{% url &#39;category&#39; %}?cats={{ category.name }}

huangapple
  • 本文由 发表于 2023年1月9日 13:58:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定