1. 程式人生 > >Django cms專案之models.py,admin.py,forms.py(2)

Django cms專案之models.py,admin.py,forms.py(2)

1.models.py設定

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
(1)自定義User
class NewUser(AbstractUser):
profile = models.CharField('profile', default='', max_length=256)

    def __str__(self):
return self.username
(2)其他
class 
Column(models.Model): name = models.CharField('column_name', max_length=256) intro = models.TextField('introduction', default='') def __str__(self): return self.name class Meta: verbose_name = 'column' verbose_name_plural = 'column' ordering = ['name'] class ArticleManager(models.Manager):
def query_by_column(self, column_id): query = self.get_queryset().filter(column_id=column_id) def query_by_user(self, user_id): user = User.objects.get(id=user_id) article_list = user.article_set.all() return article_list def query_by_polls(self): query = self.get_queryset().order_by('poll_num'
) return query def query_by_time(self): query = self.get_queryset().order_by('-pub_date') return query def query_by_keyword(self, keyword): query = self.get_queryset().filter(title__contains=keyword) return query class Article(models.Model): objectes = ArticleManager() column = models.ForeignKey(Column, blank=True, null=True, verbose_name='belong to') title = models.CharField(max_length=256) author = models.ForeignKey(NewUser) image = models.ImageField(upload_to='img') content = models.TextField('content') pub_date = models.DateTimeField(auto_now_add=True, editable=True) update_time = models.DateTimeField(auto_now=True, null=True) published = models.BooleanField('notDraft', default=True) poll_num = models.IntegerField(default=0) comment_num = models.IntegerField(default=0) keep_num = models.IntegerField(default=0) def __str__(self): return self.title class Meta: verbose_name = 'article' verbose_name_plural = 'article' @property def image_url(self): if self.image and hasattr(self.image, 'url'): return self.image.url
最後的函式是呼叫圖片的路徑,在模板時這樣呼叫
src="{{ article.image_url|default_if_none:'#' }}"
class Comment(models.Model):
user = models.ForeignKey(NewUser, null=True)
    article = models.ForeignKey(Article, null=True)
    content = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True, editable=True)
    poll_num = models.IntegerField(default=0)
    def __str__(self):
return self.content


class Poll(models.Model):
user = models.ForeignKey(NewUser, null=True)
    article = models.ForeignKey(Article, null=True)
    comment = models.ForeignKey(Comment, null=True)

2.admin.py設定(設定在後臺的顯示)

from django.contrib import admin
from django.db import models
from django import forms
from .models import Comment, Article, Column, NewUser

class CommentAdmin(admin.ModelAdmin):
list_display = ('user_id','article_id','pub_date', 'content','poll_num')

class ArticleAdmin(admin.ModelAdmin):
formfield_overrides = {
        models.TextField: {'widget': forms.Textarea(
                           attrs={'rows': 41,
'cols': 100
})},
}
    list_display = ('title','pub_date', 'poll_num')

class NewUserAdmin(admin.ModelAdmin):
list_display = ('username','date_joined', 'profile')

class ColumnAdmin(admin.ModelAdmin):
list_display = ('name', 'intro')


admin.site.register(Comment, CommentAdmin)
admin.site.register(Article, ArticleAdmin)
admin.site.register(Column, ColumnAdmin)
admin.site.register(NewUser, NewUserAdmin)

3.forms.py設定

主要設計了評論和上傳的表單

from django import forms
from django.forms import ModelForm
from .models import Article

class CommentForm(forms.Form):
comment = forms.CharField(label='', widget=forms.Textarea(attrs={'cols': '60', 'rows':'6'} ))


class PostForm(ModelForm):
class Meta:
model = Article
        fields = '__all__'
在模板呼叫時
<form action="/image/{{ article.id }}/comment/" method="post">
    {% csrf_token %}
    {{ commentform.as_p }}
    <input class="btn btn-default" type="submit" value="Submit" />
</form>
別忘記新增{%  csrf_token %}防止跨域請求攻擊的