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')),
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
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 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 don't understand my problem here is my code:
urls.py
urlpatterns = patterns('blog.views',
...
url(r'^(?P<slug>.+)$', 'blog', name='blog'),
url(r'^membres$', 'membres', name='membres'),
)
views:
def blog(request, slug):
posts = Post.objects.filter(blog__slug=slug)
return render(request, 'blog/blog.html', locals())
def membres(request):
membres = User.objects.all()
return render(request, 'blog/membres.html', {'membres': membres})
Here is my link in my base.html template
<li>List</li>
When I click the link it redirect me to the blog view and then render blog.html instead of using membres view.
I got no error in console or in my template.
All my code is in my app called 'blog'
Django uses the first pattern that matches. Your first URL regex matches any string, including /membres, so Django never tries the second one. I suggest something like this:
urlpatterns = patterns('blog.views',
url(r'^/blog/(?P<slug>[-\w]+)/$', 'blog', name='blog'),
url(r'^membres/$', 'membres', name='membres'),
)
If you must have a catch-all pattern, it should be the last one in the list, so the other patterns have a chance to match before:
urlpatterns = patterns('blog.views',
url(r'^membres/$', 'membres', name='membres'),
# other patterns...
url(r'^(?P<slug>[-\w]+)/$', 'blog', name='blog'),
)
It's also a good habit to always include the trailing slash (Django will append it to requests by default). To match a slug, I suggest [-\w]+, which will match any sequence of alphanumeric characters, _ and -.
It's because urlresolver takes patterns from top to bottom and 'membres' matches (?P<slug>.+) so urlresolver returns blog view. Put more concrete urlpatterns higher. Also I suggest using more specific characters in slug regexp, i.e. (?P<slug>[A-Za-z0-9_\-]+).
Django stops at the first URL pattern that matches. This means that your
blog view -- which simply looks for one or more characters -- will interpret your mysite.com/membres URL to be a blog post with the slug membres.
To fix it, try swapping the order of your URL patterns:
urlpatterns = patterns('blog.views',
...
url(r'^membres$', 'membres', name='membres'),
url(r'^(?P<slug>.+)$', 'blog', name='blog'),
)
In general, you want your most general patterns at the bottom for exactly this reason.
I'm having the 404 error when trying to handle a view that is not related to the main page. For example, if I initially start at the main page home, and want to navigate to another page called, otherpage, I receive a 404 otherpage.html not found.
The way I'm doing is based off intuition. So if there's a better way to do this, please mention it:
in the file:
prd/
views.py
url.py
otherstuffthatshouldbehere.py..
I have views.py (this is where I think the error is):
from django.shortcuts import render
def home(request):
context = {}
template = "index.html"
return render(request, template, context)
def otherpage(request):
context = {}
template = "otherpage.html"
return render(request, template, context)
Then urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', 'prd.views.home', name='home'),
url(r'^$', 'prd.views.otherpage', name='otherpage'),
url(r'^admin/', include(admin.site.urls)),
)
This returns a 404 for the otherpage.html. I have the template directory working fine. But how do I go about handling multiple views?
EDIT:
Upon adding:
url(r'^otherpage$', 'prd.views.otherpage', name='otherpage'),
I received this error:
Using the URLconf defined in prd.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^about$ [name='about']
^projects$ [name='projects']
^admin/
The current URL, about.html, didn't match any of these.
The urlpatterns starts at the top and then goes down until it matches a URL based on the regex. In order for Django to serve up the page located at otherpage.html there has to be a URL defined in urlpatterns that matches otherpage.html otherwise you will get the 404.
In this case you have:
url(r'^$', 'prd.views.home', name='home'),
url(r'^$', 'prd.views.otherpage', name='otherpage'),
Note that nothing will ever get to otherpage here because home has the same pattern (regex) and will therefore always match first. You want to have a different URL for both those so that you can differentiate between them. Perhaps you could do something like:
url(r'^$', 'prd.views.home', name='home'),
url(r'^otherpage.html$', 'prd.views.otherpage', name='otherpage'),
After making this change you now have a match for otherpage and hopefully no more 404.
EDIT:
url(r'^otherpage.html$', 'prd.views.otherpage', name='otherpage'),
This matches www.example.com/otherpage.html but it will not match www.example.com/otherpage
To match www.example.com/otherpage you need to use:
url(r'^otherpage$', 'prd.views.otherpage', name='otherpage'),
Note the difference in the regex, there's no .html here. The regex matches exactly what you put in it.