Writing in the front

In python class, the teacher required the use of Python web framework to implement the student management system. After searching for information, he finally chose to use Django to build the system

Django

Django is an open source Web application framework written in Python. Django, which complies with the BSD copyright, was first released in July 2005, and its first official version 1.0 was released in September 2008. Django uses the MVC software design pattern, model M, View V, and Controller C.

The MVC pattern

Model – A Model represents an object or JAVA POJO that accesses data. It can also have logic to update the controller as the data changes. View – A View represents a visualization of the data contained in the model. Controller (Controller) – Controllers act on models and views. It controls the flow of data to model objects and updates the view as the data changes. It separates the view from the model.

Student management system functions

2. Common user: can only query related information. 3. Root user: can add student information. Student learning includes: name, student id, address, college, grades ### Core code part manage.py this is the server management

#! /usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE'.'studentsSystem.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()
Copy the code

Configure local database information

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql'.'NAME': '* * * * * *'.# database name
        'USER': 'root'.# database name
        'PASSWORD': '* * * * * *'.# database password
        'HOST': 'localhost'.# indicates local access
        'PORT': '3306'.The port number will be configured when the database is installed}}Copy the code

Login interface code – write it in view.py

# Login interface
def login(request):
    if request.method == "POST":
        id = request.POST.get('id')
        username = request.POST.get('username')
        password = request.POST.get('password')
        if not all([id, username, password]):
            return HttpResponse('Parameter incomplete')
        else:
            student = StudentModel.objects.filter(username=username, password=password)
            if len(student):
                # request.session['username'] = username
                Use the following method to store user information into a session, which is enabled by default in middleware
                request.session['user'] = {
                    'id': id,
                    'username': username,
                    'password': password
                }
                context = {
                    'status': username,
                    'aa': 'Logged in'.'lenght': 1}return render(request, 'studentManage/index.html', context)

            else:
                context = {
                    'aa': 'Wrong username and password'
                }
                return render(request, 'studentManage/login.html', context)
    else:
        context = {
            'status': 'Not logged in'.'length': 0}return render(request, 'studentManage/login.html', context)
Copy the code

Student information sheet – written in models.py

# Student information sheet
class StudentInformationModel(models.Model):
    stu_id = models.CharField(max_length=15, verbose_name='student ID')
    stu_name = models.CharField(max_length=30, verbose_name='Student name')
    stu_phone = models.CharField(max_length=20, verbose_name='Student Phone')
    str_addr = models.TextField(verbose_name='Student Address')
    stu_faculty = models.CharField(max_length=20, verbose_name='departments')
    stu_major = models.CharField(max_length=30, verbose_name='professional')
    # Cancel foreign keys (foreign keys are available)
    # stu_course = models.ForeignKey('CourseModel', on_delete=True)
    class Meta():
        db_table = 'studentinformation'
Copy the code

Distribution of the url

urlpatterns = [
    path('admin/', admin.site.urls),
    path('student/', include('studentsApp.urls', namespace='student')),]Copy the code
app_name = 'studentsApp'
urlpatterns = [
    path('index/', views.index, name='index'),
    path('login/', views.login, name='login'),
    path('logout/', views.logout, name='logout'),
    path('add/', views.add, name='add'),
    path('select/', views.select, name='select'),
    path('delete/', views.delete, name='delete'),
    path('update/', views.update, name='update')]Copy the code

Results show

1. Start the service: python manage.py runServer

2. Access the system:http://127.0.0.1:8000/student/index

3. Log in to the system

4. Select an operation

5. Check student information

6. Modify student information

7. Background management interface

8. Student information management

The detailed code has been uploaded to Github

Github.com/ljr7822/iwe…

Write in the back

Although the program is well written, there are still some places to optimize, such as the front end to add student information can be used to import forms to add in batches, there is the front end page is too simple. Hopefully, I’ll write a good demo later