我想通过单选按钮获取选定的值并将其传递给更新视图,但是我找不到办法。这两个视图都是基于类的。
异常值:
反向查找“ update-author”,未找到任何参数。尝试了1种模式:
ListView:
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
UpdateView:
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>
答案 0 :(得分:0)
因此,据我了解,您拥有一个包含所有作者发布者和体裁的listView,并且您希望在选定作者的情况下调用updateView。
一种方法是使用javascript:
以下是一些Jquery示例,您可以用来做到这一点:
$(document).ready(function(){
$('#myformid').submit(function(event) {
//prevent the form submition on click
event.preventDefault();
//get the selected value
var selected_author_id =$('input[name="choice"]:checked').val();
console.log(selected_author_id);
//add this value to our form action attribute
$(this).attr('action', 'update-author/'+String(selected_author_id));
//finaly submit the form
$(this).unbind('submit').submit();
});
});
别忘了导入jquery