I am going through Django's tutorials (Django tutorial part 3) and I am at the part where we are adding views and urls.
In this demo app they add subviews for polls, e.g. /polls/34/ to get to the 34th poll. Quoting from the tutorial:
When somebody requests a page from your website – say, “/polls/34/”, Django will load the mysite.urls Python module because it’s pointed to by the ROOT_URLCONF setting. It finds the variable named urlpatterns and traverses the regular expressions in order. After finding the match at '^polls/', it strips off the matching text ("polls/") and sends the remaining text – "34/" – to the ‘polls.urls’ URLconf for further processing. There it matches r'^(?P<question_id>[0-9]+)/$', resulting in a call to the detail() view like so:
My question is as follows:
suppose you have a view that is related to your app, but where the url - prepended by the app name - makes less sense than just the new view by itself. How could you make a view (which is part of the app) start at a new path name.
This might be confusing so here is an example.
Suppose you have an app (decks) for decks users make out of a set of cards. Then /deck/regex would take you to a given deck. If someone wanted to see more information about a card in the deck then /decks/cards/regex makes less sense then /cards/regex as a card can be in multiple decks.
It isn't a perfect example, but I think it highlights what I am trying to do.
I had some type of URL for you:
1st:
# The url in Projects, which you have to include to link your app.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^table/', include('table.urls')),
]
This upper case is your code now:
# So you can change is to:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('table.urls',namespace="table")),
]
And then: in tables.urls.py you can do what ever you want:
urlpatterns = [ url(r'^$', views.index, name='index'),
url(r'^decks/(?P<pk>.*)$', views.decksView, name='decksView'),
url(r'^card/(?P<pk>.*)$', views.cardView, name='cardView'),
]
What you are saying can easily be done but it is not done so because it causes a lot of confusion with regards to routing.
Talking about your decks example. Lets say you are in an app called decks, where root urls.py file is this :
from django.conf.urls import url,include
from django.contrib import admin
from Decks import views as deckviews
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'/cards/regex^$',deckviews.get_card_information),
url(r'^decks/',include('deckviews.urls')),
]
and the urls.py in Decks app can be:
from django.conf.urls import url,include
import views
urlpatterns = [
url(r'^regex/',views.generate_decks)
]
Here is an example where to generate decks, the decks/regex calls the generate random decks from within the urls.py of the decks app meanwhile you get your desired cards/regex which points to the views inside the decks app but still has the url that you wish for.
This ways you can route any url to any function and thats the beauty of django. This is rarely used though as it creates a lot of confusion when the apps get bigger and more complex.
Hope this helps. Cheers!
Related
I have two apps: core and dm
Here's my code:
core/urls.py
urlpatterns = [
path('', views.index, name='index')
]
dm/urls.py
urlpatterns = [
path('', views.dm, name='dm'),
path('prices', views.dm_prices, name='prices')
]
mysite/urls.py
from core import views
from dm import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('core.urls')),
path("stripe/", include("djstripe.urls", namespace="djstripe")),
path('dm', include('dm.urls')),
path('dm/prices', include('dm.urls')),
]
And so, looking from the 404 page, I can see that the URLs he sees are:
admin/
[name='index']
stripe/
dm [name='dm']
dm prices [name='prices']
dm/prices [name='dm']
dm/prices prices [name='prices']
I would be really happy if someone could explain to me how django reads the different URLs and how it orders them.
Thanks a lot!
First of all, you don't need to include urls of same app twice, include('<my_app>.urls') will include all the urls you mentioned in your app's urlpatterns. So 'dm/prices' is redundant in your case, and changing it to this will do the job.
from core import views
from dm import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('core.urls')),
path('dm/', include('dm.urls')),
path("stripe/", include("djstripe.urls", namespace="djstripe")),
]
For more information about include() read this django docs
It's maybe a bit outside of the scope of your question, but is the 404 page the problem you would like to solve?
As Rustam Garayev said path('dm', include('dm.urls')), django includes all urls from that file (the same applies to those in the 'core' path). That's why they appear twice. You essentially created four paths:
<yoursite>/dm/ (namespace dm)
<yoursite>/dm/prices (namespace prices)
<yoursite>/dm/prices (namespace dm)
<yoursite>/dm/prices/prices (namespace prices)
The ordering is the same as in the file that imports the apps' urls, i. e. 1 and 2 are the result from your first include statement, 3 and 4 from the second.
By the way, I realized that you're importing two views functions (core.views and dm.views) and recommend to define an alias in order to avoid name clashes in case you have methods with the same name in them.
from core import views as cviews
from dm import views as dviews
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
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 am pretty new to Django and when I laid my site out I did it in the following way:
The project is a "portal" of sorts,
App 1 "home" is the app that houses the homepage, login, registration
and other "site-wide" functionality
App 2 "inventory" is a basic asset inventory
App 3 "dashboard" is a set of status dashboards based on assets in the inventory
Right now I am trying to add the login functionality and I have a main project urls.py that looks like this:
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('home.urls'), name='home'),
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
)
My plan is to use the inclusion of .../home/urls.py to manage any site-wide functionality such as logging in, registering, etc.
Right now the home index view shows just fine, but anything else in .../home/urls.py gives me a 404
File: /home/urls.py
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^test/$', views.index, name='home_test'),
url(r'^ajax_login/$', views.ajax_login, name='ajax_login'),
url(r'^$', views.index, name='home_index'),
)
At this point I suppose my question is two-fold: Am I approaching this correctly? If so, how can I get the desired functionality? If not, how should I change the way things are set up/laid out to do it the correct "best practices" way?
Thanks in advance!
EDIT
Got it working thanks to dt0xff and holdenweb, accepted holdenweb's answer since it was more accurate including the need to put the home url inclusion below the rest.
Here is my new portal/urls.py file for reference
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
url(r'^', include('home.urls'), name='home'),
)
The issue is with your first pattern, which will only match the empty URL. So any empty URL will cause the home/urls.py URLs to be analyzed, but only the empty one will match even in that.
Unfortunately if there is no common prefix then that pattern "^" will match every URL (since they all start at the beginning ...).
So you should either use a common prefix for all the pages, or put the home URL specification at the end to give the other URLs a chance to match before it is tested.
Django looks in urls like this:
Get list of root urls - portal/urls
Find first, which matches to current url
If it is include, go to included urls, cutting off matched part
Your problem is here
url(r'^$', include('home.urls'), name='home'),
I mean, here
'^$'
You want url to match "start and then end of url". It works good with root(dunno.com/), but not with others, because url will contain something more...
So, just remove $ and you are fine
url(r'^', include('home.urls'), name='home'),
you never need to add regex($) at project/urls.py
I have two URL patterns that both exist in the same application that I'm working on getting set up.
I need urls like the following to work.
http://www.domain.com/p/12345
http://www.domain.com/s/12345
However, both of these live in the same django application.
My main urls.py looks something like this for handling the /p/12345 urls.
urlpatterns = patterns('',
(r'^p/', include('myproject.myapp.urls')),
)
and my urls.py for the application is similar. but this still only handles the /p/12345 urls.
urlpatterns = patterns('myproject.myapp.views',
(r'^(?P<object_id>\d+)/$', 'some_view'),
)
My issue is that both are almost identical but just have a different prefixes. How can I do this for both the /p/12345 and /s/12345 urls. I've read through the documentation but wasn't able to figure this one out. I've thought of 'sloppy' ways to do this with 2 urls.py files, but I know there must be a better way.
You can include a URLs file with an empty pattern. You could do this:
main urls.py
urlpatterns = patterns('',
(r'foo/', 'foo_view'),
(r'^', include('myproject.myapp.urls')),
)
app urls.py
urlpatterns = patterns('puzzlequest.pq.views',
(r'^p/(?P<object_id>\d+)/$', 'some_view'),
(r'^s/(?P<object_id>\d+)/$', 'other_view'),
)
Note that other routes (like foo/) have to come first.