貌似Django從版本1.6開始就放棄了對自帶的comments的使用,具體原因未查,但是現在使用Django的內部的模組也可以實現評論功能,那就是藉助於forms模組,下面是我的一個小例子。
環境準備
•作業系統: windows 7 64 位元旗艦版
•IDE:PyCharm 2016.1
•Python :2.7.11
•Django :1.9.6
from __future__ import unicode_literals from django.contrib import admin from django.db import models from django import forms # Create your models here. TOPIC_CHOICES = ( ('level1','Bad'), ('level2','SoSo'), ('level3','Good'), ) class RemarkForm(forms.Form): subject = forms.CharField(max_length=100,label='Mark Board') mail = forms.EmailField(label='email') topic = forms.ChoiceField(choices=TOPIC_CHOICES,label='choose one topic') message = forms.CharField(label='content for mark',widget=forms.Textarea) cc_myself = forms.BooleanField(required=False,label='watch this tie') class Remark(models.Model): subject = models.CharField(max_length=100) mail = models.EmailField() topic = models.CharField(max_length=100) message = models.CharField(max_length=300) cc_myself = models.BooleanField() def __unicode__(self): return self.subject class Meta: ordering = ['subject'] admin.site.register([Remark,])
url映射檔案urls.py
"""FormRelative URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.9/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url from django.contrib import admin from app.views import * urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^remark/$',reamark), ] 视图层views.py
from django.shortcuts import render from app.models import * from django.http import * # Create your views here. # subject = models.CharField(max_length=100) # mail = models.EmailField() # topic = models.CharField(max_length=100) # message = models.CharField(max_length=300) # cc_myself = models.BooleanField() def reamark(request): if request.method =="POST": form = RemarkForm(request.POST) if form.is_valid(): myremark = Remark() myremark.subject=form.cleaned_data['subject'] myremark.mail = form.cleaned_data['mail'] myremark.topic = form.cleaned_data['topic'] myremark.message = form.cleaned_data['message'] myremark.cc_myself = form.cleaned_data['cc_myself'] myremark.save() # return HttpResponse("Publish Success!") else: form = RemarkForm() ctx = { 'form':form, 'ties':Remark.objects.all() } return render(request,'message.html',ctx)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="." method="post"> {% for field in form %} {% csrf_token %} <div> {{ field.label_tag }}:{{ field }} {{ field.errors }} </div> {% endfor %} <div> <input type="submit" value="Remark"> </div> </form> <hr> {% for tie in ties %} <div> <ul> <li>{{ tie.subject }}</li> <li>{{ tie.mail}}</li> <li>{{ tie.topic}}</li> <li>{{ tie.message }}</li> <li>{{ tie.cc_myself }}</li> </ul> <hr> </div> {% endfor%} </body> </html>
初始化資料庫
# Database # https://docs.djangoproject.com/en/1.9/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
這裡我們在瀏覽器下輸入
127.0.0.1:8000/admin就可以看到下面
然後輸入127.0.0.1:8000/
這樣,除了沒有美化介面,其餘的都完成了呢。
這裡雖然是個很簡單的小例子,但是我也從中發現了自己的一些概念上的問題,比如說對於模型設計的不合理,因為沒有評論時間,這就顯得很尷尬了。
然後是
// 创建数据库表结构 python manage.py makemigrations python manage.py migrate // 按照提示进行操作即可,目的是为了创建一个超级管理员 python createsuperuser //在自带的开发服务器上运行我们的项目 python manage.py runserver
好了,今天就介紹到這裡吧,由於本人能力一般,程式碼或邏輯有錯的地方,歡迎大家批評指正!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持PHP中文網。 更多Python Django使用forms來實現評論功能相關文章請關注PHP中文網!