我正在尝试制作一个表单,当前登录用户可以在其中发布新状态。我正在使用模型表格。加载页面时我没有收到任何错误,但是似乎没有发布任何数据。所以我的问题是如何让这个为当前用户发布新状态?这是我当前的代码:
我的观点:
@login_required
def post(request, username):
logged_user = request.user
newpostform = PostForm(instance=request.user.get_profile(), data=request.POST)
if newpostform.is_valid():
newpostform.save()
newpostform = PostForm()
context = {'newpostform': newpostform,}
return render(request, 'twitterclone/newpost.html', context)
我的表格:
class PostForm(ModelForm):
class Meta:
model = Post
我的模型:
class Profile(models.Model):
user = models.ForeignKey(User)
bio = models.TextField()
image = models.ImageField(upload_to='images/%Y/%m/%d')
following = models.ManyToManyField('self', blank = True, null = True)
def __unicode__(self):
return unicode(self.user)
class Post(models.Model):
profile = models.ForeignKey(Profile)
body = models.CharField(max_length=140)
pub_date = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.body[:50])
我的模板:
<form action="" method="POST">
{% csrf_token %}
{{newpostform.body}}
<button>Tweet It</button>
</form>