使用 createview 在模板中将多对多字段显示为复选框
Showing many to many field as checkbox in template with createview
查看:
class addrecipe(CreateView):
model = Recipe
template_name = 'recipebook/addrecipe.html'
fields = '__all__'
型号:
class Recipe(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=200)
servings = models.IntegerField(default=1, blank=False)
tools = models.ManyToManyField(Tool)
def __str__(self):
return self.name
模板:
{% extends 'recipeBook/base.html' %}
{% block title %}Add New Recipe{% endblock title %}
{% block header %}
<header>
<a href="{% url 'recipeBook:recipe_list' %}">Recipe list</a>
</header>
{% endblock header %}
{% block content %}
<h1>Add New Recipe</h1>
<form action="" method ="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Add">
</form>
{% endblock content %}
表格:
from django import forms
from .models import Recipe, Tool
class AddRecipeForm(forms.ModelForm):
name = forms.CharField(label="Recipe Name", max_length=50)
description = forms.Textarea(max_length=200)
servings = forms.IntegerField(default=1, blank=False)
tools = forms.ModelMultipleChoiceField(queryset=Tool.objects.all(), widget=forms.CheckboxSelectMultiple, required = True)
class Meta:
model = Recipe
fields = ("__all__",)
目前工具栏显示如下:
我正在尝试找到一种方法将数据库中的所有工具显示为清单,用户可以在其中简单地勾选与他们正在添加的配方相关的每个工具,或者添加一个新工具,如果它不是已经在列表中。有办法实现吗?
我需要将表单 class 添加到我的视图中,如下所示:
class addrecipe(FormView):
form_class = AddRecipeForm
model = Recipe
template_name = 'recipebook/addrecipe.html'
fields = '__all__'
extra_context = {
'recipe_list': Recipe.objects.all()
}
查看:
class addrecipe(CreateView):
model = Recipe
template_name = 'recipebook/addrecipe.html'
fields = '__all__'
型号:
class Recipe(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=200)
servings = models.IntegerField(default=1, blank=False)
tools = models.ManyToManyField(Tool)
def __str__(self):
return self.name
模板:
{% extends 'recipeBook/base.html' %}
{% block title %}Add New Recipe{% endblock title %}
{% block header %}
<header>
<a href="{% url 'recipeBook:recipe_list' %}">Recipe list</a>
</header>
{% endblock header %}
{% block content %}
<h1>Add New Recipe</h1>
<form action="" method ="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Add">
</form>
{% endblock content %}
表格:
from django import forms
from .models import Recipe, Tool
class AddRecipeForm(forms.ModelForm):
name = forms.CharField(label="Recipe Name", max_length=50)
description = forms.Textarea(max_length=200)
servings = forms.IntegerField(default=1, blank=False)
tools = forms.ModelMultipleChoiceField(queryset=Tool.objects.all(), widget=forms.CheckboxSelectMultiple, required = True)
class Meta:
model = Recipe
fields = ("__all__",)
目前工具栏显示如下:
我正在尝试找到一种方法将数据库中的所有工具显示为清单,用户可以在其中简单地勾选与他们正在添加的配方相关的每个工具,或者添加一个新工具,如果它不是已经在列表中。有办法实现吗?
我需要将表单 class 添加到我的视图中,如下所示:
class addrecipe(FormView):
form_class = AddRecipeForm
model = Recipe
template_name = 'recipebook/addrecipe.html'
fields = '__all__'
extra_context = {
'recipe_list': Recipe.objects.all()
}