django实战项目例子
时间: 2023-12-16 13:26:37 浏览: 110
Django项目实战
为您介绍一个基于Django的实战项目:一个简单的博客系统。该博客系统具有以下功能:
1.用户可以注册、登录、注销账户。
2.用户可以创建、编辑、删除博客文章。
3.用户可以查看其他用户发布的博客文章。
4.博客文章可以按照发布时间或者浏览量进行排序。
5.用户可以对博客文章进行评论。
6.用户可以对其他用户的评论进行回复。
以下是该博客系统的实现步骤:
1.创建Django项目
```shell
django-admin startproject myblog
```
2.创建Django应用
```shell
python manage.py startapp blog
```
3.定义模型
```python
from django.db import models
from django.contrib.auth.models import User
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
created_time = models.DateTimeField(auto_now_add=True)
modified_time = models.DateTimeField(auto_now=True)
views = models.PositiveIntegerField(default=0)
class Comment(models.Model):
content = models.TextField()
article = models.ForeignKey(Article, on_delete=models.CASCADE)
commentator = models.ForeignKey(User, on_delete=models.CASCADE)
created_time = models.DateTimeField(auto_now_add=True)
parent_comment = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, related_name='children')
```
4.创建数据库表
```shell
python manage.py makemigrations
python manage.py migrate
```
5.编写视图函数
```python
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Article, Comment
from .forms import CommentForm
def index(request):
article_list = Article.objects.all().order_by('-created_time')
return render(request, 'blog/index.html', context={'article_list': article_list})
def detail(request, article_id):
article = get_object_or_404(Article, pk=article_id)
article.views += 1
article.save(update_fields=['views'])
form = CommentForm()
comment_list = Comment.objects.filter(article=article)
context = {'article': article,
'form': form,
'comment_list': comment_list
}
return render(request, 'blog/detail.html', context=context)
def archives(request, year, month):
article_list = Article.objects.filter(created_time__year=year,
created_time__month=month
).order_by('-created_time')
return render(request, 'blog/index.html', context={'article_list': article_list})
def category(request, category_id):
pass
def search(request):
pass
def login(request):
pass
def register(request):
pass
def logout(request):
pass
```
6.编写模板文件
```html
<!-- index.html -->
{% for article in article_list %}
<h2><a href="{% url 'blog:detail' article.id %}">{{ article.title }}</a></h2>
<p class="date">{{ article.created_time }}</p>
<p>{{ article.content|truncatechars:200 }}</p>
{% endfor %}
<!-- detail.html -->
<h2>{{ article.title }}</h2>
<p class="date">{{ article.created_time }}</p>
<p>{{ article.content }}</p>
<h3>评论</h3>
<form method="post" action="{% url 'blog:comment' article.id %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">发表评论</button>
</form>
{% for comment in comment_list %}
<div class="comment">
<p>{{ comment.content }}</p>
<p class="date">{{ comment.created_time }}</p>
<p class="commentator">{{ comment.commentator }}</p>
<a href="#comment-form">回复</a>
{% for child_comment in comment.children.all %}
<div class="child-comment">
<p>{{ child_comment.content }}</p>
<p class="date">{{ child_comment.created_time }}</p>
<p class="commentator">{{ child_comment.commentator }}</p>
<a href="#comment-form">回复</a>
</div>
{% endfor %}
</div>
{% endfor %}
<!-- base.html -->
{% block content %}
{% endblock %}
```
7.编写表单
```python
from django import forms
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['content']
```
8.配置URL
```python
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.index, name='index'),
path('article/<int:article_id>/', views.detail, name='detail'),
path('archives/<int:year>/<int:month>/', views.archives, name='archives'),
path('category/<int:category_id>/', views.category, name='category'),
path('search/', views.search, name='search'),
path('comment/<int:article_id>/', views.comment, name='comment'),
path('login/', views.login, name='login'),
path('register/', views.register, name='register'),
path('logout/', views.logout, name='logout'),
]
```
阅读全文