URL Pattern with Primary Key not working properly - python

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

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

Django URL give me space between root and included urls

So I create an url in root/project/urls.py with this lines
from django.conf.urls import include
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('app.urls'))
]
while in my root/app/urls.py
from django.urls import path
from .views import UserView, AuthenticationView
urlpatterns = [
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
So it is expected to give me http://localhost:8000/users/register and http://localhost:8000/users/auth urls.
Meanwhile my request doesn't behave as expected.
apparently it returns me a space between the root path and include path. I check my root/project/settings.py file I don't find any weird settings. Does anybody know what is going on?
The space is just for displaying how the URL is built up, on the debug screen only.
I have experienced the same and at first I too thought Django somehow added the space. In the end it really is that there is no match between the URL's specified and the URL in your browser. Safari doesn't display the full url, so error can happen quickly...
Some additional info on urls in Django can be found here.
The error message in your screenshot states that the request URL is http://localhost:8000/users does not exist.
Here you redirects /users/ to app.urls:
path('users/', include('app.urls'))
But in app.urls, you never included a pattern for when the URL ends with only "/users/". Instead, "/users/register/" and "/users/auth/" are both specified.
urlpatterns = [
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
So http://localhost:8000/users/register and http://localhost:8000/users/auth should be valid URLs, but http://localhost:8000/users is not.
You can add another URL pattern for when the URL ends at "/users/":
urlpatterns = [
path('', AuthenticationView.as_view()), # maybe the same as /auth/ ?
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
In conclusion, Django's actually not wrong about that page does not exist (404), it's because you hadn't match http://localhost:8000/users in any of the urlpatterns.
Have you tried to use regular expressions?
path(r'^admin/', admin.site.urls)
Otherwise in the Django 2 version the urls schema has been changed, I use url function instead the path function, that can be a possible fix for your problem

Polls application -- django tutorial not working

I followed the instructions in the tutorial
I made one change for debugging, here are the files:
mysite1\urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^abc/', include('polls.urls')),
url(r'^polls/', admin.site.urls),
url(r'^admin/', admin.site.urls),
]
File: mysite1\polls\urls.py
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^&', views.index, name='index'),
]
Now if I go to the site http://127.0.0.1:8000/polls/ then it shows the login page same as going to the site http://127.0.0.1:8000/admin/.
However, if I go to the site http://127.0.0.1:8000/abc/ it gives me the following:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/abc
Using the URLconf defined in mysite1.urls, Django tried these URL patterns, in this order:
^abc/
^polls/
^admin/
The current URL, abc, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Can someone guide me what I am doing wrong?
In mysite1\polls\urls.py It should be
url(r'^$', views.index, name='index'),
Notice that your code had an & instead of $. $ indicates a end-of-string match character.
I had a few issues with this, maybe it'll help someone. My VS code was running python 2.7 so the first line on page urls.py in the web project folder (not the polls folder) I had to change to "django.conf.urls import include, url" from "from "django.urls import include, path". I had to include .conf. and urls, path was not working and it was not working without "conf".
Another issue I had on this same page is I had to correct it to "urlpatterns = [url('', include('hello.urls'))..." from "urlpatterns = [path('', include("hello.urls"))". Path was not working so I got this to work.
The third thing is, Notice that there is a '' in the code. It should be blank otherwise when you go to url http://127.0.0.1:8000/ it doesn't redirect to views.py in the polls folder.
I hope this helps someone. Thanks

Django-nested-admin urls not loading

I'm trying to get any sort of nested inline to work with python 3 and django 1.8.
I tried django super-inlines and that didn't really make them nested, they just kinda showed up next to eachother.
I'm currently trying django-nested-admin and I get a page not found error with this pattern search:
^ ^$ [name='browse']
^admin/ ^server-data.js$ [name='nesting_server_data']
browse links to a separate set of urls and this just will not load any admin links.
urls.py:
from django.conf.urls import include, url
import nested_admin
urlpatterns = [
url(r'^', include('flightdb.urls')),
url(r'^admin/', include('nested_admin.urls')),
]
Connecting to /admin/ gets a 404 not found error.
Any help with why the page isn't loading or any better way to get nested-inlines to work would be greatly appreciated.
The pattern r'^' will match anything that also matches r'^admin/'. Django performs the URL matching by starting from the beginning of urlpatterns and progresses until it finds the first match. So in this case things that were supposed to be in admin are matching '^' which is flightdb.urls where they won't be found. You could potentially fix this issue by reordering the url's as follows:
urlpatterns = [
url(r'^admin/', include('nested_admin.urls')),
url(r'^', include('flightdb.urls')),
]
Now admin is checked for a match before the rest of the top level urls.
EDIT: I suspect your issues are coming from using "admin" in your url pattern to go to somewhere other than the default Django admin package. I did some searching around in the source of django-nested-admin and in particular came across something here:
https://github.com/theatlantic/django-nested-admin/blob/master/nested_admin/options.py in particular have a look at line 275:
context = {
'title': _('Add %s') % force_unicode(opts.verbose_name),
'adminform': adminForm,
'is_popup': (IS_POPUP_VAR in request.POST or
IS_POPUP_VAR in request.GET),
'show_delete': False,
'media': mark_safe(media),
'inline_admin_formsets': inline_admin_formsets,
'errors': helpers.AdminErrorList(form, formsets),
'root_path': reverse('admin:index'),
'app_label': opts.app_label,
}
Here there is a call to reverse('admin:index'). This needs to resolve to the usual admin path in order to work. When you have your url's defined like you do you run into the problem of admin now being something other than the default that django-nested-admin is expecting to see.
Perhaps try a different url pattern that is not "admin" such as the one suggested by their documentation:
url(r'^nested_admin/', include('nested_admin.urls')),
This way the reverse used in the internals of the django-nested-admin package will work.
I had the same problem. This is what my settings.py and urls.py look like
settings.py
INSTALLED_APPS = [
'nested_admin',
'django.contrib.admin',
# ...
]
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^nested_admin/', include('nested_admin.urls')),
#...
]
Now, don't go to yourproject.com/nested_admin but yourproject.com/admin .
You still need the include for nested_admin or it won't work.

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

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.

Categories