We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Success is finding satisfaction in giving a little more than you take.
Build a Blog Application
Creating a comment system
post detail
models.py
class Comment(models.Model): post = models.ForeignKey(Post, related_name='comments') name = models.CharField(max_length=80) email = models.EmailField() body = models.TextField() created = models.DateTimeField(auto_now_add=True) update = models.DateTimeField(auto_now=True) active = models.BooleanField(default=True) class Meta: ordering = ('created', ) def __str__(self): return 'Comment by {} on {}'.format(self.name, self.post)
post
related_name
comment.post
post.comment.all()
many-to-one
created
active
Comment
python manage.py makemigrations blog
pythom manage.py migrate
CommentAdmin
admin.py
from .models import Post, Comment class CommentAdmin(admin.ModelAdmin): list_display = ('name', 'email', 'post', 'created', 'active') list_filter = ('active', 'created', 'updated') search_fields = ('name', 'email', 'body') admin.site.register(Comment, CommentAdmin)
Creating forms from models
Django
Form
ModelForm
forms.py
from .models import Comment class CommentForm(forms.ModelForm): class Meat: model = Comment fields = ('name', 'email', 'body')
Handling ModelForms in views
post_detail
from .models import Post, Comment from .forms import EmailPostForm, CommentForm def post_detail(request, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) # 这个post的活动评论 comments = post.comments.filter(active=True) if request.method == 'POST': # 评论被提交 comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # 创建评论对象但是不保存到数据库 new_comment = comment_form.save(commit=False) # 指定当前的post为评论 new_comment.post = post # 保存评论到数据库 new_comment.save() else: comment_form = CommentForm() return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments, 'comment_form': comment_form})
Adding comments to the post deatail template
blog/detail.html
content
{% with comments.count as total_comments %} <p> {{ total_comments }} comment{{ total_comments|pluralize }} </p> {% endwith %}
ORM
comments.count()
comments.count
{% with %}
{% endwith %}
total_comments|pluralize
total_comments
pluralize
comment
s
{% for comment in comments %} <div class="comment"> <p class="info"> Comment {{ forloop.counter }} by {{ comment.name }} {{ comment.created }} </p> {{ comment.body|linebreaks }} </div> {% empty %} <p>There are no comments yet.</p> {% endfor %}
{% for %}
forloop.counter
{% if new_comment %} <p>Your comment has been added.</p> {% else %} <p>Add a new comment</p> <form action="." method="post"> {{ comment_form.as_p }} {% csrf_token %} <p><input type="submit" value="Add comment"></p> </form> {% endif %}
POST
CSRF token
Django 忘记了用户密码解决方法
SQL
createsuperuser
python manage.py createsuperuser
shell
User
set_password
python manage.py shell
from django.contrib.auth.models import User user = User.objects.get(username='admin') user.set_password('new_password') user.save()
Django 1.2
python manage.py changepassword username
The text was updated successfully, but these errors were encountered:
No branches or pull requests
0x01 Django
Build a Blog Application
Creating a comment system
post detail
模板,显示评论列表和增加评论的表单models.py
增加一个模型用于保存评论post
对应评论(可以多个评论),related_name
属性定义了这个关联的名字,一个评论的话可以用comment.post
来表示,一个post
中所有评论可以用post.comment.all()
表示,这是一个many-to-one
关联关系created
字段用于按照时间前后顺序来分类保存评论active
字段用于手动去除不想要的评论Comment
模型不会同步到数据库中,所以需要创建一个新的数据迁移CommentAdmin
admin.py
增加Creating forms from models
Django
有两种类来创建表单Form
和ModelForm
ModelForm
这个类forms.py
增加Handling ModelForms in views
post detail
视图来实例化表单,修改post_detail
Adding comments to the post deatail template
post detail
模板中增加评论的功能blog/detail.html
模板,在content
中增加ORM
来执行查询集comments.count()
,注意在Django
模板中,不用圆括号来调用方法,所以上面只写了comments.count
{% with %}
这个标签允许我们分配一个值给指定的新变量直到{% endwith %}
标签total_comments|pluralize
这里根据total_comments
评论的数量,用了pluralize
来显示是否需要在comment
后面加上s
来代表复数{% for %}
标签来循环,如果评论为空,那么返回默认的输出,通过forloop.counter
来枚举这些评论POST
请求的CSRF token
Django
忘记了用户密码解决方法Django
保存的密码是经过加密的,所以用SQL
重写的话比较麻烦,所以可以使用以下三种方法:createsuperuser
来创建一个新的帐号shell
,用User
的set_password
方法来重置密码Django 1.2
后新版,可以直接调用shell
修改The text was updated successfully, but these errors were encountered: