python基于html的校园网设计与实现(django+mysql)
由于这个问题涉及到的内容较多,且涉及到作业的完整性,我将提供一个基于Django框架的简单网站的创建过程,这个过程可以作为您的计算机毕设的一部分。
首先,您需要安装Django和MySQL。以下是安装命令:
pip install django
pip install mysqlclient
然后,您需要创建一个新的Django项目和应用:
django-admin startproject myschoolproject
cd myschoolproject
python manage.py startapp myschoolapp
接下来,您需要配置settings.py
来使用MySQL数据库:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myschooldb',
'USER': 'myschooluser',
'PASSWORD': 'myschoolpassword',
'HOST': 'localhost',
'PORT': '3306',
}
}
INSTALLED_APPS = [
'myschoolapp',
# ...
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
然后,您需要创建一些模型和视图:
# models.py
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
roll_no = models.IntegerField()
# ...
class Teacher(models.Model):
name = models.CharField(max_length=100)
# ...
# views.py
from django.shortcuts import render
from .models import Student, Teacher
def home(request):
students = Student.objects.all()
teachers = Teacher.objects.all()
return render(request, 'home.html', {'students': students, 'teachers': teachers})
接下来,您需要创建HTML模板:
<!-- home.html -->
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome to My School</h1>
<h2>Students</h2>
<ul>
{% for student in students %}
<li>{{ student.name }} - Roll No: {{ student.roll_no }}</li>
{% endfor %}
</ul>
<h2>Teachers</h2>
<ul>
{% for teacher in teachers %}
<li>{{ teacher.name }}</li>
{% e
评论已关闭