The current URL, , didn't match any of these - python

I have already checked this error on other stackoverflow threads but don't find any error on my code. Perhaps I'm tired but it seems ok to me.
website.urls.py:
from django.conf.urls import patterns, include, url
#from django.contrib import admin
urlpatterns = patterns('',
# Register and Login
url(r'^inscription\.html$', 'membres.views.register'),
# List of users
url(r'^membres', include('membres.urls')),
)
membres.urls.py:
from django.conf.urls import patterns, url
urlpatterns = patterns('membres.views',
url(r'^/(?P<slug>\d+)\.html$', 'view_user_public')
)
Of course I get :
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
^inscription\.html$
^membres ^/(?P<slug>\d+)\.html$
The current URL, membres/nicolas.html, didn't match any of these.
The inscription.html works properly. In the membres.urls.py file, if I change r'^/(?P<slug>\d+)\.html$ to r'^\.html$, the url membres.html works properly and loads the view...
What's wrong with r'^/(?P<slug>\d+)\.html$ ?
Thanks a lot

\d+ would match digits only. nicolas is not composed of digits.
\w+ is what you're looking for.
More generally, [\w-]+ for slugs which typically contain hyphens.

Related

urlpattern Regex is not working as expected

I have a Django website and for the times that pages are not ready I want to redirect any URL to a specific maintenance page.
So in the urlpatterns of my website I added this regex expecting it to capture anything after / but it's not working.
urlpatterns = [ path(r'/.*',maintenance_view,name='maintenance') ]
I found the answer myself. The problem was that I had to use re_path and also in the regex django was not caring about the "/", so I removed it.
from django.urls import re_path
urlpatterns=[
re_path(r'.*',maintenance_view, name='maintenance')
]

URL Pattern with Primary Key not working properly

Need help with the regex in urls. I'm building a different app, not the one shown in the lecture above. To relate to my case with the lecture, School is Clients, and Students is categories.
In urls.py file, from url_patterns :
url(r'^(?P<pk>[-\w]+)/$', views.DetailClientList.as_view(), name='Detail_Client_List'),
This work correctly, with the address being http://127.0.0.1:8000/App1/cli1/, where cli1 is a Clients table primary key (one of the records).
But when I put the line below in url patterns (instead of the above)
url(r'^<str:pk>/$', views.DetailClientList.as_view(), name='Detail_Client_List')
I get the following error (same error for int:pk):
Page not found (404)
Request Method:GET
Request URL:http://127.0.0.1:8000/App1/cli1/
The resulting URL is the same in both cases described above. So where am I going wrong here. I'm guessing its an issue with the url pattern regex (although resulting URL is the same?).
Please help. TIA!
try with re_path instead of 'url'
from django.urls import re_path, path
1. re_path
re_path(r'^<str:pk>/$', views.DetailClientList.as_view(), name='Detail_Client_List')
2. path
path('<str:pk>/', views.DetailClientList.as_view(), name='Detail_Client_List')
You might misunderstood the url(...)--(Doc) with path(...)--(Doc)
So,
from django.conf.urls import url
urlpatterns = [
url(
r'^(?P<pk>[-\w]+)/$',
views.DetailClientList.as_view(),
name='Detail_Client_List'
),
]
is identical with the following,
from django.urls import path
urlpatterns = [
path(
'<str:pk>',
views.DetailClientList.as_view(),
name='Detail_Client_List'
)
]

Django urls.py: url doesn't work properly

I'm developing a webpage with Django.
I want to add/define an url for the application.
At this moment the url (dsvd/) doen't work properly. It shows only table data but no css and background.
here is the code for the main urls.py file.
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'kleedkamer_overview.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('kleedkamer_overview.urls')),
url(r'^dsvd/', 'kleedkamer_overview.views.indeling'),
)
and here the code inside the application urls.py file
from django.conf.urls import patterns, url
from kleedkamer_overview import views
urlpatterns = patterns ('',
url(r'^$',views.indeling, name='indeling'),
)
Is there anyone who can help me find the problem?
Greetz.
It looks like line is giving you trouble:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
It seems like you are trying to map this URL to kleedkamer_overview.views.indeling but the URL regrex does not end with a '$'. Urls.py files that are mapped to other apps look like this:
url(r'^admin/', include(admin.site.urls))
Notice the include function call and that the regrex expression r'^admin/' does not end with a $
However mapping to a specific view is a bit different and it looks like this:
url(r'^$', 'kleedkamer_overview.views.home', name='home')
Notice that this time, where the include() function call would go you are instead telling Django which specific view you want to use and that the site regrex is r'^$' ending with a $.
Try changing this:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
to this:
url(r'^dsvd/$', 'kleedkamer_overview.views.indeling')
Edit:
I saw your comments and while the urls.py is not the source of your problem, what I said is still valid because the urls.py was malformed. You should not even have the offending line there at all because you have already included the urls.py from kleedkamer_overview , there is no need to include it twice. It isn't DRY and it is just bad practice in general. This is why this still works despite being malformed because sites are looked for in order, in this case first it looks for /admin then / and does not reach /dsvd because it was already caught by / and your malformed url mapping is NEVER reached.

