用Django写出以下项目代码。 项目介绍 云笔记项目: 用户可以在该系统注册、登录,登录后可以在里面写日记,包括对日记的增删改查 需求分解: 模块1:用户模块 功能:用户注册、用户登录 字段:用户名、密码 界面:一般的简单注册登录界面即可 模块2 :云笔记模块 功能:展示首页、对笔记进行增删改查 字段:标题、内容、新增时间、修改时间 界面: 首页要求未登录状态显示登录和注册按钮,已登录状态显示欢迎用户以及查看笔记按钮; 笔记列表界面要求用表格展示所有笔记,并且每个笔记包含修改和删除按钮,界面顶部显示欢迎用户以及新增笔记按钮; 新增和修改界面要求包含标题、内容以及提交按钮; 以上数据,用静态字典即可,
时间: 2023-05-31 16:06:07 浏览: 58
不需要使用数据库存储。
代码实现如下:
# views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
notes = []
note_id = 0
def index(request):
if request.user.is_authenticated:
return render(request, 'notes.html', {'notes': notes})
else:
return render(request, 'index.html')
@login_required
def add_note(request):
global note_id
if request.method == 'POST':
title = request.POST['title']
content = request.POST['content']
notes.append({'id': note_id, 'title': title, 'content': content})
note_id += 1
return redirect('index')
return render(request, 'add_note.html')
@login_required
def edit_note(request, note_id):
note = next((note for note in notes if note['id'] == note_id), None)
if request.method == 'POST':
note['title'] = request.POST['title']
note['content'] = request.POST['content']
return redirect('index')
return render(request, 'edit_note.html', {'note': note})
@login_required
def delete_note(request, note_id):
note = next((note for note in notes if note['id'] == note_id), None)
notes.remove(note)
return redirect('index')
def user_login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('index')
else:
return render(request, 'login.html', {'error': 'Invalid login credentials'})
return render(request, 'login.html')
@login_required
def user_logout(request):
logout(request)
return redirect('index')
def register(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = User.objects.create_user(username=username, password=password)
login(request, user)
return redirect('index')
return render(request, 'register.html')
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('add_note/', views.add_note, name='add_note'),
path('edit_note/<int:note_id>/', views.edit_note, name='edit_note'),
path('delete_note/<int:note_id>/', views.delete_note, name='delete_note'),
path('login/', views.user_login, name='login'),
path('logout/', views.user_logout, name='logout'),
path('register/', views.register, name='register'),
]
# templates
# base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %} {% endblock %}</title>
</head>
<body>
{% if user.is_authenticated %}
<p>Welcome {{ user.username }}!</p>
{% else %}
<a href="{% url 'login' %}">Login</a>
<a href="{% url 'register' %}">Register</a>
{% endif %}
{% block content %}
{% endblock %}
</body>
</html>
# index.html
{% extends 'base.html' %}
{% block title %}Home{% endblock %}
{% block content %}
<h1>Notes</h1>
<a href="{% url 'add_note' %}">Add Note</a>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Created</th>
<th>Last Modified</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for note in notes %}
<tr>
<td>{{ note.title }}</td>
<td>{{ note.content }}</td>
<td>{{ note.created }}</td>
<td>{{ note.modified }}</td>
<td>
<a href="{% url 'edit_note' note.id %}">Edit</a>
<a href="{% url 'delete_note' note.id %}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
# add_note.html
{% extends 'base.html' %}
{% block title %}Add Note{% endblock %}
{% block content %}
<h1>Add Note</h1>
<form method="post">
{% csrf_token %}
<label for="title">Title</label>
<input type="text" name="title" required><br><br>
<label for="content">Content</label>
<textarea name="content" required></textarea><br><br>
<input type="submit" value="Save">
</form>
{% endblock %}
# edit_note.html
{% extends 'base.html' %}
{% block title %}Edit Note{% endblock %}
{% block content %}
<h1>Edit Note</h1>
<form method="post">
{% csrf_token %}
<label for="title">Title</label>
<input type="text" name="title" value="{{ note.title }}" required><br><br>
<label for="content">Content</label>
<textarea name="content" required>{{ note.content }}</textarea><br><br>
<input type="submit" value="Save">
</form>
{% endblock %}
# login.html
{% extends 'base.html' %}
{% block title %}Login{% endblock %}
{% block content %}
<h1>Login</h1>
{% if error %}
<p>{{ error }}</p>
{% endif %}
<form method="post">
{% csrf_token %}
<label for="username">Username</label>
<input type="text" name="username" required><br><br>
<label for="password">Password</label>
<input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
{% endblock %}
# register.html
{% extends 'base.html' %}
{% block title %}Register{% endblock %}
{% block content %}
<h1>Register</h1>
<form method="post">
{% csrf_token %}
<label for="username">Username</label>
<input type="text" name="username" required><br><br>
<label for="password">Password</label>
<input type="password" name="password" required><br><br>
<input type="submit" value="Register">
</form>
{% endblock %}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)