在 django 视图的上下文中传递表单会在 aws 服务器上提供 502 bad gateway

问题描述

我在 django 视图中传递表单对象后立即收到 502 bad gateway 错误。如果我不通过表单,它不会在服务器上出现任何错误

这是我的模型

    class scholarship(models.Model):
    GRADUATION_CHOICES = (
    ('UG','Under Graduate'),('G','Graduate'),('PG','Post Graduate'),)
    name = models.TextField()
    slug = models.SlugField(max_length=250,unique=True)
    supportingImage = models.FileField(upload_to ='student/scholarship/supportingImage/',null=True,blank=True,validators=[FileExtensionValidator(['jpg','png','jpeg'])])
    author = models.ForeignKey(User,on_delete=models.SET_NULL,related_name='authorSCH',blank=True)
    productCategory = models.TextField(null=True,blank=True)
    currentClass = models.ManyToManyField(studentClass,related_name='currentClasssstudent',blank=True)
    graduation = models.CharField(max_length=10,choices=GRADUATION_CHOICES,default='G')
    scholarshipAmount = models.TextField(null=True,blank=True)
    fee = models.TextField(null=True,blank=True)
    gender = models.CharField(max_length=50,choices=GENDER_CHOICES,blank=True)
    country = models.ForeignKey(countryAndCurrencyList,related_name="countrySCH",null=True)
    state = models.ForeignKey(state,related_name="stateSCH",null=True)
    city = models.ForeignKey(city,related_name="citySCH",null=True)
    eligibility = models.TextField(null=True,blank=True)
    scholarshipCourse = models.ManyToManyField("course",related_name='scholarshipCourseS',blank=True)
    scholarshipCareer = models.ManyToManyField(career,related_name='scholarshipCareerS',blank=True)
    scholarshipCollege = models.ManyToManyField(collegeProfile,related_name='scholarshipCollegeS',blank=True)
    scholarshipUniversity = models.ManyToManyField(universityProfile,related_name='scholarshipUniversityS',blank=True)
    scholarshipEntrance = models.ManyToManyField(entranceExams,related_name='scholarshipEntrance',blank=True)
    scholarshipGlobalisation = models.CharField(max_length=15,choices = AREA_CHOICE,default='india')
    datetoApply = models.DateField(default=datetime.date.today)
    wheretoApply = models.TextField(null=True,blank=True)
    lastDatetoApply = models.DateField(default=datetime.date.today)
    description = models.TextField(null=True,blank=True)
    publish = models.DateTimeField(default=timezone.Now)
    created = models.DateTimeField(auto_Now_add=True)
    updated = models.DateTimeField(auto_Now=True)
    status = models.CharField(max_length=10,choices=STATUS_CHOICES,default='draft')


    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'Student-Scholarship'

    def get_absolute_url(self):
        return reverse('studentApp:scholarshipDetail',args=[self.slug])

    def save(self):
        if self.name:
            if not self.slug:
                self.slug = re.sub(r'[\W_]+','-',self.name.lower())
            super(scholarship,self).save()
        else:
            super(scholarship,self).save()

这是我的表格

class scholarshipForm(forms.ModelForm):
class Meta:
    model = scholarship
    exclude = ['publish','created','updated','status','author']
    widgets = {
        'description': SummernoteWidget(),}

这是我的看法

def scholarshipView(request,instanceID=None):
    staff = checkIfStaff(request)
    scholarshipInstance = None
    scholarshipFlag = False
    classList = studentClass.objects.all()
    print("before list")
    print("after list")
    if instanceID:
        scholarshipInstance = get_object_or_404(scholarship,id=instanceID)
        scholarshipFlag = True
    print("check if instant")
    if request.method == 'POST':
        form = scholarshipForm(request.POST,request.FILES,instance=scholarshipInstance)
        crr_usr = request.user
        if form.is_valid():
            if instanceID:
                name = request.POST.get('nameOfChange')
                obj = scholarshipChange(nameOfChange=name,scholarship=scholarshipInstance)
                obj.save()
            cd = form.save()
            cd.refresh_from_db()
            cd.author = crr_usr
            cd.save()
            form = scholarshipForm()
            messages.success(request,'Scholarship @R_518_4045@ion sent For Verification.')
        else:
            messages.error(request,'Please check. An Error occured for saving the scholarship @R_518_4045@ion.')
    else:
        form = scholarshipForm(instance=scholarshipInstance)
        print(form)
    print("form initialised")
    if staff == 0:
        return HttpResponse('You are not authorised to view this page. If you are seeing this message by mistake,Kindly get in touch with your System Administrator')
    else:
        #content = {'form': form,'flag':scholarshipFlag,'classList':classList,'courseList':courseList,'collegeList':collegeList,'careerList':careerList,'entranceList':entranceList,'universityList':universityList}
        return render(request,'dashboard/UI/add-scholarship.html',{'classList':classList,'form':form})

一旦我在渲染语句中传递表单对象,它就会给我带来问题。如果我避免传递表单对象并传递其他任何东西,或者即使只是渲染模板,它也不会出错。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)