【问题标题】:How to add category to my polls app?如何将类别添加到我的投票应用程序?
【发布时间】:2018-02-13 19:26:20
【问题描述】:

我的 models.py 是

class Question(models.Model):
    question_text = models.CharField(max_length= 100)
    pub_date = models.DateTimeField('Date Is Published')
    image = models.ImageField(upload_to="Question_Image", blank=True)

    def __str__(self):
        return self.question_text

而且,我必须添加一个类“类别”,以便我的每个问题都可以选择以下四个类别之一:世界、技术、屏幕和运动。例如,我拥有的一个类别 html 是 screen.html 和我只需要显示基于类别“屏幕”的问题。这是我的 screen.html。

{% extends 'polls/base.html' %}
{% block main_content %}
{% if latest_questions %}
<body>
    <ul style="list-style: none;">
        {% for question in latest_questions %}
        <li style="float:center">
            <div id="tabs"><a id="tabsword" href={% url 'polls:detail' question.id %}>
                <img id="tabsimage" align="middle" src='{{question.image.url}}'/>
                <p style=" font-size: 25px; font-family: Verdana; color: whitesmoke; text-align: center"><b>{{question.question_text}}</b></p>
            </a>
            </div>
        </li>
        {% endfor %}
    </ul>
{% else %}
    <p> You have no questions. Please add some.</p>
{% endif %}
{% endblock %}

帮助我以某种方式修改代码,以便我可以添加类类别,将每个问题从四个类别中分配出来,并仅根据上述 html 上的类别“屏幕”显示问题。

【问题讨论】:

    标签: django django-models django-templates django-views


    【解决方案1】:

    如果您不想为类别创建另一个模型,一种简单的方法是执行以下操作(来自Django documentation 字段选项):

    from django.db import models
    
    class Person(models.Model):
        SHIRT_SIZES = (
            ('S', 'Small'),
            ('M', 'Medium'),
            ('L', 'Large'),
        )
        name = models.CharField(max_length=60)
        shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)
    

    只需将衬衫尺码的名称替换为您想要的型号字段和选择即可。

    【讨论】:

      【解决方案2】:

      没有办法做到这一点。 例如在你的模型中定义选择

      class Question(models.Model):
          CATEGORY_SIZES = (
              ('W', 'WORlD'),
              ('T', 'TECHNOLOGY'),
              ('S', 'SCREEN'),
          )
          question_text = models.CharField(max_length= 100)
          pub_date = models.DateTimeField('Date Is Published')
          image = models.ImageField(upload_to="Question_Image", blank=True)
          category = models.CharField(max_length=1, choices=CATEGORY_SIZES)
      
          def __str__(self):
              return self.question_text
      

      在你的意见中.py

      def your_view(request):
          questions = Question.objects.filter(category='S')
          context = {'questions': questions}
          return render(request, your_template ,context)
      

      其他方式:如果您认为单独的模型将是您更好的选择

      class Categories(models.Model):
          name = models.CharField(max_length= 100)
      
          def __str__(self):
              return self.name
      
      
      class Question(models.Model):
          question_text = models.CharField(max_length= 100)
          pub_date = models.DateTimeField('Date Is Published')
          image = models.ImageField(upload_to="Question_Image", blank=True)
          category = models.ForeignKey(Categories)
      
          def __str__(self):
              return self.question_text
      

      在views.py中

      def your_view(request):
          category = Category.objects.get(name='Screen')
          questions = category.question_set.all()
          context = {'questions': questions}
          return render(request, your_template,context)
      

      【讨论】:

        猜你喜欢
        • 2017-06-14
        • 2011-05-19
        • 1970-01-01
        • 2019-03-23
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多