diff --git a/DjangoBlog/urls.py b/DjangoBlog/urls.py index 6c64812..204e923 100644 --- a/DjangoBlog/urls.py +++ b/DjangoBlog/urls.py @@ -19,5 +19,5 @@ from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'', include('blog.urls', namespace='blog', app_name='blog')), - + url(r'', include('comments.urls', namespace='comment', app_name='comments')) ] diff --git a/blog/models.py b/blog/models.py index 635d9bf..10ef92e 100644 --- a/blog/models.py +++ b/blog/models.py @@ -4,6 +4,7 @@ from django.conf import settings class Article(models.Model): + """文章""" STATUS_CHOICES = ( ('d', '草稿'), @@ -20,7 +21,7 @@ class Article(models.Model): pub_time = models.DateTimeField('发布时间', blank=True, null=True, help_text="不指定发布时间则视为草稿,可以指定未来时间,到时将自动发布。") status = models.CharField('文章状态', max_length=1, choices=STATUS_CHOICES, default='o') - commentstatus = models.CharField('评论状态', max_length=1, choices=COMMENT_STATUS) + comment_status = models.CharField('评论状态', max_length=1, choices=COMMENT_STATUS) summary = models.CharField('摘要', max_length=200, blank=True, help_text="可选,若为空将摘取正文的前300个字符。") views = models.PositiveIntegerField('浏览量', default=0) author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='作者', on_delete=models.CASCADE) diff --git a/blog/views.py b/blog/views.py index 3696a85..11f1cac 100644 --- a/blog/views.py +++ b/blog/views.py @@ -3,6 +3,7 @@ from django.shortcuts import render # Create your views here. from django.views.generic.list import ListView from django.views.generic.detail import DetailView +from django.views.generic import UpdateView from django.views.generic.edit import CreateView, FormView from django.views.generic.dates import YearArchiveView, MonthArchiveView from blog.models import Article, Category, Tag @@ -11,6 +12,7 @@ import markdown from django.template.defaultfilters import stringfilter from django.utils.safestring import mark_safe from django.core.exceptions import ObjectDoesNotExist +from comments.forms import CommentForm class ArticleListView(ListView): @@ -56,6 +58,13 @@ class ArticleDetailView(DetailView): except ObjectDoesNotExist: return None + form = CommentForm() + if self.request.user.is_authenticated(): + user = self.request.user + form.fields["email"].initial = user.email + form.fields["name"].initial=user.username + + kwargs['form'] = form next_article = get_article(articleid + 1) prev_article = get_article(articleid - 1) kwargs['next_article'] = next_article @@ -63,6 +72,15 @@ class ArticleDetailView(DetailView): return super(ArticleDetailView, self).get_context_data(**kwargs) + """ + def post(self, request, *args, **kwargs): + form = CommentForm(request.POST) + + if form.is_valid(): + data = form.cleaned_data + pass + """ + class CategoryDetailView(ArticleListView): # template_name = 'index.html' diff --git a/comments/forms.py b/comments/forms.py new file mode 100644 index 0000000..cd4109f --- /dev/null +++ b/comments/forms.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +""" +@version: ?? +@author: liangliangyy +@license: MIT Licence +@contact: liangliangyy@gmail.com +@site: https://www.lylinux.org/ +@software: PyCharm +@file: forms.py +@time: 2016/11/12 下午2:45 +""" +from .models import Comment +from django import forms +from django.forms import ModelForm +from django.contrib.auth.models import User + +""" +class CommentForm(forms.Form): + url = forms.URLField(label='网址', required=False) + email = forms.EmailField(label='电子邮箱', required=False) + name = forms.CharField(label='姓名') + body = forms.CharField(widget=forms.Textarea, label='评论') + parent_comment_id = forms.IntegerField(widget=forms.HiddenInput, required=False) +""" + + +class CommentForm(ModelForm): + url = forms.URLField(label='网址', required=False) + if User.is_authenticated: + email = forms.EmailField(label='电子邮箱', required=False, widget=forms.HiddenInput) + name = forms.CharField(label='姓名', widget=forms.HiddenInput) + else: + email = forms.EmailField(label='电子邮箱', required=False) + name = forms.CharField(label='姓名') + # body = forms.CharField(widget=forms.Textarea, label='评论') + parent_comment_id = forms.IntegerField(widget=forms.HiddenInput, required=False) + + class Meta: + model = Comment + fields = ['body'] + diff --git a/comments/models.py b/comments/models.py index 218b6d7..464fac8 100644 --- a/comments/models.py +++ b/comments/models.py @@ -7,12 +7,15 @@ from blog.models import Article # Create your models here. class Comment(models.Model): + #url = models.URLField('地址', blank=True, null=True) + #email = models.EmailField('电子邮件', blank=True, null=True) + body = models.TextField('正文') created_time = models.DateTimeField('创建时间', auto_now_add=True) last_mod_time = models.DateTimeField('修改时间', auto_now=True) author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='作者', on_delete=models.CASCADE) article = models.ForeignKey(Article, verbose_name='文章', on_delete=models.CASCADE) - parant_comment = models.ForeignKey('self', verbose_name="上级评论", blank=True, null=True) + parent_comment = models.ForeignKey('self', verbose_name="上级评论", blank=True, null=True) class Meta: ordering = ['created_time'] diff --git a/comments/templatetags/comments_tags.py b/comments/templatetags/comments_tags.py index 1d3e766..2c916fc 100644 --- a/comments/templatetags/comments_tags.py +++ b/comments/templatetags/comments_tags.py @@ -17,18 +17,28 @@ from django import template from django.template.loader import render_to_string from ..models import Comment from blog.models import Article +from comments.forms import CommentForm register = template.Library() - +""" @register.simple_tag(name='get_comment_count') def GetCommentCount(parser, token): commentcount = Comment.objects.filter(article__author_id=token).count() return "0" if commentcount == 0 else str(commentcount) + " comments" + @register.inclusion_tag('comments/tags/post_comment.html') -def load_post_comment(article): +def load_post_comment(article, lastform=None): + if not lastform: + form = CommentForm() + form.article_id = article.id + form.parent_comment_id = '' + else: + form = lastform return { - 'article': article + 'article': article, + 'form': form } +""" \ No newline at end of file diff --git a/comments/urls.py b/comments/urls.py new file mode 100644 index 0000000..f5af5fd --- /dev/null +++ b/comments/urls.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +""" +@version: ?? +@author: liangliangyy +@license: MIT Licence +@contact: liangliangyy@gmail.com +@site: https://www.lylinux.org/ +@software: PyCharm +@file: urls.py +@time: 2016/11/12 下午3:03 +""" + +from django.conf.urls import url +from . import views + +urlpatterns = [ + # url(r'^postcomment/(?P\d+)$', views.CommentPostView.as_view(), name='postcomment'), + url(r'^article/(?P\d+)/postcomment$', views.CommentPostView.as_view(), name='postcomment'), +] diff --git a/comments/views.py b/comments/views.py index 91ea44a..2a04c9a 100644 --- a/comments/views.py +++ b/comments/views.py @@ -1,3 +1,47 @@ from django.shortcuts import render # Create your views here. +from .models import Comment +from blog.models import Article +from .forms import CommentForm +from django.views.generic.edit import FormView +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse +from django.contrib.auth.models import User +from django.contrib.auth import get_user_model +from accounts.models import BlogUser + + +class CommentPostView(FormView): + form_class = CommentForm + template_name = 'blog/articledetail.html' + + def get(self, request, *args, **kwargs): + article_id = self.kwargs['article_id'] + url = reverse('blog:detail', kwargs={'article_id': article_id}) + return HttpResponseRedirect(url) + + def form_invalid(self, form): + article_id = self.kwargs['article_id'] + article = Article.objects.get(pk=article_id) + + return self.render_to_response({ + 'form': form, + 'article': article + }) + + def form_valid(self, form): + """提交的数据验证合法后的逻辑""" + article_id = self.kwargs['article_id'] + article = Article.objects.get(pk=article_id) + if not self.request.user.is_authenticated(): + pass + author_id = self.request.user.pk + + comment = form.save(False) + + comment.author = BlogUser.objects.get(pk=author_id) + + comment.article = article + comment.save(True) + return HttpResponseRedirect('/') diff --git a/templates/blog/articledetail.html b/templates/blog/articledetail.html index 2fbefda..535fc69 100644 --- a/templates/blog/articledetail.html +++ b/templates/blog/articledetail.html @@ -26,9 +26,10 @@ class="meta-nav">→ {% endif %} - {% if article.commentstatus == "o" %} - {% load comments_tags %} - {% load_post_comment article %} + {% if article.comment_status == "o" %} + {% comment %}{% load comments_tags %} + {% load_post_comment article from %}{% endcomment %} + {% include 'comments/tags/post_comment.html' %} {% endif %} diff --git a/templates/comments/tags/post_comment.html b/templates/comments/tags/post_comment.html index 16f6944..fe3e323 100644 --- a/templates/comments/tags/post_comment.html +++ b/templates/comments/tags/post_comment.html @@ -5,31 +5,55 @@ -
-

电子邮件地址不会被公开。 必填项已用*标注 -

-

+ required="required">{% endcomment %}

-

- {% endcomment %} + + -

-

- + aria-describedby="email-notes" aria-required='true' required='required'/>{% endcomment %} + {% if not form.email.is_hidden %} + {{ form.email.label_tag }} + {% endif %} + {{ form.email }} + {{ form.email.errors }}

-

-

+ {% comment %}

+ {{ form.url.label_tag }} + {{ form.url }} + {{ form.url.errors }} + +

{% endcomment %} + {{ form.parent_comment_id }} + +

+ +

+ - \ No newline at end of file + + + +