0.4.0更新,增加照片评论功能,需要登录。
模型models.py:content=models.CharField(max_length=5000)author=models.ForeignKey(User,related_name=’user_comments’)time=models.DateTimeField(auto_now_add=True)photo=models.ForeignKey(Photo,related_name=’photo_comments’):return u’%s’%(self.content)
增加一个Comment类,,Photo和User作为外键指定,也就是每一个Comment对象有对应的照片和作者,这样就能对应起来了。
模板photo.html
在图片界面,下面增加一个评论框,之后小更新了一下首页把已有评论放在图片下面。
photo.html
{% if user.is_authenticated %}=%}====>=====>{% else %}<h3>评论照片需要登录噢亲~</h3>{% endif %}
index.html
{% for cmt in photo.photo_comments.all %} ==>=>=> </div> </div> {% endfor %}视图views.py:photo=Photo.objects.get(pk=id)if request.method==’POST’:content=request.POST.get(‘content’)new_comment=Comment.objects.create(content=content,author=request.user,photo=photo)new_comment.save()photo.photo_comments.add(new_comment)photo.save()return HttpResponseRedirect(‘/p/%s’ %id)else:return render_to_response(‘photo.html’,RequestContext(request,{‘photo’:photo}))
完。
而不去欣赏今天就开在我们窗口的玫瑰。