简体   繁体   中英

django i can't see my form

I don't understand why I can't see my form.

I would like to create a form with a drop down menu with display_name of rider model as the choices.

my models.py:

class Rider(models.Model):
    first_name = models.CharField(max_length = 256,blank= True)
    last_name = models.CharField(max_length = 256,blank= True)
    display_name = models.CharField(max_length = 256,blank= True)
    birth = models.DateTimeField(default=timezone.now)
    place_of_birth = models.CharField(max_length = 256,blank= True)
    age = models.IntegerField(default = 0,blank= True)
    nationality = models.CharField(max_length = 256,blank= True)
    height = models.FloatField(default = 0.0 ,blank= True)
    weight = models.FloatField(default = 0.0 ,blank= True)
    team = models.CharField(max_length = 256, blank= True)
    team_code = models.CharField(max_length = 256, blank= True)
    cost = models.IntegerField(default = 0,blank= True)


    def __str__(self):
        return self.display_name

my form.py:

class ScoreForm(forms.ModelForm):
   riders =forms.ModelChoiceField(queryset=Rider.objects.all(),to_field_name="display_name")
   class Meta:
      model = Rider
      fields = ('display_name',)

my view.py:

class ScoreView(TemplateView):
    form_class = ScoreForm
    model=Rider
    template_name = 'blog/score.html'

my score.html:

{% extends 'blog/base.html'%}
{% block content %}
<h1>Score page</h1>
<form method="POST" >
    {% csrf_token %}
    {{form}}
    <button type="submit" class="save btn btn-default" name="button">Save</button>
</form>
{% endblock %}

Screenshot: 截图

To automatically include the form in the template context, subclass FormView instead of TemplateView . The form view will automatically create a form instance from form_class and insert it into the template context where you can render it by using the tag {{ form }} .

from django.views.generic.edit import FormView

class ScoreView(FormView):
    ...

https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-editing/#form-handling-with-class-based-views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM