Discussion:
path('posts/(?P<post_id>[0-9]+)/$'
(too old to reply)
e***@gmail.com
2018-12-02 15:50:53 UTC
Permalink
When I run http://localhost:8000/posts/2

I get the error:


Using the URLconf defined in blog.urls,
Django tried these URL patterns, in this order:


1. admin/
2.
3. Posts/(?P<post_id>[0-9]+)/$
4. ^media/(?P<path>.*)$
The current path, posts/2, didn't match any of these.


Here is my .py's

from django.contrib import admin
from django.urls import path
from posts import views
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('posts/(?P<post_id>[0-9]+)/$', views.post_details),

] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)


views.py

from django.shortcuts import render
from posts.models import Post

# Create your views here.
def home(request):
posts = Post.objects.order_by('pub_date')

return render(request, 'posts/home.html', {'posts': posts})

def post_details(request, post_id):
return render(request, "posts/posts_detail.html", {'post_id': post_id})


Any ideas why it doesn't work. I am a newbie with django
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d28eac04-2edc-405e-9942-e883b1e7bff8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jani Tiainen
2018-12-02 17:59:22 UTC
Permalink
Hi.

Path is a new url format which uses format of <parser:variable> for exanple
like <int:path> . If you want to use regular expressions use re_path
instead.
Post by e***@gmail.com
When I run http://localhost:8000/posts/2
Using the URLconf defined in blog.urls,
1. admin/
2.
3. Posts/(?P<post_id>[0-9]+)/$
4. ^media/(?P<path>.*)$
The current path, posts/2, didn't match any of these.
Here is my .py's
from django.contrib import admin
from django.urls import path
from posts import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('posts/(?P<post_id>[0-9]+)/$', views.post_details),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
views.py
from django.shortcuts import render
from posts.models import Post
# Create your views here.
posts = Post.objects.order_by('pub_date')
return render(request, 'posts/home.html', {'posts': posts})
return render(request, "posts/posts_detail.html", {'post_id': post_id})
Any ideas why it doesn't work. I am a newbie with django
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/d28eac04-2edc-405e-9942-e883b1e7bff8%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/d28eac04-2edc-405e-9942-e883b1e7bff8%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHn91of_f5HKn36niAVbuVjg%3DmxYeLPSHxRxBOn5kRiW9ZLD2w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
e***@gmail.com
2018-12-03 00:51:00 UTC
Permalink
I am new to django so I am not sure I understand what you are saying. Can
you give me a rewrite of the correct syntax of the line that is not correct?

Thank you
Post by Jani Tiainen
Hi.
Path is a new url format which uses format of <parser:variable> for
exanple like <int:path> . If you want to use regular expressions use
re_path instead.
Post by e***@gmail.com
When I run http://localhost:8000/posts/2
Using the URLconf defined in blog.urls,
1. admin/
2.
3. Posts/(?P<post_id>[0-9]+)/$
4. ^media/(?P<path>.*)$
The current path, posts/2, didn't match any of these.
Here is my .py's
from django.contrib import admin
from django.urls import path
from posts import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('posts/(?P<post_id>[0-9]+)/$', views.post_details),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
views.py
from django.shortcuts import render
from posts.models import Post
# Create your views here.
posts = Post.objects.order_by('pub_date')
return render(request, 'posts/home.html', {'posts': posts})
return render(request, "posts/posts_detail.html", {'post_id': post_id})
Any ideas why it doesn't work. I am a newbie with django
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
<javascript:>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/d28eac04-2edc-405e-9942-e883b1e7bff8%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/d28eac04-2edc-405e-9942-e883b1e7bff8%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2f56611d-1510-4f94-b2a2-82aa0a700780%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Lachlan Musicman
2018-12-03 02:02:52 UTC
Permalink
Post by Jani Tiainen
Hi.
Path is a new url format which uses format of <parser:variable> for
exanple like <int:path> . If you want to use regular expressions use
re_path instead.
Post by e***@gmail.com
When I run http://localhost:8000/posts/2
Here is my .py's
from django.contrib import admin
from django.urls import path
from posts import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('posts/(?P<post_id>[0-9]+)/$', views.post_details),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
So, to your urls.py you need to add:

from django.urls import path, re_path

And your url patterns will need to look like

re_path('posts/(?P<post_id>[0-9]+)/$', views.post_details),

I've not looked for a while, but it might even need to be

re_path(r'posts/(?P<post_id>[0-9]+)/$', views.post_details),

Cheers

L.




------
"The fact that you're still saying something like 'feminism gone mad'
suggests that feminism hasn't gone mad ENOUGH yet."

Helen Zaltzman / @HelenZaltzman
https://twitter.com/HelenZaltzman/status/1065384934846545920
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGBeqiPuTq_4N2dd9nBDDPfFfOo0Ra_0Q23i5k0Wjgmhe4%2B5KQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
e***@gmail.com
2018-12-03 11:54:26 UTC
Permalink
They both worked.

Thank you!!!!!
Post by Lachlan Musicman
Post by Jani Tiainen
Hi.
Path is a new url format which uses format of <parser:variable> for
exanple like <int:path> . If you want to use regular expressions use
re_path instead.
Post by e***@gmail.com
When I run http://localhost:8000/posts/2
Here is my .py's
from django.contrib import admin
from django.urls import path
from posts import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('posts/(?P<post_id>[0-9]+)/$', views.post_details),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
from django.urls import path, re_path
And your url patterns will need to look like
re_path('posts/(?P<post_id>[0-9]+)/$', views.post_details),
I've not looked for a while, but it might even need to be
re_path(r'posts/(?P<post_id>[0-9]+)/$', views.post_details),
Cheers
L.
------
"The fact that you're still saying something like 'feminism gone mad'
suggests that feminism hasn't gone mad ENOUGH yet."
https://twitter.com/HelenZaltzman/status/1065384934846545920
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54c9a62c-1c4a-480a-876e-c248f19ccefa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jani Tiainen
2018-12-03 10:58:00 UTC
Permalink
path('posts/<int:post_id>/$') should work as well.
Post by e***@gmail.com
I am new to django so I am not sure I understand what you are saying. Can
you give me a rewrite of the correct syntax of the line that is not correct?
Thank you
Post by Jani Tiainen
Hi.
Path is a new url format which uses format of <parser:variable> for
exanple like <int:path> . If you want to use regular expressions use
re_path instead.
Post by e***@gmail.com
When I run http://localhost:8000/posts/2
Using the URLconf defined in blog.urls,
1. admin/
2.
3. Posts/(?P<post_id>[0-9]+)/$
4. ^media/(?P<path>.*)$
The current path, posts/2, didn't match any of these.
Here is my .py's
from django.contrib import admin
from django.urls import path
from posts import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('posts/(?P<post_id>[0-9]+)/$', views.post_details),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
views.py
from django.shortcuts import render
from posts.models import Post
# Create your views here.
posts = Post.objects.order_by('pub_date')
return render(request, 'posts/home.html', {'posts': posts})
return render(request, "posts/posts_detail.html", {'post_id': post_id})
Any ideas why it doesn't work. I am a newbie with django
--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/d28eac04-2edc-405e-9942-e883b1e7bff8%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/d28eac04-2edc-405e-9942-e883b1e7bff8%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/2f56611d-1510-4f94-b2a2-82aa0a700780%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/2f56611d-1510-4f94-b2a2-82aa0a700780%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHn91ofU6wd6reV-BVyuW%2BwLOhkxC%3DU9EXPXLNcmq1-7fCsC_g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...