[英]Django modelform is not inserting data
I have a Modelform that works great and the redirect happens normally without raising an error. 我有一个可以正常工作的Modelform,重定向正常进行而不会引发错误。 but the data is not inserted into the table, here are my files :
但是数据没有插入表中,这是我的文件:
models.py models.py
class Item(models.Model):
# custom validators
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed.')
# fields
dress_name = models.CharField(max_length=20, null=False, verbose_name='وصف الفستان',
help_text='مثال: فستان سواريه احمر')
dress_size = models.ForeignKey(Size, on_delete='DO_NOTHING', verbose_name='مقاس الفستان')
dress_color = models.ForeignKey(Color, on_delete='DO_NOTHING', verbose_name='لون الفستان')
dress_image1 = models.ImageField(upload_to='documents/%Y/%m/%d', null=False, verbose_name='الصورة الأساسية للفستان',
help_text='لا يمكنك تركها فارغة')
dress_image2 = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, verbose_name='صورة إضافية ')
dress_image3 = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, verbose_name='صورة إضافة ')
dress_action = models.ForeignKey(Action, on_delete='DO_NOTHING', verbose_name='الفستان معروض لل ',
help_text='للبيع او للإيجار ')
dress_price = models.IntegerField(default=1, verbose_name='السعر')
dress_country = models.ForeignKey(Country, on_delete='CASCADE', verbose_name='البلد')
dress_city = models.CharField(max_length=80, verbose_name='المدينة او المحافظة')
dress_mobile = models.CharField(max_length=20, validators=[alphanumeric], verbose_name='رقم الهاتف ')
created_by = models.CharField(max_length=250,)
created_at = models.DateField(auto_now=True)
dress_active = models.BooleanField(default=False)
dress_special = models.BooleanField(default=False)
def __str__(self):
return self.dress_name
forms.py forms.py
class AddDressForm(ModelForm):
class Meta:
model = Item
exclude = ['created_by', 'created_at','dress_active','dress_special']
views.py views.py
def add_dress(request):
current_user = request.user
all_dress = Item.objects.all()
all_dress_s = Item.objects.all().filter(dress_special=True)
if request.method == "POST":
add_dress_form = AddDressForm(request.POST)
if add_dress_form.is_valid():
model_instance = add_dress_form.save(commit=False)
model_instance.created_by = current_user
model_instance.save()
return redirect('home')
else:
add_cat_form = AddDressForm()
context = {
'add_cat_form': add_cat_form,
'current_user': current_user,
'all_dress': all_dress,
'all_dress_s': all_dress_s,
}
return render(request, "fostania/add_dress.html", context)
I just need to know what is missing inside the code that simply let the request pass with no errors but never insert data into table! 我只需要知道代码中缺少的内容,这些内容只会让请求毫无错误地通过,而永远不会将数据插入表中!
**form in template ** **模板中的表格**
<form action="" method="post" name="AddDressForm" align="right">
{% csrf_token %}
{% for field in add_cat_form %}
{{ field.label_tag }} {{ field }} {{ feild.help_text }}
<br>
{% endfor %}
<input type="submit" class="btn btn-success" style="width: 100px;" value="إضافة" />
</form>
Here: 这里:
if add_dress_form.is_valid():
model_instance = add_dress_form.save(commit=False)
model_instance.created_by = current_user
model_instance.save()
return redirect('home')
Your are unconditionnally redirecting whether the form is valid or not. 您将无条件地重定向表格是否有效。 You want only want to redirect if the form was valid:
您只想在表单有效的情况下进行重定向:
if add_dress_form.is_valid():
model_instance = add_dress_form.save(commit=False)
model_instance.created_by = current_user
model_instance.save()
return redirect('home')
And you also want to make sure you display form errors in your template, cf https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html 而且您还想确保在模板中显示表单错误,请参阅https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.