urls.py 文件中,urlpatterns已经是一个 path() 或者 re_path() 的列表,怎么看是否有 None 的配置
时间: 2023-06-16 20:06:29 浏览: 39
在 urlpatterns 列表中,如果某个 path() 或者 re_path() 的参数中没有设置 view 参数,则它的值为 None。例如:
```python
urlpatterns = [
path('example/', None),
re_path(r'^example2/$', None),
]
```
上述代码中,'example/' 和 '^example2/$' 对应的视图函数为 None。这样配置是不正确的,如果访问对应的 URL 就会出现错误。正确的做法是需要给每个 path() 或者 re_path() 设置一个视图函数。
相关问题
sql注入自动化.py:None (sql注入自动化.py) sql注入自动化.py:31: in <module> inject_urls = get_urls() sql注入自动化.py:21: in get_urls with open(fuzz_file, "r") as f: E TypeError: expected str, bytes or os.PathLike object, not NoneType
这个错误的意思是在 `sql注入自动化.py` 文件的第 31 行,调用了 `get_urls()` 函数,但是函数内部的 `fuzz_file` 参数是 `None`,导致在第 21 行无法打开这个文件。你需要检查一下 `get_urls()` 函数的实现,看看为什么 `fuzz_file` 参数会是 `None`,或者在调用 `get_urls()` 函数时确认参数传递正确。
在Django Rest Framework中,models.py中有TestPaper模型中三个字段分别是是:用”exam“于连接Exam模型的外键;用”student“于连接Student模型的外键;“score”用来记录分数的IntegerField。Exam模型中有一个字段“responses_number”用来记录最大作答次数。现在你需要在views.py中写入一个功能为,若作答超过最大作答次数,则选取该学生在此次考试中数值最大的“score”进行记录。并配置路由和请求方法
在views.py中可以写一个函数,实现题目中的功能,然后在路由中配置请求方法。
例如:
```python
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import TestPaper, Exam, Student
@api_view(['POST'])
def record_score(request):
# 获取请求参数
exam_id = request.data.get('exam_id')
student_id = request.data.get('student_id')
# 查询考试和学生信息
exam = Exam.objects.get(id=exam_id)
student = Student.objects.get(id=student_id)
# 查询该学生在此次考试中已经作答的次数
answered_times = TestPaper.objects.filter(exam=exam, student=student).count()
# 如果作答次数小于最大作答次数,则直接记录分数
if answered_times < exam.responses_number:
score = request.data.get('score')
TestPaper.objects.create(exam=exam, student=student, score=score)
else:
# 否则,选取该学生在此次考试中数值最大的“score”进行记录
max_score = TestPaper.objects.filter(exam=exam, student=student).order_by('-score').first()
# 记录分数
score = request.data.get('score')
if max_score is None or score > max_score.score:
TestPaper.objects.create(exam=exam, student=student, score=score)
return Response({'result': 'success'})
```
在路由中配置请求方法为POST:
```python
from django.urls import path
from .views import record_score
urlpatterns = [
path('record_score/', record_score, name='record_score'),
]
```
这样就实现了题目中的功能,可以通过向`/record_score/`发送POST请求来记录分数。请求体需要包含`exam_id`、`student_id`和`score`三个参数。