|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 kingnok 于 2022-11-9 23:13 编辑
问题描述:学习用django,但是在学习的过程中出现一个问题,就是从一个网页跳到另外一个网页(从get_students.html跳转到edit,不显示网页界面,显示的是网页的代码。而且中文部分还是乱码,请大神指导一下,谢谢了~~,代码如下
URLS.py代码:
"""django_demo2 URL Configuration
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01.views import classes
from app01.views import teachers
from app01.views import students
urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html',classes.get_classes),
path('add_classes.html',classes.add_classes),
path('del_classes.html',classes.del_classes),
path('edit_classes.html',classes.edit_classes),
path('students.html',students.get_students),
path('add_students.html',students.add_students),
path('del_students.html',students.del_students),
path('edit_students.html',students.edit_students),
]
就是这段代码不现实网页
edit_students.html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生修改</title>
</head>
<body>
<form action="edit_students.html?nid={{ obj.id }}" method="POST">
{% csrf_token %}
<p><input type="text" name="nid" value="{{ obj.id }}" style="display:none"></p>
<p><input type="text" name="username" value="{{ obj.username }}"></p>
<p><input type="text" name="age" value="{{ obj.age }}"></p>
<p>
{% if obj.gender %}
男:<input type="radio" name="gender" checked="checked" value="1">
女:<input type="radio" name="gender" value="0">
{% elif obj.gender %}
男:<input type="radio" name="gender" value="0">
女:<input type="radio" name="gender" checked="checked" value="1">
{% endif %}
</p>
<P>
<select name="class_id">
{% for row in cs_list %}
{% if row.id == obj.cs_id %}
<option value="{{ row.id }}" selected="selected">{{ row.title }}</option>
{% else %}
<option value="row.id">{{ row.title }}</option>
{% endif %}
{% endfor %}
</select>
</P>
<input type="submit" value="提交">
</form>
</body>
</html>
get_students.html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<a href="/add_students.html">添加</a>
</div>
<div>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>班级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for row in stu_list %}
<tr>
<td>
{{ row.id }}
</td>
<td>
{{ row.username }}
</td>
<td>
{{ row.age }}
</td>
<td>
{{ row.gender }}
</td>
<td>
{{ row.cs.title }}
</td>
<td>
<a href="del_students.html?nid={{ row.id }}">删除</a>
<a href="edit_students.html?nid={{ row.id }}">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
from django.db import models
models.py代码:
# Create your models here.
class Classes(models.Model):
'''
班级表
'''
title = models.CharField(max_length=32)
m = models.ManyToManyField('Teachers')
class Teachers(models.Model):
'''
老师表
'''
name=models.CharField(max_length=32)
class C2T(models.Model):
'''
班级-教师对应表
'''
cid=models.ForeignKey(Classes, on_delete=models.CASCADE)
tid=models.ForeignKey(Teachers, on_delete=models.CASCADE)
class Student(models.Model):
username=models.CharField(max_length=32)
age= models.IntegerField()
gender=models.BooleanField()
cs= models.ForeignKey(Classes,on_delete=models.CASCADE)
students.py代码:
from django.shortcuts import render
from django.shortcuts import redirect
from app01 import models
def get_students(request):
stu_list=models.Student.objects.all()
for row in stu_list:
print(row.id,row.username,row.cs.title,row.cs.id,)
return render(request,'get_students.html',{'stu_list':stu_list})
def add_students(request):
if request.method=="GET":
cs_list = models.Classes.objects.all()
return render(request,'add_students.html',{'cs_list':cs_list})
elif request.method=="POST":
u = request.POST.get("username")
a = request.POST.get("age")
g = request.POST.get("gender")
c = request.POST.get("cs")
models.Student.objects.create(username=u,age=a,gender=g,cs_id=c)
return redirect('students.html')
def del_students(request):
nid = request.GET.get('nid')
models.Student.objects.filter(id=nid).delete()
return redirect('students.html')
def edit_students(request):
nid = request.GET.get('nid')
obj = models.Student.objects.filter(id=nid).first()
print(obj.id)
cs_list = models.Classes.objects.all()
return render(request, 'edit_students.html', {'obj':obj}, {'cs_list':cs_list})
def edit_students(request):
nid = request.GET.get('nid')
obj = models.Student.objects.filter(id=nid).first()
print(obj.id)
cs_list = models.Classes.objects.all()
# return render(request, 'edit_students.html', {'obj':obj}, {'cs_list':cs_list})
# 写成这样,导致render的content_type参数接收到了一个字典,而不是默认的正确的 'text/html' MIME文档类型,从而导致 网页渲染失败(类型不对,不认识!)
# 修改为:
return render(request, 'edit_students.html', context={'obj':obj, 'cs_list':cs_list})
|
|