我正在尝试创建一个简单的消息传递应用程序,用户可以在其中相互发送邮件。
我可以创建一条消息并发送它,但如果我想将它保存为草稿怎么办?
<form method="POST" >{% csrf_token %}
{{form.recipient}}
{{form.subject}}
{{form.message}}
<input type = "submit" value= "send" class="save" />
</form>
<input type = "submit" value= "draft" class="save"/>
我的表单有一个提交按钮,它是用于发送邮件的发送按钮。如果我要实现一个保存功能,即草稿功能。我还必须在表单中添加一个保存按钮。如果我这样做的话。如何在我的 views.py 中唯一标识用户选择了哪个选项?
@login_required
def Create(request):
person = Person.objects.get(user=request.user)
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'))
return render(request,'create.html',{'messages':messages,'form':form,'person':person})
形式
class NewMessageForm(forms.ModelForm):
recipient = forms.CharField(required=True,max_length=1)
message = forms.CharField(widget=forms.Textarea,required=True,max_length=1)
class Meta:
model = Thread
fields = ('subject',)
模型
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)