首页 > 解决方案 > 带引导程序的表单

问题描述

我正在尝试使创建表单看起来更好,但它不起作用。它显示得很好,但是当我尝试提交时它什么也没做。我已经尝试过使用不同形式的一个字段,我想我不能让它工作,因为它有文件字段和外键等。

为什么它不起作用?

表格.py

class Lcreate(forms.ModelForm):
    class Meta:
        model = Auction
        fields = ('name', 'img', 'description', 'category', 'starting_bid')
        
        widgets = {
            'description' : forms.Textarea(attrs={
                'rows': '5',
                'cols': '90',
                'maxlength': '500',
            }),
            'name' : forms.Textarea(attrs={
                'rows': '1',
                'cols': '100',
                'maxlength': '30',
            }),
        }

视图.py

@login_required(login_url="login")
def create(request):
    if request.method == "POST":
        form = Lcreate(request.POST or None, request.FILES or None)
        if form.is_valid():    
            user = request.user
            starting_bid = form.cleaned_data["starting_bid"] 
            description = form.cleaned_data["description"]
            name = form.cleaned_data["name"]
            img = form.cleaned_data["img"]
            category = form.cleaned_data["category"]
            listing = Auction(user=user, starting_bid=starting_bid, description=description, name=name, img=img, category=category)
            listing.save()
            return redirect('index')
    else:
        form = Lcreate()   
    return render(request, "auctions/create.html", {
        "form": Lcreate,
        "categories": Category.objects.all()

    })

html

<div class="space"></div>
    <div class="create-form">
        <h1>Create Listing</h1> 
        <form method="POST" enctype="multipart/form-data">
        {%csrf_token%}
        <div class="form-group">
            <label for="name">Title</label>
            <input autofocus class="form-control" id="name" type="text" name="name" placeholder="Title">
            <div class="space"></div>
            <label for="img">Image</label>
            <input type="file" id="img" name="img" class="form-control-file">
            <div class="space"></div>
            <label for="description">Description</label>
            <textarea class="form-control" name="description" id="description" rows="3" placeholder="description"></textarea>
            <div class="space"></div>
            <label for="category">Category</label>
            <select class="form-control" id="category" name="category">
                {% for category in categories %}
                    <option name="category">{{category.category_list}}</option>
                {%endfor%}
            </select>
            <div class="space"></div>   
            <label for="starting_bid">Bid</label> 
            <input class="form-control" type="number" name="starting_bid" placeholder="Starting Bid">
        </div>
            <button type="submit" class="btn btn-primary">Post Auction</button>    
        </form>
    </div>

标签: pythondjango

解决方案


应该是这样的

视图.py:

def create(request):
    infos=listing.objects.all()
    form = listingForm(request.POST, request.FILES)
    if form.is_valid():
       listing.objects.create(**form.cleaned_data)
       return render(request, "auctions/index.html", {'infos':infos})
    else:
       return render(request, 'auctions/create.html', {'form': form})

HTML

<h2>Create Listings</h2>
<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}{% load widget_tweaks %}
    <div class="form-group row">
        <label for="{{ form.id_name}}" class="col-sm-2 col-form-label">Title</label>
        <div class="col-sm-10">
            {{ form.name|add_class:"form-control" }}
        </div>
    </div>
    <div class="form-group row">
        <label for="{{ form.id_description}}" class="col-sm-2 col-form-label">Description</label>
        <div class="col-sm-10">
            {{ form.description |add_class:"form-control" }}
        </div>
    </div>
    <div class="form-group row">
        <label for="{{ form.id_starting_bid}}" class="col-sm-2 col-form-label">Starting Bid</label>
        <div class="col-sm-10">
            {{ form.id_starting_bid|add_class:"form-control" }}
        </div>
    </div>
    <div class="form-group row">
        <label for="{{ form.id_img }}" class="col-sm-2 col-form-label">Image</label>
        <div class="col-sm-10">
            {{ form.img |add_class:"form-control" }}
        </div>
    </div>
    <div class="form-group row">
        <label for="{{ form.id_category }}"  class="col-sm-2 col-form-label">Category</label>
        <div class="col-sm-10">
            {{ form.category|add_class:"form-control" }}
        </div>
    </div>
    <div class="form-group row">
        <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
</form>
        

推荐阅读