英文:
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> ·
    </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变量来传递类别列表。
相关资料:
- Django官方文档:https://docs.djangoproject.com/
 - Django URL Dispatcher文档:https://docs.djangoproject.com/en/3.2/topics/http/urls/
 - Django模板语言文档:https://docs.djangoproject.com/en/3.2/topics/templates/
 - 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("home")
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_list %}
  <div class="card">
    <div class="card-header">
      <span class="font-weight-bold"><a href="">{{ 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' %} 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 'category' cats=category.name %}
Also you can use as:
{% url 'category' %}?cats={{ category.name }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论