我正尝试将我的应用程序的URL包含到项目的URL中,如下所述,但似乎有问题,请帮助我找出问题所在。
我的项目名称是trydjango,其中是urls.py
文件
urlpatterns = [
path('teacher/', include('teacher.urls')),
path('admin/', admin.site.urls),
path('', homeView, name='home'),
]
这是teacher.urls
文件
from django.urls import path
from .views import addTeacherView, listTeacherView, teacherIndexView, deleteTeacherView, editTeacherView
app_name = "teacher"
urlpatterns = [
path('addTeacher/', addTeacherView, name='add-teacher'),
path('listTeacher/', listTeacherView, name='list-teacher'),
path('teacherIndex/<int:my_id>/', teacherIndexView, name='teacher-index'),
path('deleteTeacher/<int:my_id>/',
deleteTeacherView, name='delete-teacher-view'),
path('editTeacher/<int:my_id>/', editTeacherView, name='edit-teacher'),
]
在models.py中,我以相反的方式添加了应用名称
class Teacher(models.Model):
firstName = models.CharField(max_length=72)
email = models.EmailField()
salary = models.DecimalField(max_digits=100000, decimal_places=2)
def get_absolute_url(self):
return reverse('teacher:edit-teacher', kwargs={'my_id': self.id})
请帮助我发现问题 预先感谢!
编辑:错误在这里,我的声誉在10以下,它不允许我在此处添加图像!但这是它显示的错误的副本
NoReverseMatch at /
Reverse for 'add-teacher' not found. 'add-teacher' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 2.1.7
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'add-teacher' not found. 'add-teacher' is not a valid view function or pattern name.
Exception Location: C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 622
Python Executable: C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe
Python Version: 3.7.2
Python Path:
['C:\\Users\\Administrator\\Desktop\\123\\src',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']
Server time: Fri, 22 Mar 2019 20:29:59 +0000
Error during template rendering
In template C:\Users\Administrator\Desktop\123\src\teacher\templates\teacher\base.html, error at line 19
Reverse for 'add-teacher' not found. 'add-teacher' is not a valid view function or pattern name.
9 <title>Document</title>
10 <link rel="stylesheet" href="{% static 'style.css' %}">
11 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
12
13 </head>
14
15 <body>
16 <nav>
17 <a class="logo" href="{% url 'home' %}">LOGO</a>
18 <div class="navbar">
19 <a href="{% url 'add-teacher' %}">Add Teacher</a>
20 <a href="{% url 'list-teacher' %}">List Teacher</a>
21 </div>
答案 0 :(得分:1)
我相信您缺少一些url调用的名称空间。在模板中,您有{% url 'add-teacher' %}
。那应该是{% url 'teacher:add-teacher' %}
。寻找老师网址的其他情况。
答案 1 :(得分:1)
我您的模板使用的地址是这样的:
<a href="{% url 'add-teacher' %}">Add Teacher</a>
您必须将这种寻址方式更改为此(由于应用teacher
):
<a href="{% url 'teacher:add-teacher' %}">Add Teacher</a>