is not a valid regular expression in urls.py - python

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]+)/$'

Related

Django 1.11 url pattern error, how to solve?

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".

Django URL pattern doesn't work

I have my urls.py included like this:
urlpatterns = [
path('files/', include('files.urls')),
]
then, in files/urls.py, I put this:
urlpatterns = [
path('', views.index, name='index'),
path(r'(?P<name>[a-z]+)', views.check, name='check')
]
So, I assume that when example.com/files works, so should example.com/files/somename, but it doesn't:
Using the URLconf defined in example.urls, Django tried these URL
patterns, in this order:
[name='index']
files/ [name='index']
files/ (?P<name>[a-z]+) [name='check']
The current path, files/somename, didn't match any of these.
What am I missing here?
You don't need to use regexp with path method. Instead you can simply specify str as argument type:
path('<str:name>/', views.check, name='check')
If you want to use regular expression, use re_path:
re_path(r'(?P<name>[a-z]+)', views.check, name='check')

Django URL dispatcher - it calls the same view to the different URL(regular expression)

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'),
]

Django app urls not working properly

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.

Match all urls without the prefix in Django

I'm currently using the following urls.py:
api_patterns = [
url(r'^users/', include('users.urls', namespace='user')),
]
internal_patterns = [
# ...
]
urlpatterns = [
url(r'^api/', include(api_patterns)),
url(r'^internal/', include(internal_patterns)),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?!(?:api|internal|admin)/)', MainView.as_view()),
]
The point of this config is to render MainView if url doesn't have the api, internal or admin prefix:
/api/users/... — found
/api/foo/ — not found
/foo/ — found
How can I make it simplier and more intent revealing?
I think your intent will be more clear if you do this in two urls:
url(r'^(api|internal|admin)/', SomeView.as_view()),
url(r'^.*', MainView.as_view())
MainView will be executed only if a url does not begin with api, internal or admin.
SomeView will be executed if a url begins with api/internal/admin but doesn't match the patterns above it. You can customize this view to either return a default 404 page, or perform other functions as you need.
Using your examples:
/api/users will execute include(api_patterns)
/api/foo will execute SomeView
/foo will execute MainView
Edit
To address the first point in your comment: url patterns are regexes, while you can extract these into variables to remove duplication, it can make code hard to read. Here's one example for removing duplication (I'm sure you can come up with others):
d = OrderedDict([
(r'api', api_patterns),
(r'internal', internal_patterns),
(r'admin', admin.site.urls),
])
main_view_re = r'^!({})/'.format('|'.join(d.keys()))
urlpatterns = [url(r'^{}/'.format(k), include(v)) for k, v in d]
urlpatterns.append(url(main_view_re, MainView.as_view()))
For django >= 3 rather use re_path:
from django.urls import re_path
urlpatterns = [
re_path(r'^.*',MainView.as_view())
]
urlpatterns = [
url(r'^api/', include(api_patterns)),
url(r'^internal/', include(internal_patterns)),
url(r'^admin/', include(admin.site.urls)),
url(r'', MainView.as_view()),
]
Leaving no prefix would allow you to catch any URL that a user might try after the URL conf matches the api, internal and admin url's.

Categories

Resources