表单布尔属性在提交后不会更改值

时间:2018-03-03 08:26:55

标签: python django

我正在为文件上传构建一个表单,该表单只对学生类型的帐户可见。我希望在学生提交表单后,它不会再次显示,所以在我的Student模型中我使用了一个布尔值。提交后,它将成为现实,表格不应再出现。问题是,布尔值没有改变值,所以我认为视图有问题。



class StudentFileUpload(models.Model):
    course = models.ForeignKey("Course", related_name='files', on_delete=None, default=None)
    files = models.FileField(upload_to='student_files', null=True, blank=True)
    comment = models.CharField(max_length=100, blank=True)
    user = models.OneToOneField('courses.User', on_delete=models.CASCADE, primary_key=True, blank=True, default=None)

    def __str__(self):
        return str(self.files)

    def file_link(self):
        if self.files:
            return "<a href='%s'>download</a>" % (self.files.url,)
        else:
            return "No attachment"

    file_link.allow_tags = True
    file_link.short_description = 'File Download'


class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)


class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    name = models.CharField(max_length=30, null=True, blank=True, default=None)
    surname = models.CharField(max_length=50, null=True, blank=True, default=None)
    email = models.EmailField(unique=True, null=True, blank=True, default=None)
    student_ID = models.CharField(unique=True, max_length=14,
                                  validators=[RegexValidator(regex='^.{14}$',
                                                             message='The ID needs to be 14 characters long.')],
                                  null=True, blank=True, default=None)
    photo = models.ImageField(upload_to='students_images', null=True, blank=True, default=None)
    phone = models.CharField(max_length=15, null=True, blank=True, default=None,
                             validators=[RegexValidator(regex='^[a-zA-Z0-9+]+$',
                                                        message='Not a valid phone number.')], )
    file_status = models.BooleanField(default=False)
&#13;
{% if user.is_student %}
                            {{ user.student.file_status }}
                            {% if user.student.file_status == False %}
                                <form method="POST" action="" enctype="multipart/form-data">
                                    {% csrf_token %}
                                    {{ form.as_p }}
                                    <button type="submit">Submit</button>
                                </form>
                            {% endif %}
                        {% endif %}
&#13;
def courses(request, slug):
    query = Course.objects.get(slug=slug)
    data = request.POST.copy()
    data['course'] = query.id
    form = StudentFileForm(data, request.FILES)
    if request.method == 'POST':
        if form.is_valid():
            upload = form.save(commit=False)
            upload.user = request.user
            upload.user.student.file_status = True
            print(upload.user.student.file_status)
            upload.save()
            return HttpResponseRedirect(reverse('courses:confirmation'))
    else:
        form = StudentFileForm(initial={'course': query.id})
    context = {'courses': Course.objects.filter(slug=slug),
               'students': Student.objects.all(),
               'students_data': StudentData.objects.all(),
               'lectures': query.lectures.order_by('lecture_category'),
               'form': form,
               'uploads': StudentFileUpload.objects.all(),
               }
    return render(request, 'courses/courses.html', context)

class StudentFileForm(forms.ModelForm):
    class Meta:
        model = StudentFileUpload
        fields = ('course', 'files', 'comment')
        widgets = {
            'course': forms.HiddenInput()
        }
        exclude = ['user']
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您只保存&#34;上传&#34;,因此您只需指定user.id. 如果需要更改Student对象,则应隐式保存它: upload.user.student.save()