博客开通登录才可评论的功能


发布时间:2019-02-15 13:08    作者: Uncle Hui   已过去:1 年,5 月   阅读总量:376 已被赞:3


如果不使用登录限制,会造成大量的垃圾评论,影响服务器数据的维护,所以最好开通登录才可以评论的功能,步骤如下:

1.因为django的视图里,都传入了一个 request参数,这个参数里已经包含了用户信息,所以我们只需要判断前端是否有用户存在在就可以了,如果存在用户,就可以留言,否则就请登录:{{request.user.username }}获取用户名,

{% if request.user.is_authenticated %} 判断是否存在用户

<div class="title" id="comment">
        <h3>评论:({{ pingluns|length }})</h3>
      </div>
      <div id="respond">
     {% if request.user.is_authenticated %}
            <form action="" method="POST">
                <div class="comment">
                    <input name="name" id="" class="form-control" size="22"    maxlength="15" autocomplete="off" tabindex="1" type="hidden" value="{{request.user.username }}">
                    <input name="email" id="" class="form-control" size="22"  maxlength="58" autocomplete="off" tabindex="2" type="hidden" value="{{ request.user.emali }}">
                    <input name="wenzhang_id" id="" class="form-control" size="22"  maxlength="58" autocomplete="off"  type="hidden" value="{{ wenzhang.pk }}">
                    <div class="comment-box">
                        <textarea placeholder="您的评论或留言(必填)" name="comment" id="comment-textarea" cols="100%" rows="3" tabindex="3"></textarea>
                <button type="submit" name="comment-submit" id="comment-submit" tabindex="4">评论</button>
                        </div>
                    </div>
            </form>
       {% else %}
         <form class="form-inline"  action="{% url 'blog_login' %}" method="POST">
          <div class="form-group">
            <label for="exampleInputName2">用户名</label>
            <input type="text" class="form-control" name="username" placeholder="用户名">
          </div>
          <div class="form-group">
            <label for="exampleInputEmail2">密码</label>
            <input type="password" class="form-control" name="password" placeholder="密码">
          </div>
          <button type="submit" class="btn btn-default">登录</button>
         </form>
           {% endif %}
            </div>
      <span id="postcomments">
{% for pinglun in pingluns %}
<div class="item article-meta-time" style="border:1px solid #31B2F3">
<ol id="comment_list" class="commentlist">
<li class="comment-content"><span class="comment-f">{{ forloop.counter }}</span>
    <div class="comment-main"><p><a class="address"  rel="nofollow">{{ pinglun.author }}</a><span class="time">{{ pinglun.time }}</span><br>{{ pinglun.content }}</p></div></li></ol>
</div>
    {% empty %}
    暂无评论,欢迎发表意见!
{% endfor %}
      </span>
     </div>

2.views.py里写登录视图:如果没有用户,就要走这步

from django.contrib import auth #导入用户模块
def login(request):
    uesrname = request.POST.get('username',None)
    password = request.POST.get('password',None)
    user = auth.authenticate(request,username=uesrname,password=password) #将获取的用户名和密码提交验证
    if user is not None:
        auth.login(request,user)
        return redirect('/')
    else:
        return render(request,'blog/404.html',{'message':"用户名或密码错误"})

这里还要继续优化,以便登录后回到用户来访的页面,下步在完善!

3.url.py

path('login/', views.login,name='blog_login'),

4.评论接收视图

if request.method == 'POST':
    name = request.POST.get('name')
    print(name)
    print('............................................')
    email = request.POST.get('email')
    print(email)
    comment = request.POST.get('comment')
    wenzhang_id = request.POST.get('wenzhang_id')
    wenzhang = Blogwenzhang.objects.get(pk=wenzhang_id)
    pingluns = Pinlun(author=name, email=email, content=comment)
    pingluns.wenzhang = wenzhang
    pingluns.save()

我是直接写在文章详情视图里的

点赞

3




登陆后方可评论