Django: Link To Url In Template When Function Is In A View In Another Directory?
Solution 1:
Django's reverse url resolver has one job. It takes the arguments fed to it(either through the {% url ... %}"
template tag or the urlresolves.reverse
in your python code.
As long as your code is in a valid Django configuration(Below) at start up time the directories templates and views are in isn't relevant. This particular example assumes you've included your apps in the settings.py
configuration and that your root urls.py
includes entries that delegate to the urls.py
files in the individual apps main
and notes
- templates
|
- main.html
- notes.html
- main
|
- views.py
- models.py
- urls.py
- notes
|
- views.py
- models.py
- urls.py
- urls.py- settings.py
What do you need for this to work
so first lets look at main
I have to create a view function in views.py
from django.shortcuts import render
defmain(request):
return render(request, 'main.html', {})
and a urls.py
entry in main/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', main, name='main'),
]
So this is my main view. Lets assume I want to include a <ul>
with links to various notes from my other app. First things first I need to create a note/<note_id>
view. I am using the <note_id>
parameter to address my note entries.
in notes/views.py
I create a notes function that takes an note_id
keyword argument.
defnotes(note_id=None):
#... Get note from database and put in contextreturn render(request, 'notes.html', {})
then create a notes/urls.py
entry for the view we've created.
from django.conf.urls import url, patterns
urlpatterns = patterns(
'main.views',
url(r'^(?P<note_id>[0-9]+)/?$','notes', name='notes'),
)
We then need to pull this all together using the root urls.py
file
from django.conf.urls import include, url
urlpatterns = [
url(r'^main/', include('main.urls')),
url(r'^notes/', include('notes.urls')),
]
So lets stop. What do we have right now? We have a view called main
that binds context
to the main.html
template and returns as a response some markup. We also have a urls.py
entry that tells the web server how to reach that view. We have the exact same thing for a view called notes
in a different app. Finally, we have a root urls.py
that pull my app-specific urls.py
files into the top-level.
What do we want to do? We want to include in main.html
a <ul>
filled with valid url references to our notes.
Since we already have everything setup this is easy we use the {% url ... %}
template tag with the appropriate information to resolve entries from our urls.py
files.
In our main.html
template we simply add something like this...
<ul><li><ahref="{% url "notes" note_id=1 %}">note 1</a></li><li><ahref="{% url "notes" note_id=2 %}">note 2</a></li><li><ahref="{% url "notes" note_id=3 %}">note 3</a></li></ul>
and when main.html
is invoked the markup above will look something like...
<ul><li><ahref="/notes/1">note 1</a></li><li><ahref="/notes/2">note 2</a></li><li><ahref="/notes/3">note 3</a></li></ul>
The important thing to remember here is what url
and urlresolver.reverse
actually do. They take a view name and some arguments and they convert them into a valid url as a string. Its a DRY approach to url management because later if you change notes
to notes_important
or something all of the templates will automatically fix everything for you.
Post a Comment for "Django: Link To Url In Template When Function Is In A View In Another Directory?"