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.
Related
I have url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic') in urls.py but when I try to go to localhost:8000/topics/1 it tells me that it tried one pattern: topics/(?P<topic_id>**\\**d+)/$
I would think it would be topics/(?P<topic_id>**\**d+)/$
I'm using a book called The Python Crash Course (1st edition)(ch. 18).
This is a local server using Django 1.11 with Python. I've tried a lot of reformatting on the url pattern but I am new at this so I don't know what else to do.
...
urlpatterns = [
url(r'^$', views.index, name='index'),
# Show all topics.
url(r'^topics/$', views.topics, name='topics'),
# Detail page for a single topic.
url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic'),
]
I expected it to pop up with the correct page but it always says 'NoReverseMatch at /topics/01/'
So you've forgotten the trailing slash at the end of your URL, hence why it's not matching.
You could remove the slash from the regex, but that would shift the problem: it wouldn't work if you put a slash.
I guess you could end the pattern with /?$, but here's a solution that's probably more robust: Jiaaro's answer to: django urls without a trailing slash do not redirect
Basically:
check your APPEND_SLASH setting in the settings.py file
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".
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 started learning Django, I'm not sure what the include() function means.
Here is mysite/urls.py. - project
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
Here is polls/urls.py. - app in project
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
From django document, include() function was described as follows.
Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
I'm not sure what is that point, what is remaining string.
In case of the example above, what is remining string, what is url strings which was chopped off?
For example, from this URL:
polls/5/results
the URL rule:
url(r'^polls/', include('polls.urls')),
chops off the polls/ part of URL and sends the remaining string after polls/, whatever it might be, for example (see here more):
5/results/
to urls from the poll app's urls.py, where it will then be mapped to a view based on the URL rules defined in this file
Whenever it will encounter any url with /polls then it will include all the urls of polls app.
Example:
If you type /polls/hey
Then as soon as it sees /polls it will go to polls urls file and later it will search for:
hey/ matching over there.
Lets say there is one more entry in your polls/urls.py like
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
here year is the query string parameter. so your url will look like
/polls/articles/2007 so in this case /polls/articles/ will matched up and 2007 will pass to year_archive method
In your example there is no chopped string, the URL comes back as simply polls/, but when you have another url such as '^new$' then that url is being chopped, merged with polls/ and it returns polls/new, hope this makes sense..
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')),