Why it happens? I do everything according to the instructions. I study. urls.py

from django.conf.urls import url,include from django.contrib import admin from . import views urlpatterns = [ #url(r'^$',views.main,name="main"), url(r'^post/(?P<id>[0-9]+)/', views.post_detail, name='post_detail'), ] 

views.py

 from django.shortcuts import render from django.utils import timezone from .models import Post from django.shortcuts import render, get_object_or_404 # Create your views here. def post_detail(request,id): post = get_object_or_404(Post, pk=id) return render(request, 'main/post_detail.html', {'post': post}) 

post_detail.html

 {% extends 'main/base.html' %} {% block content %} <div class="post"> {% if post.published_date %} <div class="date"> {{ post.published_date }} </div> {% endif %} <h1>{{ post.title }}</h1> <p>{{ post.text|linebreaksbr }}</p> </div> {% endblock %} 

What to do? Even instead of id put pk, but nothing helps.

Page not found

  • maybe the post with aydishnikom "1" is not in the database? - Ivan Semochkin
  • @IvanSemochkin is. I myself in the admin panel created posts - Aminev Timur
  • maybe the first post was deleted and then several more were created, respectively, the id "1" is no longer in the database. - Ivan Semochkin
  • @IvanSemochkin nothing has changed. Post with id 4 exists, but the link does not work. id 4 - Aminev Timur
  • Is this urls.py unique in the project? - andreymal

2 answers 2

try to put at the end of references $

or import views in the full way, it seems that urls.py and views.py are in different directories.

    A url uses its own syntax, where ^ is the opening element and $ is the closing element.
    In your case, you forgot to put the closing one, which can also be noticed on the page with an error that shows you the templates into which you tried to cram your link. Just you expect that the jango will go through 2 patterns, but instead of the second pattern, the whole line of code.

    Here you can read about url:
    https://tutorial.djangogirls.org/ru/django_urls/