python - Django: link to url in template when function is in a view in another directory? -
i'm trying display comments in template this:
{{ url note.note }} {{ url note.note_by }}
my problem template in directory view "requests" notes are. how link view in directory (or more specifically; note.note inside function in views.py in directory)?
in views.py in same directory template, linking template this:
def blah(request): return render(request, 'site/blah.html')
and in views.py request , save comments (note.note) this:
def messages(request): if request.method == "post": new_note_text = request.post.get('new_note') new_note = note() new_note.note = new_note_text new_note.note_by = request.user new_note.note_datetime = timezone.now() new_note.save() return render(request, 'theme/messages.html', context)
edit:
urls.py in same directory template:
from django.conf.urls import include, patterns, url site import views urlpatterns = patterns('', url(r'^test$', views.blah, name='blah'), )
settings.py:
import os base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) secret_key = '' # security warning: don't run debug turned on in production! debug = true allowed_hosts = [] # application definition installed_apps = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app', 'theme', 'site', ) middleware_classes = ( 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.auth.middleware.sessionauthenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', 'django.middleware.security.securitymiddleware', ) root_urlconf = 'app.urls' templates = [ { 'backend': 'django.template.backends.django.djangotemplates', 'dirs': [], 'app_dirs': true, 'options': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] wsgi_application = 'appen.wsgi.application' databases = { 'default': { 'engine': 'django.db.backends.sqlite3', 'name': os.path.join(base_dir, 'db.sqlite3'), } } language_code = 'en-us' time_zone = 'europe' use_i18n = true use_l10n = true use_tz = true static_url = '/static/'
django's reverse url resolver has 1 job. takes arguments fed it(either through {% url ... %}"
template tag or urlresolves.reverse
in python code.
as long code in valid django configuration(below) @ start time directories templates , views in isn't relevant. particular example assumes you've included apps in settings.py
configuration , root urls.py
includes entries delegate urls.py
files in individual apps main
, 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 need work
so first lets @ main
have create view function in views.py
from django.shortcuts import render def main(request): return render(request, 'main.html', {})
and urls.py
entry in main/urls.py
from django.conf.urls import url . import views urlpatterns = [ url(r'^$', main, name='main'), ]
so main view. lets assume want include <ul>
links various notes other app. first things first need create note/<note_id>
view. using <note_id>
parameter address note entries.
in notes/views.py
create notes function takes note_id
keyword argument.
def notes(note_id=none): #... note database , put in context return render(request, 'notes.html', {})
then create notes/urls.py
entry 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 need pull using 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. have right now? have view called main
binds context
main.html
template , returns response markup. have urls.py
entry tells web server how reach view. have exact same thing view called notes
in different app. finally, have root urls.py
pull app-specific urls.py
files top-level.
what want do? want include in main.html
<ul>
filled valid url references our notes.
since have setup easy use {% url ... %}
template tag appropriate information resolve entries our urls.py
files.
in our main.html
template add this...
<ul> <li> <a href="{% url "notes" note_id=1 %}">note 1</a></li> <li> <a href="{% url "notes" note_id=2 %}">note 2</a></li> <li> <a href="{% url "notes" note_id=3 %}">note 3</a></li> </ul>
and when main.html
invoked markup above like...
<ul> <li> <a href="/notes/1">note 1</a></li> <li> <a href="/notes/2">note 2</a></li> <li> <a href="/notes/3">note 3</a></li> </ul>
the important thing remember here url
, urlresolver.reverse
do. take view name , arguments , convert them valid url string. dry approach url management because later if change notes
notes_important
or of templates automatically fix you.
Comments
Post a Comment