Django - How include() can be used to create plug and play URLs? - python

As mentioned here,
the include() function allows referencing other URLconfs. Note that the regular expressions for the include() function don't have a $ (end-of-string match character) but rather 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.
The idea behind include() is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (polls/URLs.py), they can be placed under /polls/, or under /fun_polls/, or under /content/polls/, or any other path root, and the app will still work.
Need clarification on second point(above) about, include() is to make it easy to plug-and-play URLs,
For one of the app(webapp) in Django project:
webapp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.py
where, root URLconf is pointing to webapp.urls as shown below,
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^webapp/', include('webapp.urls')),
]
here is the complete code
With webapp app, How include() can be used in creating plug and play URLS?

Maybe in a django project, you will register many apps into it, and each will have its own urlconf, at that moment, using the include() will make things simple.
You could just add a prefix for each app and using include() to combine all into root urlConf.
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.
The include() works by import_module(), you could refer to this function at this

In Django URLs, include() lets you refer to other app's URLconfs.
It basically means something like this
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^webapp/', include('webapp.urls')),
]
webapp.urls.py
urlpatterns = [
url(r'^$', views.content),
]
Now, whenever you will call /webapp/ it will render the content view.
Plug-and-Play basically means URL /webapp/ is ready to be called. It will return anything that will come under this URL if you have a view specified for this URL.

Related

How to configure urls.py in django to redirect to another file's urls?

I'm following a django tutorial that is a little outdated and in the urls.py file of the first app's directory we need to configure where to direct django to for any url starting with 'notes/'.
There are two separate 'apps' inside the project. I'm in the first one, not notes.
This is the code currently. I added include to import statement:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(url(r’^notes/‘, include('notes.urls'))),
]
Inside the urlpatterns object, on the first line, path('admin/', admin.site.urls), comes predefined, but I need to add a redirect such that django goes to a different app, called 'notes' and searches there for its entry point, since all of the urls will begin with ‘notes/’.
The tutorial says to use a regular expression and uses this code:
url(r’^notes/‘, include(notes.urls))
so that any url that starts with 'notes/' should be redirected to this other file notes.urls.
However the predefined ones that currently come out of the box with a django project start with path.
I enclosed my notes/n redirect line in path, but not sure if this is correct. Should I instead directly write:
url(r’^notes/‘, include(notes.urls))
Also, do I need to delete the first line provided?
path('admin/', admin.site.urls),
The tutorial has:
urlpatterns = patterns('',
url(r’^notes/‘, include(notes.urls)),
)
and no admin urls line. It's from 2014 I believe.
Simply do:
path('notes/', include('notes.urls'))

Refer to my (reusable) app's url namespace in Django

I want to write a reusable Django application.
I tell my users to add the following to their urls.py
path('slack/', include(('slack_integration.urls', 'slack_integration'), namespace='slack_integration'),
And in my urls.py I want to have a view login_callback.
Now in my view, I need to get a value of slack_integration:login_callback.
I can trust the user that he/she will integrate it with slack_integration prefix and use it. But is this the best practise? Can I somehow get the name of the namespace for the app if user chooses a different name for it?
Thanks a lot!
Using namespace= within urls.py files is no longer supported, as it moves something specific to the Django app outside of the Python package that is the Django app.
The best practice now is to define the app_name within the urls.py file inside the Django app.
The old way: DON'T DO THIS (pre-Django 2.0)
the root urls.py
path('slack/', include(('slack_integration.urls', 'slack_integration'), namespace='slack_integration'),
The new way: DO THIS! (Django 2.0+)
the root urls.py
from django.urls import path, include
urlpatterns = [
path('slack/', include(('slack_integration.urls', 'slack_integration')),
]
slack_integration/urls.py
from django.urls import path
app_name = "slack_integrations"
urlpatterns = [
path('', HomeView.as_view(), name='home'),
]
As you can see, this keeps the namespace for the patterns within the app itself, along with the templates most likely to use it. The days of extra instructions on how to include an app are over! Good luck.

Django included urls doesn't work

Can't figure out why included urls isn't working. Project has 1 my app. So in myproject/mysite/urls.py i have:
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
......
url(r'^sub/', include('subscription.urls')),
url(r'^', include('cms.urls')),
)
Then in myproject/subscription/urls.py:
from django.conf.urls import patterns, url
from .views import subscribe
urlpatterns = patterns(
url(r'^subscribe/', subscribe),
)
If i try to go to http://localhost:8000/lt/sub/subscribe/ it displays 404 page (Page not found). What might be the problem?
EDITED: Project tree:
myproject/
manage.py
media/
static/
subscription/
templates/
__init__.py
admin.py
forms.py
models.py
urls.py
views.py
mysite/
locale/
static/
templates/
__init__.py
settings.py
urls.py
wsgi.py
You have an error in calling the patterns() function:
urlpatterns = patterns(
url(r'^subscribe/', subscribe),
)
patterns() accepts a view prefix as the first argument, if you pass an url() instance, this will not be used as a url pattern. If you had any other url patterns, this would give you an error, but in this specific case patterns() will simply return an empty list.
Since patterns() is deprecated, it is better to switch to the new-style url configuration, and use a list:
urlpatterns = [
url(r'^subscribe/', subscribe),
]
Otherwise you'd have to pass a prefix as the first argument. Since you don't actually use the prefix (you pass a view function, not the import location as a string), this would generally be the empty string ''.
if i try localhost:8000/sub/subscribe, it appends url with /lt/
Thats because your url pattern requires a trailing slash, which you haven't provided
r'^subscribe/' # Shouldn't have the slash or should be optional
Normally this wouldn't be a problem since djangos APPEND_SLASH would help you out (if you're using CommonMiddleware) but it would seem that django-cms has a catch-all redirect applied on your application to redirect to a locale.

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 redirect inside of urls.py

I plan on having several different apps inside my django project, each for a new technology I want to play around with. As I work on each on, I want to have the root URL path redirect to the project I'm working on.
Directory Structure:
backyard/
my_project/
views.py
backyard/
urls.py
backyard/backyard/urls.py:
from django.conf.urls import patterns, include, url
from django.shortcuts import redirect
urlpatterns = patterns('',
url(r'^$', redirect('my_app/')),
url(r'^my_project/$', 'my_project.views.homepage'),
)
backyard/my_project/views.py
def homepage(request):
return render_to_response('my_project/index.html', {'data':data})
When I access the page at http:machine-name:8000/ I get an error saying The included urlconf backyard.urls doesn't have any patterns in it I most definitely have URLs in my urls.py, what is the issue?
A URLconf needs to map regexes to views -- the redirect function doesn't return a view; rather, it returns an HttpResponse. You can look at this question for an example of how to define a redirect directly in the URLconf.

Categories