Django ChoiceField

后端 未结 4 1302
醉话见心
醉话见心 2020-12-02 10:42

I\'m trying to solve following issue:

I have a web page that can see only moderators. Fields displayed on this page (after user have registered):
Username, Firs

4条回答
  •  感情败类
    2020-12-02 11:22

    First I recommend you as @ChrisHuang-Leaver suggested to define a new file with all the choices you need it there, like choices.py:

    STATUS_CHOICES = (
        (1, _("Not relevant")),
        (2, _("Review")),
        (3, _("Maybe relevant")),
        (4, _("Relevant")),
        (5, _("Leading candidate"))
    )
    RELEVANCE_CHOICES = (
        (1, _("Unread")),
        (2, _("Read"))
    )
    

    Now you need to import them on the models, so the code is easy to understand like this(models.py):

    from myApp.choices import * 
    
    class Profile(models.Model):
        user = models.OneToOneField(User)    
        status = models.IntegerField(choices=STATUS_CHOICES, default=1)   
        relevance = models.IntegerField(choices=RELEVANCE_CHOICES, default=1)
    

    And you have to import the choices in the forms.py too:

    forms.py:

    from myApp.choices import * 
    
    class CViewerForm(forms.Form):
    
        status = forms.ChoiceField(choices = STATUS_CHOICES, label="", initial='', widget=forms.Select(), required=True)
        relevance = forms.ChoiceField(choices = RELEVANCE_CHOICES, required=True)
    

    Anyway you have an issue with your template, because you're not using any {{form.field}}, you generate a table but there is no inputs only hidden_fields.

    When the user is staff you should generate as many input fields as users you can manage. I think django form is not the best solution for your situation.

    I think it will be better for you to use html form, so you can generate as many inputs using the boucle: {% for user in users_list %} and you generate input with an ID related to the user, and you can manage all of them in the view.

提交回复
热议问题