我正在尝试过滤我的视图,以便在选择特定学校时,将学校的pk放置在名为major的视图函数中,该函数将进一步查询db以显示与之相对应的适当专业学校。
我现在得到一个页面,找不到404错误,并且找不到原因。
url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:Major>/', views.Major, name='Major')
]
models.py
from django.db import models
class Major(models.Model):
name = models.CharField(max_length=30, db_index=True)
class School(models.Model):
name = models.CharField(max_length=50, db_index=True)
school_Major_merge = models.ManyToManyField(Major, through='School_Major')
class School_Major(models.Model):
major = models.ForeignKey(Major, on_delete=models.CASCADE)
school = models.ForeignKey(School, on_delete=models.CASCADE)
class professor(models.Model):
ProfessorIDS = models.IntegerField()
ProfessorName = models.CharField(max_length=100)
ProfessorRating = models.DecimalField(decimal_places=2,max_digits=4)
NumberofRatings = models.CharField(max_length=50)
#delete major from the model
school = models.ForeignKey(School , on_delete=models.CASCADE)
major = models.ForeignKey(Major , on_delete=models.CASCADE)
def __str__(self):
return self.ProfessorName
views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import professor, School, Major, School_Major
def index(request):
schools = School.objects.all()
return render(request, 'locate/index.html', {'schools': schools})
def Major(request, school_pk):
#Filter to a show the association of 1 schools majors
school_choice = Major_School.objects.filter(school_id = school_pk)
#Filter majors names required
majors = Major.objects.filter(id = school_choice.major_id)
return render(request, 'locate/major.html', {'majors' : majors})
我将发布索引文件的代码,该文件将学校信息拉到下面,并且在单击学校(超链接集)时,其基本假设是将学校的主键传递到Major函数中,然后进行进一步过滤
<ul>
{% for list in schools %}
<li><a href="{% url 'Major' {{list.id}}">{{list.name}}</a></li>
<br><br>
{%endfor%}
</ul>
学校的主键已正确拉出,但由于某些原因,当我单击学校的超链接时,该功能无法启动,我收到404页面未找到错误。
主要功能的用途, 1)收到学校的主密钥,该密钥将传递到M2M表。 2)现在仅显示与该学校相对应的专业,仅过滤和存储具有与所选学校相关的主键的专业
我认为我的函数的逻辑是正确的,我只是不明白为什么我会收到404错误。
点击我的第一所学校时,确切的错误是PK = 1
The current path, locate/{% url 'Major' 1/, didn't match any of these.
更新:
我根据以下建议进行了研究,并举例说明了index.html的格式适用于调度程序,并且调度程序也已正确设置,但是WHY OH WHY会{ %仍然弹出,我不明白,我重新启动服务器,以为它可能正在四处寻觅,但是纳达。
index.html
{% for list in schools %}
<li><a href="{% url 'Major' 'list.id' %}">{{list.name}}</a></li>
<br><br>
{%endfor%}
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:school_pk>/', views.Major, name='Major')
]
我遇到的当前路径错误仍然是相同的,但是我不明白的是将模板固定为正确的格式后(我还使用了一个示例项目,该项目作为我的参考没有传递参数的问题)我不仅仅传递/ locate / school_pk,而是传递花括号和%
locate/ [name='index']
locate/ <int:school_pk>/ [name='Major'] <== This one
admin/
register/ [name='register']
profile/ [name='profile']
login/ [name='login']
logout/ [name='logout']
[name='blog-home']
about/ [name='blog-about']
post/<int:pk>/ [name='post-detail']
post/new/ [name='post-create']
post/<int:pk>/update/ [name='post-update']
post/<int:pk>/delete/ [name='post-delete']
user/<str:username> [name='user-posts']
^media/(?P<path>.*)$
The current path, locate/{% url 'Major' 1/, didn't match any of these.
请注意,即使我更正了错误,也已在其中添加了{%。
答案 0 :(得分:2)
我认为模板应该是:
<li><a href="{% url 'Major' list.id %}">{{list.name}}</a></li>
URL如下所示:
path('<int:school_pk>/', views.Major, name='Major')
作为参考,请检查documentation。
答案 1 :(得分:0)
在您的for循环中应该是
<ul>
{% for list in schools %}
<li><a href="{% url 'Major' list.id">{{list.name}}</a></li>
<br><br>
{%endfor%}
</ul>
只需移除双大括号{{ }}
答案 2 :(得分:0)
所以我注意到错误消息中此特定的URL调度程序之间存在空格
locate/ [name='index']
locate/ <int:school_pk>/ [name='Major'] <== This one
admin/
register/ [name='register']
profile/ [name='profile']
login/ [name='login']
logout/ [name='logout']
[name='blog-home']
about/ [name='blog-about']
post/<int:pk>/ [name='post-detail']
post/new/ [name='post-create']
post/<int:pk>/update/ [name='post-update']
post/<int:pk>/delete/ [name='post-delete']
user/<str:username> [name='user-posts']
^media/(?P<path>.*)$
The current path, locate/{% url 'Major' 1/, didn't match any of these.
所以我在url.py上添加了一个空格,看来已经成功了
url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path(' <int:school_pk>/', views.Major, name='Major')
]
我在主项目中使用的所有其他URL都以相同的方式设置,但没有出现此问题,但是由于某些原因,我尝试过的所有内容都不会动摇,我不得不放置一个空格以使其正常工作< / p>