【问题标题】:Using django form within custom html template在自定义 html 模板中使用 django 表单
【发布时间】:2021-04-14 22:27:15
【问题描述】:

我有一个 html 文件,我想使用这个模板来呈现我的表单并将其保存到我的数据库中 这个怎么做? HTML 代码:

    <div class="container-contact100">
        <div class="wrap-contact100">
            <form class="contact100-form validate-form" enctype="multipart/form-data" method="POST">
                {% csrf_token %}
                <span class="contact100-form-title">
                    Add Item!
                </span>

                <div class="wrap-input100 validate-input" data-validate="Name is required">
                    <span class="label-input100">Title</span>
                    <input class="input100" type="text" name="name" placeholder="Enter food name">
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 validate-input">
                    <span class="label-input100">Price</span>
                    <input class="input100" type="number" name="price" placeholder="Enter food price">
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 validate-input" data-validate = "Message is required">
                    <span class="label-input100">Description</span>
                    <textarea class="input100" name="message" placeholder="Your description here..."></textarea>
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 validate-input" data-validate="Image is required">
                    <input type="file" class="custom-file-input" id="customFile" name="filename">
                    <label class="custom-file-label" for="customFile">Choose file</label>
                    <span class="focus-input100"></span>
                </div>

                <div class="container-contact100-form-btn">
                    <div class="wrap-contact100-form-btn">
                        <div class="contact100-form-bgbtn"></div>
                        <button class="contact100-form-btn" type="submit">
                            <span>
                                Add
                                <i class="fa fa-long-arrow-right m-l-7" aria-hidden="true"></i>
                            </span>
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>



    <div id="dropDownSelect1"></div>

这是我的 forms.py :

from django import forms
from .models import Product

class AddItemForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = '__all__'

即使你可以通过 github 从此链接访问我的项目:

https://github.com/imanashoorii/FoodMenu.git

【问题讨论】:

    标签: python html django forms model


    【解决方案1】:

    您必须根据您的Porudct 模型的字段名称在您的html 中设置form 中的name 属性,如下所示(使用您的 GitHub 链接中的数据):

    add_product.html

    <form method="post" enctype="multipart/form-data"> # here define enctype attribute so your form can accept images
        {% csrf_token %}
        <input type="text" name="title">
        <input type="text" name="description">
        <input type="number" name="price">
        <input type="file" name="image">
        <button type="submit">Submit</button>
    </form>
    

    或者您可以将{{ form.as_p }} 传递给您的html 以在@987654326 中呈现每个字段,而不是手动定义每个字段像这样@标签:

    <form method="post" enctype="multipart/form-data"> 
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
    

    那么您将如何将其保存在视图中:

    views.py

    def add_product(request):
        if request.method == 'POST':
            form = AddItemForm(request.POST, request.FILES) # request.FILES is so your form can save images
            if form.is_valid()
                form.save()
                return redirect('home') # redirect to a valid page
        else:
            form = AddItemForm()
        return render(request, 'add_product.html', {'form': form})
    

    【讨论】:

    • 请忽略 add_product.html 并查看 index.html ,我想在 index.html 模板中为每个元素使用表单字段...
    • add_product.html 只是一个例子,你可以使用任何你想要的模板我的朋友
    • dude thxx 对您的回复和帮助:*,但现在我对我的 views.py add_product 进行了一些更改并解决了我的问题。
    • 所以你的问题解决了吗?
    • 是的 thnx ,,, 在这里添加它作为答案
    【解决方案2】:

    我已经通过将views.py更改为此并修复它来解决我的问题:

    if form.is_valid():
        title = request.POST.get('title')
        description = request.POST.get('description')
        price = request.POST.get('price')
        image = request.POST.get('image')
        form.save()
        return redirect("food:home")
    

    【讨论】:

    • 您不必在此处向每个字段请求数据,因为您没有在视图函数的任何地方使用它,验证后只需 form.save() 就足够了 :)
    猜你喜欢
    • 2015-11-28
    • 2017-05-02
    • 1970-01-01
    • 2015-11-28
    • 2014-07-12
    • 2011-10-05
    • 1970-01-01
    • 2013-03-28
    • 2018-04-14
    相关资源
    最近更新 更多