Django modelform没有插入数据

时间:2018-05-18 23:26:23

标签: python django

我有一个效果很好的模型,并且重定向正常发生而不会引发错误。但数据没有插入表中,这是我的文件:

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

class AddDressForm(ModelForm):
    class Meta:
        model = Item
        exclude = ['created_by', 'created_at','dress_active','dress_special']

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)

我只需要知道代码中缺少什么,只是让请求通过而没有错误但从不将数据插入表中!

**表格模板**

 <form action="" method="post" name="AddDressForm" align="right">
{% csrf_token %}

{% for field in add_cat_form %}
{{ field.label_tag }}&nbsp;{{ field }}&nbsp;{{ feild.help_text }}
    <br>
{% endfor %}
 <input type="submit" class="btn btn-success" style="width: 100px;" value="إضافة" />
        </form>

1 个答案:

答案 0 :(得分:1)

下面:

created_on

您无条件地重定向表单是否有效。如果表单有效,您只想重定向:

payment_details

您还希望确保在模板中显示表单错误,参见https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html