如何在django中手动将表单字段附加到多个模型字段?

如何在django中手动将表单字段附加到多个模型字段?,django,django-forms,Django,Django Forms,这就是我的观点: def create_recipe(request): if request.method == 'POST': form = RecipeForm(request.POST) if form.is_valid(): current_user = request.user data = form.cleaned_data recipe_d

这就是我的观点:

def create_recipe(request):
    if request.method == 'POST':
           form = RecipeForm(request.POST)
           if form.is_valid():
               current_user = request.user
               data = form.cleaned_data
               recipe_data=Recipe.objects.create(user=current_user, recipebase_id=data['recipebase_id'], title=data['title'], instructions=data['instructions'])
               recipe_data.save()
               return HttpResponseRedirect(reverse('create_recipe'))
           else:
               messages.error(request, "Error")
    return render(request, 'create_recipe.html', {'form': RecipeForm })
这是我的表格:

  class RecipeForm(forms.ModelForm):

        class Meta:
            model = Recipe
            fields = ('recipebase_id', 'title

', 'instructions')
这是我的模型:

class Recipe(models.Model):

    user = models.ForeignKey('auth.User')
    recipebase_id = models.ManyToManyField(Recipebase)
    title = models.CharField(max_length=500)
    instructions = models.CharField(max_length=500)
    def __str__(self):
        return self.title
更新

<form action="/create_recipe/" method="post">
{% csrf_token  %}


     <div class="form-group">
          <label for="{{ form.recipebase_id.label }}" class="form-control" >
        <select id="id_{{ form.recipebase_id.name }}" name="{{ form.recipebase_id.name }}">
    {% for value, title in form.recipebase_id.field.choices %}

            <option value="{{ value }}">{{ title }}</option>

        {% endfor %}

          </select>
    </div>

<div class="form-group">
<label for="{{ form.title.label }}">{{ form.title.label }}:</label>
<input type="{{ form.title.type }}" name="{{ form.title.name }}" max_length="500" class="form-control" id="{{ form.title.id }}">
</div>

<div class="form-group">
<label for="{{ form.instructions.label }}">{{ form.instructions.label }}:</label>
<input type="{{ form.instructions.type }}" name="{{ form.instructions.name }}" max_length="500" class="form-control" id="{{ form.instructions.id }}">
</div>


     <input type="submit" value="submit">
</form>

{%csrf_令牌%}
{%表示值,标题格式为.recipebase_id.field.choices%}
{{title}}
{%endfor%}
{{form.title.label}}:
{{form.instructions.label}}:
当我提交表单时,它抛出recipebase_id是一个无效参数,如下所示: “recipebase_id”是此函数的无效关键字参数


这是可以理解的,因为它是一个多个字段,所以它不像普通的文本字段那样非常适合,但我真的不明白该怎么说。有什么建议吗?

在视图中,您不应该这样保存对象。你就这么做

obj=form.save()


在窗体上运行save方法可创建并保存该窗体中的对象。

在视图中,不应这样保存对象。你就这么做

obj=form.save()


在窗体上运行save方法可创建并保存该窗体中的对象。

在渲染时,可以使用django应用程序“django widget tweaks”将类添加到模板中的django窗体字段。 在django模板中使用它来添加类(例如引导类)

e、 下面的代码将表单控件类添加到输入html标记中

{%load widget\u tweaks%}
{%form.visible_fields%}
{{field.label}
{%render_field class=“form control”%}

{%endfor%}
您可以在渲染时使用django应用程序“django widget tweaks”将类添加到模板中的django表单字段。 在django模板中使用它来添加类(例如引导类)

e、 下面的代码将表单控件类添加到输入html标记中

{%load widget\u tweaks%}
{%form.visible_fields%}
{{field.label}
{%render_field class=“form control”%}

{%endfor%}
ManyToManyField
应呈现为多选字段,因此不需要手动执行任何操作。你的身体是什么样子的?您如何呈现表单?你能发布你的错误回溯吗?我已经用我正在使用的表单进行了更新,如果你能给我指出正确的方向,我将不胜感激。你为什么要手动呈现表单?你不能使用
{{form.as\u p}}
{{form.as\u table}}
?因为我必须向{{form.as\u p}中不可能的字段添加CSS类嗯,我不确定,但是你如何提交表单?为什么表单没有包装在
标记中?
ManyToManyField
应该呈现为多选字段,因此不需要手动执行任何操作。你的身体是什么样子的?您如何呈现表单?你能发布你的错误回溯吗?我已经用我正在使用的表单进行了更新,如果你能给我指出正确的方向,我将不胜感激。你为什么要手动呈现表单?你不能使用
{{form.as\u p}}
{{form.as\u table}}
?因为我必须向{{form.as\u p}中不可能的字段添加CSS类嗯,我不确定,但是你如何提交表单?为什么您的表单没有包装在
标签中?