0

我正在尝试使用用户模型创建自定义发送电子邮件应用程序。

我可以通过发送电子邮件使应用程序正常运行,但如果用户不存在,它不会在模板上引发错误。

因此,我尝试测试如果字符数超出并且没有错误 raise 是否会引发任何错误。有人能帮我吗

class Thread(models.Model):
    subject = models.CharField(max_length=100, blank=True)
    user = models.ForeignKey(User)

class Message(models.Model):
    user = models.ForeignKey(User, related_name='sender')
    recipient = models.ForeignKey(User, related_name='recipient')
    created = models.DateTimeField(auto_now_add=True)
    body = models.CharField(max_length=1000)
    read = models.BooleanField(default=False)
    trash = models.BooleanField(default=False)
    sentmessage = models.BooleanField(default=False)
    thread = models.ForeignKey(Thread)


    def __unicode__(self):
        return self.body

视图.py

@login_required
def Create(request):
    form = NewMessageForm()
    if request.method =='POST':
        form = NewMessageForm(request.POST)
        if form.is_valid():
            recipient = form.cleaned_data['recipient']
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            thread = Thread.objects.create(subject=subject,user=request.user)
            recipient = User.objects.get(username=recipient)
            message = Message.objects.create(user=request.user,recipient=recipient,body=message,thread=thread)
            return HttpResponseRedirect(reverse('world:message'))
        else:
            return HttpResponseRedirect(reverse('world:Create'))

    return render(request,'create.html',{'messages':messages,'form':form})

形式

class NewMessageForm(forms.Form):
    recipient = forms.CharField(required=True,max_length=1)
    subject = forms.CharField(required=True,max_length=1)
    message = forms.CharField(widget=forms.Textarea,required=True,max_length=1)

    def clean_recipient(self):
            recipient = self.cleaned_data['recipient']
            try:
                recipient = User.objects.get(username=recipient)
            except User.DoesNotExist:
                raise forms.ValidationError("This username does not exist")
            return recipient

模板

<form method="POST" >{% csrf_token %}

    {{form.recipient}}
    {{form.subject}}
    {{form.message}}

    <input type = "submit" value= "send" class="save" id="send"/>


</form>

{{form.recipient.errors}}
{{form.subject.errors}}
{{form.message.errors}}
4

1 回答 1

1

您的模式略有错误:

if request.method =='POST':
    form = NewMessageForm(request.POST)
    if form.is_valid():
        recipient = form.cleaned_data['recipient']
        subject = form.cleaned_data['subject']
        message = form.cleaned_data['message']
        thread = Thread.objects.create(subject=subject,user=request.user)
        recipient = User.objects.get(username=recipient)
        message = Message.objects.create(user=request.user,recipient=recipient,body=message,thread=thread)
        return HttpResponseRedirect(reverse('world:message'))
else:
    form = NewMessageForm()

想法是您首先检查它是否是 POST,好的 - 它是否有效?很好 - 返回正确的流程 - 否则会失败并通过 is_valid() 附加到上下文的错误传递表单。如果它是一个新的 - 作为最后的手段创建它,因为它还没有任何信息。

也不要忘记form.non_field_errors,因为它将包含不特定于任何字段的错误。

于 2013-07-02T10:43:45.027 回答