作者: zifanwang 发布于2020-06-12
首先打开cmd,cd到mysite并输入:
C:\mysite>python manage.py startapp commen
C:\mysite>
接着修改mysite/settings.py的INSTALLED_APPS:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'article',
'userprofile',
'commen',
]
然后重写commen/views.py:
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render, redirect
from commen.forms import CommentForm
from commen.models import Comment
@login_required(login_url='/userprofile/login/')
def post_comment(request):
# 处理POST请求
if request.user.id==5:
comments = Comment.objects.all()
else:
comments = Comment.objects.filter(user=request.user)
if request.method == 'POST':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.user = request.user
new_comment.save()
return redirect('commen:post_comment')
else:
return HttpResponse("表单内容有误,请重新填写。")
else:
checkmobile = checkMobile(request)
context = {'comments': comments ,'checkmobile': checkmobile}
return render(request, 'comment/comment.html',context)
再重写commen/admin.py:
from django.contrib import admin
from .models import Comment
# 注册ArticlePost到admin中
admin.site.register(Comment)
新建commen/forms.py并写入:
from django import forms
from commen.models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['body']
接着修改commen/models.py:
from django.contrib.auth.models import User
from django.db import models
class Comment(models.Model):
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='comments',
)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('created',)
def __str__(self):
return self.body[:20]
再新建commen/urls.py并写入:
from django.urls import path
from . import views
app_name = 'comments'
urlpatterns = [
path('post-comment/', views.post_comment, name='post_comment'),
]
修改mysite/urls.py:
path('comment/', include('commen.urls', namespace='commen')),
然后新建templates/comment文件夹 再创建templates/comment/comment.html并写入:
{% extends "base.html" %}
{% load static %}
{% block title %}
comments
{% endblock title %}
{% block content %}
{% if user.is_authenticated %}
<div class="container">
<br>
<div>
<form action="." method="post">
{% csrf_token %}
<div class="form-group">
<label for="body">
<strong>
Post a comment:
</strong>
</label>
<textarea maxlength="100" type="text" class="form-control" id="body" name="body" rows="2"></textarea>
<strong> </strong><hr>
<span onclick="add('\u263A')" width="22" height="22">☺</span>
<span onclick="add('\u263B')" width="22" height="22">☻</span>
<span onclick="add('\u273F')" width="22" height="22">✿</span>
<span onclick="add('\u2764')" width="22" height="22">❤</span>
<span onclick="add('\uFFFD')" width="22" height="22">�</span>
<span onclick="add('\u26A1')" width="22" height="22">⚡</span>
<span onclick="add('\u266B')" width="22" height="22">♫</span>
<span onclick="add('\u2693')" width="22" height="22">⚓</span>
<span onclick="add('\u2713')" width="22" height="22">✓</span><br>
<span onclick="add('\u2648')" width="22" height="22">♈</span>
<span onclick="add('\u2649')" width="22" height="22">♉</span>
<span onclick="add('\u2651')" width="22" height="22">♑</span>
<span onclick="add('\u272A')" width="22" height="22">✪</span>
<span onclick="add('\u2605')" width="22" height="22">★</span>
<span onclick="add('\u265B')" width="22" height="22">♛</span>
<span onclick="add('\u2658')" width="22" height="22">♘</span>
<span onclick="add('\u265A')" width="22" height="22">♚</span>
</div>
<button type="submit" class="btn btn-primary">send</button>
</form>
</div>
<br>
<div>
{% for comment in comments %}
<hr>
<p>
<span style="color: green">
{{ comment.created|date:"Y-m-d H:i:s" }}
</span>
<pre style="font-family: inherit; font-size: 1em;white-space: pre-wrap;word-wrap: break-word;">{{ comment.body|safe }}</pre>
</p>
{% endfor %}
</div>
</div>
{% else %}
<br>
<h5 class="row justify-content-center">
please<a href="{% url 'userprofile:login' %}">Login</a>
</h5>
<br>
{% endif%}
<script>
function add(value){
document.getElementById('body').value += (value);
}
</script>
{% endblock content %}
修改header.html:
...
<style>
.material-icons.md-18 { font-size: 18px; }
.material-icons.md-blue { color: #66ccff; }
</style>
<style>
#divcss2 img{ border-radius:23%}
</style>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div id="divcss2"><img src="/img/my_ico.jpg" height="36" width="36"></div>
<a class="navbar-brand" href="#">My Site</a>
<div class="container">
<a class="navbar-brand" href="#"></a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/"><i class="material-icons md-18 md-blue">home</i>Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'article:article_list' %}"><i class="material-icons md-18">book</i>Article</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'article:article_create' %}">New</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'commen:post_comment' %}">comment</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item dropdown">
...
</ul>
</div>
</div>
</nav>
再修改headerr.html:
...
<style>
.material-icons.md-18 { font-size: 18px; }
.material-icons.md-blue { color: #66ccff; }
</style>
<style>
#divcss2 img{ border-radius:23%}
</style>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div id="divcss2"><img src="/img/my_ico.jpg" height="36" width="36"></div>
<a class="navbar-brand" href="#">My Site</a>
<div class="container">
<a class="navbar-brand" href="#"></a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'article:article_list' %}"><i class="material-icons md-18">book</i>Article</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'commen:post_comment' %}">comment</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ user.username }}
</a>
...
</ul>
</div>
</div>
</nav>
最后运行服务器(python manage.py runserver)后可以查看效果。 进入到 manage.py同级目录下输入:
python manage.py makemigrations
然后迁移到数据库中
python manage.py migrate