当我打开http://127.0.0.1:8000/
的{{1}}时,我得到:
在/处没有NoReverseMatch,对于带有参数'(',)'的'exam-detail'则没有反向。尝试了1个模式:['exam /(?P [^ /] +)$']
我认为该错误是由于home.html
中的<str:pk>
造成的。
urls.py 文件如下:
urls.py
models.py :
from . import views
from .views import ExamListView, ExamDetailView
app_name='map'
urlpatterns = [
path("", ExamListView.as_view(), name='map-home'),
path("exam/<str:pk>", ExamDetailView.as_view(), name="exam-detail"),
path("login_student/", views.login_student, name='map-login'),
path("register_student", views.register_student, name='map-register'),
path('add_student/', views.add_student, name='add_student'),
path('front/', views.front, name="front"),
]
home.html
subject = models.TextField(primary_key = True, unique = True)
def __str__(self):
return self.subject
base.html
{% extends "map/base.html" %}
{% block content %}
<p>WELCOME HOME</p>
{% for exam in exams %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'map:exam-detail' exam.subject %}">{{ exam }} </a>
</div>
</div>
</article>
{% endfor %}
{% endblock content %}
views.py
{% load static %}
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{% static 'map/main.css' %}">
{% if title %}
<title>{{ title }}</title>
{% else %}
<title> MAP PAGE </title>
{% endif %}
</head>
<body>
<header class="site-header">
<nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
<div class="container">
<a class="navbar-brand mr-4" href="/">Project M.A.P</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarToggle">
<div class="navbar-nav mr-auto">
<a class="nav-item nav-link" href="{% url 'map:map-home' %}">Home</a> <!-- no hardcoded links -->
<a class="nav-item nav-link" href="{% url 'map:map-home' %}">About</a>
</div>
<!-- Navbar Right Side -->
<div class="navbar-nav">
{% if user.is_authenticated %}
<a class="nav-item nav-link" href="{% url 'profile' %}">Profile</a>
<a class="nav-item nav-link" href="{% url 'logout' %}">Logout</a>
{% else %}
<a class="nav-item nav-link" href="{% url 'login' %}">Login</a>
<a class="nav-item nav-link" href="{% url 'register' %}">Register</a>
<a class="nav-item nav-link" href="{% url 'map:map-register' %}">Student</a>
{% endif %}
</div>
</div>
</div>
</nav>
</header>
<main role="main" class="container">
<div class="row">
<div class="col-md-8">
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% block content %}{% endblock %}
</div>
<div class="col-md-4">
<div class="content-section">
<h3>Our Sidebar</h3>
<p class='text-muted'>You can put any information here you'd like.
<ul class="list-group">
<li class="list-group-item list-group-item-light">Latest Posts</li>
<li class="list-group-item list-group-item-light">Announcements</li>
<li class="list-group-item list-group-item-light">Calendars</li>
<li class="list-group-item list-group-item-light">etc</li>
</ul>
</p>
</div>
</div>
</div>
</main>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>[enter image description here][1]
答案 0 :(得分:0)
您错过了home.html中的exam.pk 以这种方式更新代码,
<div class="article-metadata">
<a class="mr-2" href="{% url 'map:exam-detail'
exam.pk %}">{{ exam }} </a>
</div>
</div>
答案 1 :(得分:0)
在 python manage.py shell
from my_app_name.models import Exam
exam = Exam()
exam.subject = ""
exam.save()
我已经执行了这些行。而且我还为“ google”,“ english”,..
现在Exam.objects.all()
返回
<QuerySet [<Exam: google>, <Exam: english>, <Exam: >]>
问题是由于具有长度为零的字符串作为检查对象之一的主题。现在,我使用Exam.objects.get(subject="").delete()
删除了该考试。问题解决了。
{% url 'map:exam-detail' exam.subject %}
或
{% url 'map:exam-detail' exam %}
或
{% url 'map:exam-detail' exam.pk %}
所有这些现在都在工作,因为所有这些都返回了主题。但是{% url 'map:exam-detail' exam.id %}
不起作用。因为我已经在 Exam模型中将主题设置为 primary_key ,所以Exam模型没有属性ID(默认情况下会在没有提及primary_key)。
考虑为什么它接受空字符串作为主题,尽管我已将其定义为primary_key。 然后,我想阅读有关django.db.models.TextField的信息,然后键入帮助(TextField)并得到
TextField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class 'django.db.models.fields.NOT_PROVIDED'>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text='', db_column=None, db_tablespace=None, auto_created=False, validators=(), error_messages=None)
现在尝试使用验证器 ... 后续问题link
谢谢大家 穆尼卡