When I try to fix the url in my urlpatterns it shows me this error :
The error:
Your URL pattern "url(r'^player/[?P[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view'),"
is invalid. Ensure that urlpatterns is a list of url() instance.
try removing the string 'url(r'^player/[?P[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view'),'. The list of urlpatterns should not have a prefix string as the first element.*
My Code :
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', HomePageView.as_view(), name='home-page'),
url(r'^teams/$', TeamsListView.as_view(), name='teams-list-view'),
url(r'^scores/$', ScoresListView.as_view(), name='scores-list-view'),
url(r'^player/[?P<slug>[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view'),
]
Can anyone help me??
The syntax is a bit off, you need to use round brackets (..) instead of square brackets [..] around a "capture group":
url(
r'^player/(?P<slug>[-\w\x20]+)/$',
PlayerDetailView.as_view(),
name='player-detail-view'
),
Furthermore if I recall correctly, a slug can not contain spaces, so you might want to remove the \x20.
Note that in django-2.0 and higher, the path(..) [Django-doc] function can be used, which has support for slugs like:
# Django 2.0 and higher
path('player/<slug:slug>/', PlayerDetailView.as_view(), name='player-detail-view'),
Then Django replaces the slug with a builtin pattern, which makes the URL patterns more "declarative".
Related
I have a url path in urls.py:
urlpatterns = [
...
url(r'^accounts/', include('allauth.urls')),
...
]
but the url will show: unresolved reference 'url'. Did I miss something to import?
As of Django 2 url() was replaced with path() and re_path(). If you are not using Django 1, you can update your code to use path().
from django.urls import path, include
urlpatterns = [
path('accounts/', include('allauth.urls')),
]
For matching a path with RegEx like the Django 1 url() function you can use re_path() like this...
from django.urls import path, include
urlpatterns = [
re_path(r'^accounts/', include('allauth.urls')),
]
However, because of how simple the path you are trying to match is, I would recommend using path(). It saves to overhead of performing a regular expression match. Use path() over re_path() as much as possible.
You can read more on the official Django documentation. See links below.
Django 3 Documentation
Old Django 1 Documentation
You may be using Django 2.x
For django-1.x, you can not use such path(..)s, and in that case you need to write a regular expression, like:
url(r'^complete/(?P<todo_id>[0-9]+)$', views.completeTodo, name='complete'),
If you are using django-2.x, you probably want to use path(..), like you have.
I believe it may be to do with how you've set up your regex.
For urls, instead of this:
url('complete/<todo_id>', views.completeTodo, name='complete'),
try this:
url(r'^complete/(?P<todo_id>\d+)$', views.completeTodo, name='complete'),
Or incase you want to use [path]
path('complete/<int:todo_id>', views.completeTodo, name='complete'),
I faced the problem, I need to add functions to the file urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P[0-9]+)/answer/$', views.answer, name='answer')
]
Running the server I get
"^(?P[0-9]+)/$" is not a valid regular expression: unknown extension ?P[
Tell me what's wrong...
As the error says, that's indeed not a valid regex.
?P introduces a named group, which Django uses as a keyword argument. You need to provide the name of that group, surrounded by angle brackets. For example:
url(r'^(?P<id>[0-9]+)/$'
Hello thank you very much for reading my question post.
I have different url path patterns in urlpatterns,
but Django URL dispatcher(re-path) calls the same view( views.selected_verb)
for the different URL expressed by Regular expression.
These urls call the same view(views.selected_verb)
http://127.0.0.1:8000/arabic/verbs/%D9%83%D8%A7%D9%86/
http://127.0.0.1:8000/arabic/verbs/%D9%83%D8%A7%D9%86/quiz/
Would love to know how to fix it(calls different views)
here is urlpatterns
urlpatterns = [
path('', views.index, name='index'),
path('verbs', views.verbs, name='verbs'),
re_path(r'^verbs/(?P<verb>.+)/$', views.selected_verb, name='selected_verb'),
re_path(r'^verbs/(?P<verb>.+)/quiz/$', views.quiz, name='quiz'),
]
Thank you very much again!
I think the issue is that .+ will match with anything, which includes %D9%83%D8%A7%D9%86/quiz/. Maybe you could try telling it something more explicit, like [A-Z0-9%]+. When the q character comes along in quiz, it will fail matching and then go to the next url pattern which should be the one you want.
So I think it should all look like this:
urlpatterns = [
path('', views.index, name='index'),
path('verbs', views.verbs, name='verbs'),
re_path(r'^verbs/(?P<verb>[A-Z0-9%]+)/quiz/$', views.quiz, name='quiz'),
re_path(r'^verbs/(?P<verb>[A-Z0-9%]+)/$', views.selected_verb, name='selected_verb'),
]
I have the following base urls file:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^agenda/', include('planner.urls', namespace='planner', app_name='planner'))
]
And my planner app contains the following urls:
urlpatterns = patterns('',
url(r'^', SkirmList.as_view(), name='agenda'),
url(r'^skirm/(?P<pk>\d+)/$', SkirmDetailView.as_view(), name='skirmdetailview'),
)
The problem I am having is when going to: http://localhost:8000/agenda/skirm/41/
It keeps loading the SkirmList view instead of the SkirmDetailView.
It's propably obvious to most but I am a beginner with Django. Any help is appreciated, thanks
The regex r'^' matches any string. It just says the string needs to have a start. Every string has a start, so...
You need to include an end anchor as well:
url(r'^$', ...)
This regex looks for the start of the string, followed immediately by the end, i.e. an empty string. It won't match the /agenda/skirm/41/ url.
I have this regex in my urls.py for my blog app and I'd like to know why is it not working.
url(r'^/tag/(?P<tag_text>\w+)/$', views.tag, name='tag'),
and I have defined this in the blog's views.py
def tag(request,tag_text):
and this in the application's urls.py
url(r'^blog/', include('blog.urls')),
I have tried
localhost/blog/tag/sport
but I still get: The current URL, blog/tag/sport, didn't match any of these.
Am I doing something wrong?
Your pattern is trying to match an extra /, since your include url requires a trailing slash, and your tag url is trying to match a leading slash.
You should remove either one to make it work:
# tag url in blog/urls.py
url(r'^tag/(?P<tag_text>\w+)/$', views.tag, name='tag'),
# include in project/urls.py
url(r'^blog/', include('blog.urls')),