[英]Issues using path() in urls.py in django 2.0.5
我正在嘗試使用Django 2.0.5編寫的教程,但是urls.py中出現了障礙,並且已經搜索了一周,但仍給我頁面未找到(404)錯誤消息。 有人可以給出更清晰的path()圖片,因為它似乎不支持正則表達式。 這是我的代碼:
views.py
from django.shortcuts import render
#from django.http import HttpResponse
from .models import Board
# Create your views here.
template / topics.html
{% load static %}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{board.name}}</title>
<link rel="stylesheet" type="text/css" href="{% static
'css/bootstrap.min.css' %}">>
</head>
<body>
<div class="container">
<ol class="breadcrumb my-4">
<li class="breadcrumb-item">Boards</li>
<li class="breadcrumb-item active">{{board.name}}</li>
</ol>
</div>
</body>
</html>
urls.py
from django.contrib import admin
from django.urls import path
from boards import views
urlpatterns = [
path('', views.home, name='home'),
path('<int:pk>/', views.board_topics, name='board_topics'),
path('admin/', admin.site.urls)
]
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Board(models.Model):
name = models.CharField(max_length=30, unique=True)
description = models.CharField(max_length=100)
def __str__(self):
return self.name
class Topic(models.Model):
subject = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, on_delete=models.CASCADE)
starter = models.ForeignKey(User, on_delete=models.CASCADE)
# def __str__(self):
# return self.subject
class Post(models.Model):
message = models.TextField(max_length=4000)
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(null=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
#updated_by = models.ForeignKey(User, null=True)
# def __str__(self):
# return self.subject
和我的瀏覽器中的錯誤消息
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/board/1/
Using the URLconf defined in myproject.urls, Django tried these URL patterns,
in this order:
[name='home']
<int:pk>/ [name='board_topics']
admin/
The current path, board/1/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django
settings file. Change that to False, and Django will display a standard 404
page.
您尚未定義與'/board/<pk>/'
相匹配的URL。 如果board_topics
您當前在urls.py文件中使用的board_topics
就是該URL,則應為'board/<int:pk>/'
而不是'<int:pk>/'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.