Django url dispatcher - wrong function

i am new to Django and i have some problem with Django URL dispatcher.
I have "prometfire" project and "homepage" app.
My goal is to connect this paths to their view functions:
127.0.0.1:8000 --> "homepage_view"
127.0.0.1:8000/welcome --> "welcome_view"
"homepage_view" works fine, but when i go to 127.0.0.1:8000/welcome i have same result as in "homepage_view", instead of "welcome_view" result.
Am i missing something?
Django 1.5
Python 2.7
#urls.py in prometfire
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('homepage.urls')),
url(r'^welcome/', include('homepage.urls')),
url(r'^admin/', include(admin.site.urls)),
)
#urls.py in homepage app
from django.conf.urls import patterns, include, url
urlpatterns = patterns('homepage.views',
url(r'^$', 'homepage_view'),
url(r'^welcome/', 'welcome_view'),
)
#views.py in homepage app
from django.shortcuts import render_to_response
from django.http import HttpResponse
def homepage_view(request):
return render_to_response('homepage.html',
{'name': 'bob'}
)
def welcome_view(request):
return HttpResponse('Welcome')
Your problem is that you are including your homepage urls twice. Remove the second entry
url(r'^welcome/', include('homepage.urls')),
This is explained in the docs on including other url confs
Whenever Django encounters include() (django.conf.urls.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.
In your case, the 'welcome/' is removed from the url, which leaves '', which is matched by the url pattern for the homepage.
That's because it never enters the second condition for the app, it verifies the condition at the url root conf, welcome/, and after that goes directly to ^$ in the app. A solution would be remove the welcome/ from the url root.
The first welcome definition is redundant and is causing the "bug".

What's wrong with this url?

I have a root urls.py and an app urls.py. In my root I have this:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^$', include('realestate.properties.urls')),
(r'^admin/', include(admin.site.urls)),
)
In my app urls I have the following
from django.conf.urls.defaults import *
urlpatterns = patterns('realestate.properties.views',
url(r'^$', 'property_list', {'template_name': 'properties/property_list.html'}, name='property_list'),
url(r'^(?P<slug>[-\w]+)/$', 'property_detail', { 'template': 'property_detail.html' }, name='property_details'),
)
now in my template I have a link to the details view, which looks like this:
{% url property_details property.slug %}
Everytime I render this page i get the error:
*Caught NoReverseMatch while rendering: Reverse for 'property_details' with arguments '(u'111-front-st',)' and keyword arguments '{}' not found.*
No matter what I do, I get that error. I tried capturing just the id and nothing is working, i am not sure why, I have used url's many times before so I am really confused if I am missing something obvious. Any see anything wrong here?
Thanks
Jeff
I think you need to drop the $ from your urlconf, where you include the app's urls. Probably you can remove the ^ too.
urlpatterns = patterns('',
(r'^', include('realestate.properties.urls')),
(r'^admin/', include(admin.site.urls)),
)
http://docs.djangoproject.com/en/1.2/topics/http/urls/#including-other-urlconfs
Note that the regular expressions in
this example don't have a $
(end-of-string match character) but do
include a trailing slash. 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.
Do something like this:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^$', 'realestate.properties.views.property_list'),
(r'^properties/', include('realestate.properties.urls')),
(r'^admin/', include(admin.site.urls)),
)
Otherwise (like sugested by Reiners post) you will make the first regular expression a "catch all" and /admin will never match.
You can also place the admin regular expression before your "catch all" re, but what happens if you have a slug like 'admin'? That is why I would advice against a url scheme with /<slug>/ at the first level. Instead, use /<object-type>/<slug>/, that will make room for other things in the future.

Categories