让评论指定为某文章评论
来源:互联网 发布:linux 声卡驱动 编辑:程序博客网 时间:2024/05/16 07:00
普通评论
从Article中查找普通评论
M
from django.db import models# Create your models here.class People(models.Model): name = models.CharField(null=True, blank=True,max_length=200) job = models.CharField(null=True, blank=True, max_length=200) def __str__(self): return self.nameclass Article(models.Model): headline = models.CharField(null=True, blank=True,max_length=500)#新增 content = models.TextField(null=True, blank=True)#新增 TAG_CHOICES = ( ('tech', 'Tech'), ('life','Life'), ) tag = models.CharField(null=True, blank=True, max_length=5, choices=TAG_CHOICES) def __str__(self): return self.headlineclass Comment(models.Model): name = models.CharField(null=True, blank=True, max_length=50) comment = models.TextField() belong_to = models.ForeignKey(to=Article, related_name='under_comments', null=True, blank=True) #归属=模型.外键(归属于Article,从外部查询叫under_comments,没有是True,空格是True)【article.unser_comment.all】 def __str__(self): return self.comment
Django后台不显示创建的模型的解决办法
在admin.py中注册模型
from django.contrib import adminfrom firstapp.models import People, Article, Comment# Register your models here.admin.site.register(People)admin.site.register(Article)admin.site.register(Comment)
MM
python manage.py makemigrations
python manage.py migrate
评论归属在文章下【后台管理页面操作】
V
from django.shortcuts import render, HttpResponse, redirectfrom firstapp.models import Article, Commentfrom django.template import Context, Templatefrom firstapp.form import CommentForm# Create your views here.def index(request): print(request) print('==='*30) print(dir(request)) print('==='*30) print(type(request)) queryset = request.GET.get('tag') if queryset: article_list = Aritcle.objects.filter(tag=queryset) else: article_list = Article.objects.all() context = {} context['article_list'] = article_list index_page = render(request, 'first_web_2.html', context) return index_pagedef detail(request, page_num): if request.method == 'GET': form = CommentForm if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] a = Article.objects.get(id=page_num) #实例化id=page_num的文章 c = Comment(name=name, comment=comment, belong_to=a) #实例化评论(归属于文章a) c.save() return redirect(to='detail', page_num=page_num) #重定向到page_num页的文章 context = {} #comment_list = Comment.objects.all()#不需要 article = Article.objects.get(id=page_num) context['article'] = article #context['comment_list'] = comment_list#不需要 context['form'] = form return render(request, 'article_detail.html', context)
T
从实例化article中查找comment(对外叫under_comments)
{% for comment in article.under_comments.all %}
<!DOCTYPE html>{% load staticfiles %}<html><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8"> <link href="https://fonts.googleapis.com/css?family=0swald|Raleway" rel="stylesheet"> <style type="text/css"> .ui.segment.container{ width:700px; } p { font-family:'Raleway', sans-serif; font-size:18px; } body { background: url({% static 'images/star_banner.jpg' %}); background-size:cover; background-repeat:no-repeat; background-attachment:fixed } </style></head><body> <div class="ui image"> <img src="" alt="" /> </div> <div class="ui segment padded container"> <h1 class="ui header" style="font-family:'Oswald',sans-serif;font-size: 40px"> {{ article.headline }} </h1> <p> {{ article.content }} </p> </div> <div class="ui segment container" style="width:700px;"> <h3 class="ui header" style="font-family: 'Oswald', sans-serif;">Comments</h3><!--标题Comment--> <div class="ui comments"><!--评论--> {% for comment in article.under_comments.all %} <div class="comment"> <div class="avatar"> <img src="http://semantic-ui.com/images/avatar/small/matt.jpg" alt="" /> </div> <div class="content"> <a href="#" class="author">{{ comment.name }}</a> <div class="metadata"> <div class="date">2 days ago</div> </div> <p class="text" style="font-family:'Raleway', sans-serif;"> {{ comment.comment }} </p> </div> </div> {% endfor %} </div> <div class="ui divider"></div><!--分割线评论--> <form class="ui error tiny form" method="post"> {% if form.errors %} <div class="ui error message"> {{ form.errors }} </div> {% for field in form %} <div class="{{ field.errors|yesno:'error, '}} field"><!--#依次判断表格中的field是否有error--> {{ field.label }} {{ field }} </div> {% endfor %} {% else %} {% for field in form %} <div class="field"> {{ field.label }} {{ field }} </div> {% endfor %} {% endif %} {% csrf_token %}<!--Django防跨站"{%csrf_token%}"标签--> <button type="submit" class="ui blue button">Click</button> </form> </div></body></html>
优秀评论
M
from django.db import models# Create your models here.class People(models.Model): name = models.CharField(null=True, blank=True,max_length=200) job = models.CharField(null=True, blank=True, max_length=200) def __str__(self): return self.nameclass Article(models.Model): headline = models.CharField(null=True, blank=True,max_length=500)#新增 content = models.TextField(null=True, blank=True)#新增 TAG_CHOICES = ( ('tech', 'Tech'), ('life','Life'), ) tag = models.CharField(null=True, blank=True, max_length=5, choices=TAG_CHOICES) def __str__(self): return self.headlineclass Comment(models.Model): name = models.CharField(null=True, blank=True, max_length=50) comment = models.TextField() belong_to = models.ForeignKey(to=Article, related_name='under_comments', null=True, blank=True) best_comment = models.BooleanField(default=False) #默认都不是最优评论,最优评论需手动选择 def __str__(self): return self.comment
MM
python manage.py makemigrations
python manage.py migrate
后台管理最优评论
V
from django.shortcuts import render, HttpResponse, redirectfrom firstapp.models import Article, Commentfrom django.template import Context, Templatefrom firstapp.form import CommentForm# Create your views here.def index(request): print(request) print('==='*30) print(dir(request)) print('==='*30) print(type(request)) queryset = request.GET.get('tag') if queryset: article_list = Aritcle.objects.filter(tag=queryset) else: article_list = Article.objects.all() context = {} context['article_list'] = article_list index_page = render(request, 'first_web_2.html', context) return index_pagedef detail(request, page_num): if request.method == 'GET': form = CommentForm if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] a = Article.objects.get(id=page_num) c = Comment(name=name, comment=comment, belong_to=a) c.save() return redirect(to='detail', page_num=page_num) context = {} #comment_list = Comment.objects.all() article = Article.objects.get(id=page_num) #实例化指定页面的article best_comment = Comment.objects.filter(best_comment=True, belong_to=article) #在评论中筛选评论属性best_comment为True,从属于article的的评论 #best_comment=models.BooleanField(default=True)【勾选为best_comment的评论】<=============等价于============>best_comment=True context['article'] = article #context['comment_list'] = comment_list context['form'] = form if best_comment: context['best_comment'] = best_comment[0] #如果有best_comment,则挑选第一个best_comment return render(request, 'article_detail.html', context)
T
<!DOCTYPE html>{% load staticfiles %}<html><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8"> <link href="https://fonts.googleapis.com/css?family=0swald|Raleway" rel="stylesheet"> <style type="text/css"> .ui.segment.container{ width:700px; } p { font-family:'Raleway', sans-serif; font-size:18px; } body { background: url({% static 'images/star_banner.jpg' %}); background-size:cover; background-repeat:no-repeat; background-attachment:fixed } </style></head><body> <div class="ui image"> <img src="" alt="" /> </div> <div class="ui segment padded container"> <h1 class="ui header" style="font-family:'Oswald',sans-serif;font-size: 40px"> {{ article.headline }} </h1> <p> {{ article.content }} </p> </div> <div class="ui segment container" style="width:700px;"> <h3 class="ui header" style="font-family: 'Oswald', sans-serif;">Comments</h3><!--标题Comment--> <div class="ui comments"><!--评论--> {% if best_comment %} <div class="ui mini red left ribbon label"> <i class="icon fire"></i> BEST </div> <div class="best comment"> <div class="avatar"> <img src="http://semantic-ui.com/images/avatar/small/matt.jpg " alt="" /> </div> <div class="content"> <a href="#" class="author">{{ best_comment.name }}</a> <div class="metadata"> <div class="date">2 days ago</div> </div> <p class="text" style="font-family: 'Raleway', sans-serif;"> {{ best_comment.comment }} </p> </div> </div> {% endif %} {% for comment in article.under_comments.all %} <div class="comment"> <div class="avatar"> <img src="http://semantic-ui.com/images/avatar/small/matt.jpg" alt="" /> </div> <div class="content"> <a href="#" class="author">{{ comment.name }}</a> <div class="metadata"> <div class="date">2 days ago</div> </div> <p class="text" style="font-family:'Raleway', sans-serif;"> {{ comment.comment }} </p> </div> </div> {% endfor %} </div> <div class="ui divider"></div><!--分割线评论--> <form class="ui error tiny form" method="post"> {% if form.errors %} <div class="ui error message"> {{ form.errors }} </div> {% for field in form %} <div class="{{ field.errors|yesno:'error, '}} field"><!--#依次判断表格中的field是否有error--> {{ field.label }} {{ field }} </div> {% endfor %} {% else %} {% for field in form %} <div class="field"> {{ field.label }} {{ field }} </div> {% endfor %} {% endif %} {% csrf_token %}<!--Django防跨站"{%csrf_token%}"标签--> <button type="submit" class="ui blue button">Click</button> </form> </div></body></html>
阅读全文
0 0
- 让评论指定为某文章评论
- 【推荐】为一个评论推荐一篇文章
- 为个人博客添加文章评论功能
- 哈佛商业评论文章--让用户自愿为你做贡献
- 评论
- 评论
- 评论
- 评论
- 评论
- 评论
- 评论
- 评论
- 评论
- 评论一篇搞笑文章
- 文章评论代码
- 用户文章评论
- redis文章评论
- Vue-文章详情页的评论功能(抽取为公共组件-评论组件)
- 第三方登录之新浪微博登录
- 为什么使用索引查询如此之快!
- 13 字符串
- unity给localRotation赋值
- 14 类型信息
- 让评论指定为某文章评论
- 递归基础之李白打酒
- MySQL 显示表字段和表插方法
- 平凡的世界——少平写给晓霞的信
- 不用vs,c 语言windows下编写socket,用gcc编译
- 微信小程序上传文件问题
- 16 数组
- Android 6.0 权限适配(PermissionsDipatcher)
- 解决SlidingMenu滑动Menu无法关闭菜单的问题