python - 带引导程序的表单
问题描述
我正在尝试使创建表单看起来更好,但它不起作用。它显示得很好,但是当我尝试提交时它什么也没做。我已经尝试过使用不同形式的一个字段,我想我不能让它工作,因为它有文件字段和外键等。
为什么它不起作用?
表格.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>
解决方案
应该是这样的
视图.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>
推荐阅读
- python - 用 two_datetime 转换两种格式的时间戳
- python - 在python中大写而不用小写字符串
- office-js - 使用带有 typescript 的 VSCode 的 OfficeJS 的 IIS 部署和托管
- laravel - 响应必须是社交名媛 Facebook 上的字符串
- delphi - 使用 Delphi 在 Firefox 中填充公式数据
- reactjs - 如何从我从 firestore 数据库中获取的文本值中绘制图像?
- reactjs - 如何将对象数组传递给表
- onesignal - One Signal App 推送通知终止服务器
- fortran - 数据输入后不显示任何内容
- c# - 是否可以将实体的属性设置为不变并且不影响实体的其余部分?