日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

bootstrap mysql源码_Django+Bootstrap+Mysql 搭建个人博客 (六)

發(fā)布時間:2024/9/27 数据库 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bootstrap mysql源码_Django+Bootstrap+Mysql 搭建个人博客 (六) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

6.1.comments插件

(1)安裝

pip install django-contrib-comments

(02)settings

INSTALLED_APPS =['django.contrib.sites','django_comments',

]

SITE_ID =1

(3)website/url

url(r'^comments/', include('django_comments.urls')),

(4)修改源碼

django_comments/abstracts.py第36行

原代碼

site = models.ForeignKey(Site, on_delete=models.CASCADE)

修改后

site = models.ForeignKey(Site,default=None,blank=True,null=True,on_delete=models.CASCADE)

(5)修改源碼

django_comments/abstracts.py第52行

修改和增加了幾個字段

classCommentAbstractModel(BaseCommentAbstractModel):"""A user comment about some object."""

#Who posted this comment? If ``user`` is set then it was an authenticated

#user; otherwise at least user_name should have been set and the comment

#was posted by a non-authenticated user.

user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),

blank=True, null=True, related_name="%(class)s_comments",

on_delete=models.SET_NULL)#Explicit `max_length` to apply both to Django 1.7 and 1.8+.

user_email = models.EmailField(_("user's email address"), max_length=254,

blank=True,null=True,default='xxx@xxx.com')

user_url= models.URLField(_("user's URL"), blank=True,null=True,default='https://xxx.xxx.xxx.xxx')

user_name= models.CharField(_("user's name"), max_length=50, blank=True)

user_img= models.ImageField(upload_to="user_images", blank=True,null=True,verbose_name="用戶圖像")

comment_title= models.CharField(max_length=256,default="無標題", blank=True,null=True,verbose_name="評論標題")

parent_comment= models.ForeignKey('self',default=None, blank=True,null=True,related_name="child_comment",on_delete=models.CASCADE)

level = models.PositiveIntegerField(default=0, blank=True,null=True,verbose_name='評論層級') comment= models.TextField(_('comment'), max_length=COMMENT_MAX_LENGTH)

.

.

.

.

(6)修改源碼

django_comments/admin.py第29和37行

_('Content'),

{'fields': ('user', 'user_name', 'user_email', 'user_url', 'comment','comment_title','parent_comment')}

list_display = ('comment_title','name', 'content_type', 'object_pk', 'ip_address', 'submit_date', 'is_public', 'is_removed')

(7)修改源碼

django_comments/forms.py第97行表單

class?CommentDetailsForm(CommentSecurityForm):"""

Handles the specific details of the comment (name, comment, etc.)."""

#name = forms.CharField(label=pgettext_lazy("Person name", "Name"), max_length=50)

#email = forms.EmailField(label=_("Email address"))

#url = forms.URLField(label=_("URL"), required=False)

#Translators: 'Comment' is a noun here.

comment = forms.CharField(label=_('Comment'), widget=forms.Textarea,

max_length=COMMENT_MAX_LENGTH)

comment_title= forms.CharField(max_length=256,label=_('標題'))

parent_id= forms.IntegerField(min_value=-1,label=_('父評論的id'))

level= forms.IntegerField(min_value=0,label=_('層級'),)

.

.

.def get_comment_create_data(self, site_id=None):"""Returns the dict of data to be used to create a comment. Subclasses in

custom comment apps that override get_comment_model can override this

method to add extra fields onto a custom comment model."""parent_id= self.cleaned_data['parent_id']if parent_id <=0:

parent=None

_level= self.cleaned_data['level']else:

parent= get_model().objects.get(id=parent_id)

_level= self.cleaned_data['level'] + 4 #4是縮進

returndict(

content_type=ContentType.objects.get_for_model(self.target_object),

object_pk=force_text(self.target_object._get_pk_val()),#user_name=self.cleaned_data["name"],

#user_email=self.cleaned_data["email"],

#user_url=self.cleaned_data["url"],

comment_title = self.cleaned_data['comment_title'],

parent_comment=parent,

level=_level,

comment=self.cleaned_data["comment"],

submit_date=timezone.now(),

site_id=site_id or getattr(settings, "SITE_ID", None),

is_public=True,

is_removed=False,

)

(8)修改源碼

django_comments/views/comments.py第97行表單

52行

#添加

if not request.session.get('login', None) and notuser_is_authenticated:return redirect("/")

116行

#if user_is_authenticated:

#comment.user = request.user

#添加

if request.session.get('login',None):

comment.user_name= request.session['screen_name']

comment.user_img= request.session['profile_image_url']

以上都修改完后

python manage.py makemigtations

python manage.py migrate

6.2.評論表單

detail.html


{% get_comment_form for entry as form %}

{% get_comment_count for entry as comment_count %}

評論總數(shù): {{ comment_count }}


{% csrf_token %}

評論標題:

評論內(nèi)容:

{{ form.honeypot }}{{ form.content_type }}

{{ form.object_pk }}

{{ form.timestamp }}

{{ form.security_hash }}

??重置

??評論

效果:

現(xiàn)在測試評論,可以添加成功,但是還不能顯示

6.3.顯示評論

(1)views.py

from django_comments.models importCommentdefdetail(request,blog_id):#entry = models.Entry.objects.get(id=blog_id)

entry = get_object_or_404(models.Entry,id=blog_id)

md= markdown.Markdown(extensions=['markdown.extensions.extra','markdown.extensions.codehilite','markdown.extensions.toc',

])

entry.body=md.convert(entry.body)

entry.toc=md.toc

entry.increase_visiting()

comment_list=list()defget_comment_list(comments):for comment incomments:

comment_list.append(comment)

children=comment.child_comment.all()if len(children) >0:

get_comment_list(children)

top_comments= Comment.objects.filter(object_pk=blog_id, parent_comment=None,

content_type__app_label='blog').order_by('-submit_date')

get_comment_list(top_comments)return render(request, 'blog/detail.html', locals())

(2)detail.html


{% get_comment_form for entry as form %}

{% get_comment_count for entry as comment_count %}

評論總數(shù): {{ comment_count }}


{# 評論表單#} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的bootstrap mysql源码_Django+Bootstrap+Mysql 搭建个人博客 (六)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。