ModelForm没有保存在服务器上

时间:2014-06-03 11:52:02

标签: django django-models django-views django-forms

我有一个使用模型表单创建圆圈的视图。这在localhost上完全正常,但在服务器上,由于“save”方法不起作用,因此无法创建数据。

ModelForm:
class CircleForm(forms.ModelForm):
    class Meta:
        model = Circles
        fields = ('heading','description',)
        #exclude = ['current_status', 'progress', 'Owner', 'team_tag', 'exceptional_tag','Team']

    def __init__(self, *args, **kwargs):
        super(CircleForm, self).__init__(*args, **kwargs)
        for name, field in self.fields.items():
            if field.widget.attrs.has_key('class'):
                field.widget.attrs['class'] += ' text'
            else:
                field.widget.attrs.update({'class':'text'})

Model:
class Circles(CommonInfo):
    CURRENT_STATUS = (('P', 'pending'),
                        ('C', 'complete'),
                        ('ED', 'exceeded deadline'),
                        ('I', 'incomplete'))
    Owner = models.ForeignKey(OrganisationUser, related_name="owner_name", blank=True, null=True)
    Team = models.TextField(default='', blank=True, null=True)
    heading = models.CharField(default='', max_length=100)
    description = models.CharField(default='', max_length=250)
    progress = models.PositiveIntegerField(default=0, null=True, blank=True)
    deadline = models.DateField(null=True,blank=True)
    current_status = models.CharField(choices=CURRENT_STATUS, max_length=2, default='P')
    team_tag = models.ForeignKey("certificates.Tag", related_name="team_tag_given", blank=True, null=True)
    exceptional_tag = models.ForeignKey("certificates.Tag", related_name="exeptioml_tag_given", blank=True, null= True)

View:
def non_ajax_post(self, request, *args, **kwargs):
        circle_form = CircleForm(request.POST)

        if circle_form.is_valid():
            try:
                circle = circle_form.save(commit=False)
                circle.Owner = request.user.organisation_user
                #circle.Team = json.dumps(request.POST.get('hidemps_id'))
                team_tag = Tag.objects.get(pk = request.POST.get('team_tag_id'))
                exceptional_tag = Tag.objects.get(pk = request.POST.get('exceptional_tag_id'))
                circle.team_tag = team_tag
                circle.exceptional_tag = exceptional_tag
                deadline = string_time_to_datetime(request.POST.get('deadline'), type_of_date='m/d/y')
                if deadline < datetime.now():
                    request.session['deadline_error'] = True
                    return HttpResponseRedirect(request.path)
                circle.deadline = deadline
                circle.save()
                request.session['circle_saved'] = True
                return HttpResponseRedirect(request.path) #here a different context is sent on page load
            except Exception as e:
                print e.message
        else:
            return HttpResponseRedirect(request.path)
        return HttpResponseRedirect(request.path)

此代码以我想要的方式工作并创建一个圆圈。但是当我在localhost上执行此操作时会发生这种情况。当我在服务器上尝试它时,它不起作用。 问题出在“circle.save()”这一行 我用了多个print语句来找出问题所在。打印在遇到“circle.save()”行时停止。之后,它将我重定向到同一页面。

有人可以建议一些解决方案吗?

0 个答案:

没有答案