如何在django中保存多个到多个字段

问题描述 投票:0回答:1

我试图从学生的形式保存学生,但它不是保存学生,它显示错误信息'错误的形式'。这个代码有任何解决方案。我认为错误是在HTML模板。错误是这样的:/ students / add / student /'ErrorDict'对象没有属性'status_code'请求方法:POST请求URL:http://127.0.0.1:8000/students/add/student/ Django版本:2.1.5异常类型:AttributeError异常值: 'ErrorDict'对象没有属性'status_code'

models.朋友

class Course(models.Model):
    title = models.CharField(max_length=250)
    basic_price = models.CharField(max_length=100)
    advanced_price = models.CharField(max_length=100)
    basic_duration = models.CharField(max_length=50)
    advanced_duration = models.CharField(max_length=50)


class Student(models.Model):
    name = models.CharField(max_length=100)
    course = models.ManyToManyField(Course)
    address = models.CharField(max_length=200)
    email = models.EmailField()
    phone = models.CharField(max_length=15)
    image = models.ImageField(upload_to='Students',blank=True)
    joined_date = models.DateField()

forms.朋友

class AddStudentForm(forms.ModelForm):
    class Meta:
        model = Student
        fields = '__all__'

views.朋友

def addstudent(request):
    courses = Course.objects.all()
    if request.method == 'POST':
        form = AddStudentForm(request.POST,request.FILES)
        if form.is_valid():
            student = form.save()
            student.save()
            messages.success(request,'student saved.')
            return redirect('students:add_student')
      #  else:
           # return HttpResponse(form.errors) --> it returns course

    else:
        form = AddStudentForm()
    return render(request,'students/add_student.html',{'form':form,'courses':courses})

add_student.html

<form action="{% url 'students:add_student' %}" method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="form-group">
                        <h5>Full Name <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="text" name="name" class="form-control" required data-validation-required-message="This field is required"> </div>
                    </div>
                    <div class="form-group">
                        <h5>Course <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <select name="course" id="select" required class="form-control">
                                <option value="">Select Your Course</option>
                                {% for course in courses %}
                                <option value="{{course.title}}">{{course.title}}</option>
                                {% endfor %}

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <h5>Address<span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="text" name="address" class="form-control" required data-validation-required-message="This field is required"> </div>
                    </div>
                    <div class="form-group">
                        <h5>Phone Number <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="text" name="phone" data-validation-match-match="password" class="form-control" required> </div>
                    </div>
                    <div class="form-group">
                        <h5>Email <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="email" name="email" data-validation-match-match="password" class="form-control" required> </div>
                    </div>
                      <div class="form-group">
                        <h5>Date <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="date" name="joined_date" data-validation-match-match="password" class="form-control" required> </div>
                    </div>
                    <div class="form-group">
                        <h5>Image <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="file" name="image" class="form-control" > </div>
                    </div>
                    <div class="text-xs-right">
                        <button type="submit" class="btn btn-info">Submit</button>
                    </div>
                </form>
django
1个回答
2
投票

您应该按照注释中的建议输出form.errors的值,以发现确切的错误。但是,我可以看到两个可能导致表单验证失败的直接问题。

首先,因为您的表单包含图片上传,您必须在模板中将enctype设置为multipart/form-data

<form action="{% url 'students:add_student' %}" method="post" enctype="multipart/form-data">

其次,上传的图像存在于request.FILES中,因此您需要将其传递给表单:

form = AddStudentForm(request.POST, request.FILES)
© www.soinside.com 2019 - 2024. All rights reserved.