【发布时间】:2018-12-12 20:47:32
【问题描述】:
我想通过单选按钮获取选定的值并将其传递给更新视图,但我找不到方法。两种视图都是基于类的。
异常值:
未找到任何参数的“更新作者”的反向。尝试了 1 种模式:
列表视图:
class Dashboard(ListView):
model = Author
template_name='catalog/dashboard.html'
def get_context_data(self, **kwargs):
context = super(EditDashboard, self).get_context_data(**kwargs)
context["authors"] =Author.objects.all()
context["publishers"] =Publisher.objects.all()
context["genres"] =Genre.objects.all()
return context
更新视图:
class UpdateAuthor(UpdateView):
model = Author
fields = '__all__'
template_name='catalog/updateauthor.html'
context_object_name = 'author'
型号:
class Author(models.Model):
first_name = models.CharField(max_length=200, blank=False)
last_name = models.CharField(max_length=200, blank=False)
date_of_birth = models.DateField(null=True, blank =True)
date_of_death = models.DateField(null=True, blank =True)
class Meta:
ordering = ['first_name', 'last_name']
def __str__(self):
return f' {self.first_name} {self.last_name}'
模板中的表格:
<form action="{% url 'update-author' ????? %}" method="post">
{% csrf_token %}
{% for author in authors %}
<input type="radio" name="choice" id="{{ author.id }}" value="{{ author.id }}">
<label for="author">{{ author.first_name}} {{ author.last_name}}</label><br>
{% endfor %}
<input type="submit" value="Update">
</form>
【问题讨论】:
标签: radio-button django-class-based-views django-2.0