0

update

I apologise for yet another simple Django question but I cannot find what I'm after.

Project: Blog I'm trying to URL design to the app using Django 2.0

mysite/url.py

Error after run

server error

Community
  • 1
  • 1
Al Imran Hossain
  • 119
  • 1
  • 2
  • 12

2 Answers2

2

Django 2.0 adds the new path function for urls : https://docs.djangoproject.com/fr/2.0/ref/urls/#path

path doesn't use regex anymore.

You have two solutions

1) Use path and change the url pattern to new django format

from django.urls import path 
urlpatterns = [
    url('/post/<int:pk>/', ...)
]

2) Keep you regex and use re_path

from django.urls import re_path
urlpatterns = [
    re_path('^/post/(?<pk>[0-9]+)/$', ...)
]

Note that using url function is still possible but will be likely be deprecated in a next version. It has been renamed into re_path in Django 2.0

luc
  • 41,928
  • 25
  • 127
  • 172
1

update you urls.py file.

you need to import view_post in urls.py file

from blog.views import view_post
#from appname.file.py import (class/func)name

As you are using view_post in urls.py so you also need to import it in that file.

LeLouch
  • 601
  • 8
  • 21
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
  • Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/post/1 Using the URLconf defined in mysitee.urls, Django tried these URL patterns, in this order: admin/ blog/ ^post/(?P[0-9]+)/$ [name='view_post'] The current path, post/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. – Al Imran Hossain Feb 21 '18 at 13:06
  • update your question with code instead of pictures and post your error as text code. – Usman Maqbool Feb 21 '18 at 13:09
  • update your url `post/(?P[0-9]+)/$` with `post/(?P\d+)/$`. – Usman Maqbool Feb 21 '18 at 13:23
  • i want http://127.0.0.1:8000/post/1 by first blog post – Al Imran Hossain Feb 21 '18 at 13:26
  • update your `url.py` `post/(?P[0-9]+)/$` with `post/(?P\d+)/$` and when you will hit `127.0.0.1:8000/post/1` and then it will return your desired result. – Usman Maqbool Feb 21 '18 at 13:30