1. 创建Django项目和应用
```
$ django-admin startproject notebook
$ cd notebook
$ python manage.py startapp notes
```
2. 定义数据模型
在`notes/models.py`中定义数据模型,包括用户和笔记两个模型,并添加相应的字段。
``` Python
from django.db import models
from django.contrib.auth.models import User
class Note(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
def __str__(self):
return self.user.username
```
3. 创建表并添加数据
使用Django的命令行工具创建数据库表和超级用户,并添加一些初始的用户和笔记。
```
$ python manage.py migrate
$ python manage.py createsuperuser
$ python manage.py shell
from django.contrib.auth.models import User
from notes.models import Note
User.objects.create_superuser('admin', '', 'admin123')
User.objects.create_user('user1', '', 'test123')
User.objects.create_user('user2', '', 'test123')
user1 = User.objects.get(username='user1')
user2 = User.objects.get(username='user2')
Note.objects.create(title='Note 1', content='Content 1', user=user1)
Note.objects.create(title='Note 2', content='Content 2', user=user1)
Note.objects.create(title='Note 3', content='Content 3', user=user2)
```
4. 创建视图和模板
创建`notes/views.py`文件,并添加用户注册、登录、注销和笔记增删改查等视图函数,使用Django自带的表单和模板系统。
``` Python
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import login, logout
from django.contrib.auth.decorators import login_required
from notes.models import Note
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('notes:list')
else:
form = UserCreationForm()
return render(request, 'notes/register.html', {'form': form})
def login_view(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = form.get_user()
login(request, user)
return redirect('notes:list')
else:
form = AuthenticationForm()
return render(request, 'notes/login.html', {'form': form})
@login_required
def logout_view(request):
logout(request)
return redirect('notes:list')
@login_required
def note_list(request):
notes = Note.objects.filter(user=request.user)
return render(request, 'notes/list.html', {'notes': notes})
@login_required
def note_detail(request, pk):
note = Note.objects.get(user=request.user, pk=pk)
return render(request, 'notes/detail.html', {'note': note})
@login_required
def note_create(request):
if request.method == 'POST':
note = Note.objects.create(user=request.user, **request.POST)
return redirect('notes:detail', pk=note.pk)
else:
return render(request, 'notes/create.html')
@login_required
def note_update(request, pk):
note = Note.objects.get(user=request.user, pk=pk)
if request.method == 'POST':
note.title = request.POST['title']
note.content = request.POST['content']
note.save()
return redirect('notes:detail', pk=note.pk)
else:
return render(request, 'notes/update.html', {'note': note})
@login_required
def note_delete(request, pk):
note = Note.objects.get(user=request.user, pk=pk)
note.delete()
return redirect('notes:list')
```
创建`notes/templates`文件夹,并在其中创建用户注册、登录、注销和笔记增删改查等模板。
```
notes/
templates/
notes/
base.html
register.html
login.html
list.html
detail.html
create.html
update.html
```
5. 配置URL路由
在`notes/urls.py`中配置URL路由,包括用户注册、登录、注销和笔记增删改查等URL。
``` Python
from django.urls import path
from notes.views import *
app_name = 'notes'
urlpatterns = [
path('register/', register, name='register'),
path('login/', login_view, name='login'),
path('logout/', logout_view, name='logout'),
path('', note_list, name='list'),
path('<int:pk>/', note_detail, name='detail'),
path('create/', note_create, name='create'),
path('<int:pk>/update/', note_update, name='update'),
path('<int:pk>/delete/', note_delete, name='delete'),
]
```
在`notebook/urls.py`中包含`notes`应用的URL。
``` Python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('notes/', include('notes.urls')),
]
```
6. 运行应用
使用Django内置的开发服务器运行应用。
```
$ python manage.py runserver
```
在浏览器中访问`http://127.0.0.1:8000/notes/`,应该会看到现在用户可以在该系统注册、登录,登录后可以在里面写日记,包括对日记的增删改查的功能。