Django 实现用户简单的增删

Django 实现用户简单的增删改

小生初学django,涉世未深,学着写了一个用户的增删改,还望各位大神指点.话不多说,直接上代码.

我用的是Pycharm 编写的.

1.首先创建一个项目,命名为mysite.

	django-admin startproject mystie 

2.在该项目下创建一个App, 以 student 为例.

	python3 manage.py startapp student.

3.在项目mysite中配置setting

	INSTALLED_APPS = [
	    'django.contrib.admin',
	    'django.contrib.auth',
	    'django.contrib.contenttypes',
	    'django.contrib.sessions',
	    'django.contrib.messages',
	    'django.contrib.staticfiles',
	    'student.apps.StudentConfig',
	    
	]
	

**在setting中添加设置static, 我是在最后加的.**

	STATICFILES_DIRS = [
	   os.path.join(BASE_DIR, 'static'),
	]

4.在mysite中配置 urls

	from django.contrib import admin
	from django.urls import path, include
	
	urlpatterns = [
	    path('stu/', include('student.urls')),
	    path('admin/', admin.site.urls),
	]

5.在Student App中models中创建一个Student模型类

	from django.db import models

	class Student(models.Model):
	    name = models.CharField(max_length=20)
	    age = models.IntegerField(default=18)
	    score = models.IntegerField(default=0)
	    gender = models.CharField(max_length=6)
	
	    def __str__(self):
	        return "%s, %d岁" % (self.name, self.age)

6.创建好models类后,并进行数据库迁移

	python3 manage.py makemigrations
	python3 manage.py migrate

7.创建Student 的路由urls , 并对其进行增删改的配置

from django.urls import path
from . import views

	urlpatterns = [
	    path('list', views.index),
	    path('add', views.add),	
	    path('del', views.del_stu),
	    path('save', views.save)
	]

8.编写views 视图函数

from django.shortcuts import render
from student.models import Student


**Create your views here.**
def index(request):
    stu_list = Student.objects.all()
    return render(request, 'stu.html', {'data': stu_list})

 
 ** add student**
 
def add(request):
    if request.method == "POST":
        # 获取学生信息
        name = request.POST.get("username")
        gender = request.POST.get('gender')
        age = int(request.POST.get('age'))
        score = int(request.POST.get("score"))
        stu = Student(name=name, gender=gender, age=age, score=score)
        stu.save()
    return index(request)


** del student **

def del_stu(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        stu = Student.objects.filter(id=id)
        # 删除学生
        stu.delete()
    return index(request)


**save student **

def save(request):
    if request.method == "POST":
        name = request.POST.get('username')
        gender = request.POST.get("gender")
        age = int(request.POST.get("age"))
        score = int(request.POST.get("score"))
        id = int(request.POST.get("id"))
        stu = Student.objects.get(id=id)
        stu.name = name
        stu.age = age
        stu.gender = gender
        stu.score = score
        stu.save()
    return index(request)

9. 在Pycharm中启动

命令行的方式是:

python3 manage.py runserver 

Django 实现用户简单的增删

10.启动运行web服务

命令行的方式是:

http://127.0.0.1:8000/stu/add

运行结果展示

Django 实现用户简单的增删

结束
以上就是Django 增删改的全部内容, 欢迎各位大佬进行指导,以后会给大家呈现更加完善的素材.
新手上路,多多关照!!!
Thanks