I am following a tutorial which I'm trying to write in Django 2.0.5, but I got road block in the urls.py and I've been searching for a week, but still giving me Page not found (404) error message. Can someone give a clearer picture of path() as it seems to be not supporting regex. Here is my code:
views.py
from django.shortcuts import render
#from django.http import HttpResponse
from .models import Board
# Create your views here.
templates/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
and the error message in my browser
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.
You have not defined a URL that matches '/board/<pk>/'
. If you meant for the board_topics
you currently have in your urls.py file to be that url then it should be 'board/<int:pk>/'
instead of just '<int:pk>/'
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.