[英]CreateView not saving form Django
im new to Django.我是 Django 的新手。 Im creating a bidding site, where users should be able to visit a page to create a new listing (item that they are going to put up).The form that they are required to submit has a few common fields and when valid, the said form should be saved.我正在创建一个投标网站,用户应该能够访问一个页面来创建一个新的列表(他们将要发布的项目)。他们需要提交的表单有一些常见的字段,并且当有效时,说应保存表格。
Problem is, my listingcreateview() is not doing that.问题是,我的 listingcreateview() 没有这样做。 I submit a correct form but it wont save, it just redirects to the same form page and No errors are shown.我提交了一个正确的表单,但它不会保存,它只是重定向到同一个表单页面并且没有显示错误。
This because the submitted form is validated as invalid every time.这是因为提交的表单每次都被验证为无效。 I know this because of the two functions i added inside listingcreateview(), the second one is called.我知道这是因为我在listingcreateview() 中添加了两个函数,第二个函数被调用。
It was working properly before, dont know what changes messed it up.以前可以正常工作,不知道是什么变化搞砸了。 If i add in the admin interface information by hand, it is saved successfully.如果我手动添加管理界面信息,则保存成功。
views.py:视图.py:
class ListingCreateView(CreateView):
model = Listing
fields = ['title', 'content', 'image', 'min_bid', 'categories']
def form_valid(self, form):
form.instance.seller = self.request.user
return super().form_valid(form)
def form_invalid(self, form):
return HttpResponseRedirect(reverse("index"))
models.py:模型.py:
class User(AbstractUser):
pass
class Listing(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=100)
image = models.ImageField(blank=False, upload_to='media')
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
categories = models.CharField(max_length=25, choices = category)
seller = models.ForeignKey(User, on_delete=models.CASCADE) ##
min_bid = models.FloatField(blank=False)
image_thumbnail = ImageSpecField(source='image', processors=[ResizeToFill(300, 150)], format='JPEG', options={'quality':100})
def get_absolute_url(self):
return reverse('listing-detail', kwargs={'pk': self.pk})
listing_form.html: Listing_form.html:
{% extends "auctions/layout.html" %}
{% block body %}
<h2> Create Listing </h2>
{% if message %}
<div>{{ message }}</div>
{% endif %}
{% if messages %}
<div class="alert alert-warning" role="alert">
{{ messages }}
</div>
{% endif %}
<div class="container">
<form method="POST" action="">
{% csrf_token %}
<label class="label">{{ form.title.label }}</label>
<div class="input">{{ form.title }}</div>
<label class="label">{{ form.content.label }}</label>
<div class="input">{{ form.content }}</div>
<label class="label">{{ form.image.label }}</label>
<div class="input">{{ form.image }}</div>
<label class="label">Minimal bid</label>
<div class="input">{{ form.min_bid }}</div>
<label class="label">{{ form.categories.label }}</label>
<div class="input">{{ form.categories }}</div>
<input type="submit" value="Submit">
</form>
</div>
{% endblock %}
urls.py:网址.py:
path("create-listing", login_required(ListingCreateView.as_view()), name="create-listing")
Your form is invalid because form is missing enctype="multipart/form-data" which is needed for file uploads您的表单无效,因为表单缺少文件上传所需的 enctype="multipart/form-data"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.