Django URL pattern doesn't work - python

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

Related

Your URL pattern [<URLPattern> ] is invalid. DJANGO Urls.py file

I am getting this error:
ERRORS:
app_1 | ?: (urls.E004) Your URL pattern [<URLPattern '^static/media/(?P<path>.*)$'>] is invalid. Ensure that urlpatterns is a list of path() and/or re_path() instances.
app_1 | ?: (urls.E004) Your URL pattern [<URLPattern '^static/static/(?P<path>.*)$'>] is invalid. Ensure that urlpatterns is a list of path() and/or re_path() instances.
I don't know why I am getting this error. I have correctly added everything in my url patterns
urlpatterns = [
path("admin/", admin.site.urls),
path("api/v1/jobs/", include("jobs.urls")),
path("api/v1/company/", include("company.urls")),
]
if settings.DEBUG:
urlpatterns.extend(
[
static("/static/static/", document_root="/vol/web/static"),
static("/static/media/", document_root="/vol/web/media"),
path("__debug__/", include(debug_toolbar.urls)),
]
)
The function static() returns a list already, not a single pattern. It could be that the list contains a single path element, but it is a list nonetheless.
I suggest you change your code to:
if settings.DEBUG:
# add multiple paths using 'extend()'
urlpatterns.extend(static("/static/static/", document_root="/vol/web/static"))
urlpatterns.extend(static("/static/media/", document_root="/vol/web/media"))
# add a single path using 'append()'
urlpatterns.append(path("__debug__/", include(debug_toolbar.urls)))

Django : Cannot resolve the URLs pattern : Always Index page content is displayed

I am learning Django. I build an app in which I am setting up the URL patterns but in all cases it's displaying the data for the index page.
appTwo urls.py File :
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.users, name='users'),
url(r'^$', views.help, name='help'),
]
MyProject urls.py file :
urlpatterns = [
url(r'^$', views.index, name='index'),
url('admin/', admin.site.urls),
url(r'^users/', include('appTwo.urls')),
url(r'^help/', include('appTwo.urls')),
]
If I call /users or /help, the browser display the data for index file only.
Is there something with a regex that I am doing wrong?
You're setting your urlpatterns in a wrong way. First of all, in you project's urls.py file you are saying that if a request is sent to /users or /help, then Django should look into your appTwo.urls. When Django gets there, it finds that the urlpatters are set so that everything that's empty after any of the aforementioned urls must be handled by the views.index, views.users and views.help. But as views.index is the first one in the list, then all the request end up being handled by that view.
By the way, if you're using Django >= 2.0 you no longer need to use the url() function, but the path() one instead, for which you can declare the paths as simple strings rather than regular expressions.
You should have something as follows:
MyProject/urls.py
from django.urls import path
urlpatterns = [
path('', include('appTwo.urls')),
path('admin/', admin.site.urls)
]
MyApp/urls.py
from django.urls import path
urlpatterns = [
path('', views.index, name='index'),
path('users/', views.users, name='users'),
path('help/', views.help, name='help'),
]
when django reaches here (in your apps url) while resolving the urls, see all three urls have empty regex as first param (so the first one will be selected), And as you see its index then you always show index page. You need to differentiate the urls. Something like this.
urlpatterns = [
url(r'^users/$', views.users, name='users'),
url(r'^help/$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
I think you also cant write url index with empty regex at top of other urls, otherwise it will always be selected.

Django url parameter error

Here is my problem :
Using the URLconf defined in Rase.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
bio/<slug:username>/$ [name='bio']
The current path, bio/bussiere/, didn't match any of these.
the url is :
http://localhost:8000/bio/bussiere/
here is my url file :
from django.urls import include, path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path("bio/<slug:username>/$", views.bio, name='bio'),
]
If you have any idea ?
Thanks and regards
You are using Django 2.0.X where url changed pretty much. You don't need to use regex for url when using path() just remove the trailing $ . So the url should be-
path("bio/<slug:username>/", views.bio, name='bio')

Is it better to use path() or url() in urls.py for django 2.0?

In a django online course, the instructor has us use the url() function to call views and utilize regular expressions in the urlpatterns list. I've seen other examples on youtube of this.
e.g.
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
#and in polls/urls.py
urlpatterns = [
url(r'^$', views.index, name="index"),
]
However, in going through the Django tutorial, they use path() instead e.g.:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
Furthermore regular expressions don't seem to work with the path() function as using a path(r'^$', views.index, name="index") won't find the mysite.com/polls/ view.
Is using path() without regex matching the proper way going forward? Is url() more powerful but more complicated so they're using path() to start us out with? Or is it a case of different tools for different jobs?
From Django documentation for url
url(regex, view, kwargs=None, name=None) This function
is an alias to django.urls.re_path(). It’s likely to be deprecated in
a future release.
Key difference between path and re_path is that path uses route without regex
You can use re_path for complex regex calls and use just path for simpler lookups
The new django.urls.path() function allows a simpler, more readable URL routing syntax. For example, this example from previous Django releases:
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive)
could be written as:
path('articles/<int:year>/', views.year_archive)
The django.conf.urls.url() function from previous versions is now available as django.urls.re_path(). The old location remains for backwards compatibility, without an imminent deprecation. The old django.conf.urls.include() function is now importable from django.urls so you can use:
from django.urls import include, path, re_path
in the URLconfs. For further reading django doc
path is simply new in Django 2.0, which was only released a couple of weeks ago. Most tutorials won't have been updated for the new syntax.
It was certainly supposed to be a simpler way of doing things; I wouldn't say that URL is more powerful though, you should be able to express patterns in either format.
Regular expressions don't seem to work with the path() function with the following arguments: path(r'^$', views.index, name="index").
It should be like this: path('', views.index, name="index").
The 1st argument must be blank to enter a regular expression.
Path is a new feature of Django 2.0.
Explained here :
https://docs.djangoproject.com/en/2.0/releases/2.0/#whats-new-2-0
Look like more pythonic way, and enable to not use regular expression in argument you pass to view... you can ue int() function for exemple.
From v2.0 many users are using path, but we can use either path or url.
For example in django 2.1.1
mapping to functions through url can be done as follows
from django.contrib import admin
from django.urls import path
from django.contrib.auth import login
from posts.views import post_home
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^posts/$', post_home, name='post_home'),
]
where posts is an application & post_home is a function in views.py

In Django, how can I understand include() function?

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

Categories

Resources