What does href="{% url 'profile' %} mean? - python

Update 2
I think my confusion came because I did not account for the fact that my project django_project3 is the default project.
Update
I looked at Using {% url ??? %} in django templates but it didn't fully answer my question.
Original Post
I tried to Google my question but ended up with 1000s of pages of Python theory and no straight answer.
I have a project called Blog and in it there is a file called base.html. And it contains the line of code <a class="nav-item nav-link" href="{% url 'profile' %}">Home</a>.
But in Blogs urls.py file there is no URL pattern called profile.
Whereas in my project django_project3, in the urls.py file there is a URL pattern called profile.
Does the percentage sign get the code to look in every urls.py file?

url is a built-in template tag to avoid hard-coding.
{% %} is template render syntax

Related

How to make djangocms-admin-style logo dynamic?

I'm using django-cms admin style. I managed to change the default DjangoCMS logo, by following the solution mentioned here:
Django cms 3.4.1 admin dlogo
Now the logo is a static one, but I want it to be dynamic, meaning it should get the image path from the database, where the location is stored.
As these admin pages are not render through views.py, I'm not able to sent the querysets to it.
Can anyone suggest how to do it?
using context_processors we can do this.
first need to get this: https://github.com/divio/djangocms-admin-style/blob/master/djangocms_admin_style/templates/admin/inc/branding.html
branding.html file must be put inside templates folder under admin/inc folder, so the structure will be like this templates/admin/inc/branding.html
now suppose through context_processor we got company_logo, that holds the logo url from database.
then in branding.html <div id="header-logo"> would be like:
<div id="header-logo">
{% if company_logo %}
<img src="{{ company_logo.url }}" style="height:inherit;">
{% else %}
<a class="icon-logo" href="/"><span>django CMS</span></a>
{% endif %}
</div>

Django - Creating an if statement based on the request path : not working

I'm trying to create a div within my view that should only be generated if both the words 'org' and 'dashboard' are in the page URL. I've looked at other similar questions on SO but they the solutions don't seem to work for me.
I have also tried putting the phrases org and dashboard within quotation marks which will actually display the div on the page; however this will be displayed on the page regardless of whether those phrases are in the page URL.
HTML:
{% if org and dashboard in request.path %}
<div id='url-req-brn'>
<div class='max margins'>
<h4 id='url-req-brn-h4'>You must be logged in to view the requested page.<h4>
</div>
</div>
{% endif %}
Thanks
Update:
Current solutions provide clarity as to the mistake I made in the HTML, however the div still doesn't display when I have a URL containing 'org' and 'dashboard'.
Do I have to define what 'request.path' means in my views.py file?
You'll have to quote org and dashboard as strings, and then test their containment separately:
{% if 'org' in request.path and 'dashboard' in request.path %}
The syntax follows that of plain Python.
Reference
Template complex expressions

How does {% load url from future %} and namespaces work in Django?

Can someone show me an example (plus a small explanation) of how {% load url from future %} and namespace concept works?
I'm new in python and django and i need to learn how not to make hardcoded urls and also how to use other functions like reverse().
Here is an example of what i'm trying to do:
urls.py
urlpatterns = patterns('',
"""
This one is what i did first but works with hardcoded url inside
top-navigator.html:
url(r'^books/$', 'books.views.book_index'),
The next one is what i'm trying to do:
(but of course is not correct)
"""
url(r'^books/$', include('books.views.book_index', namespace='books')),
)
top-navigator.html
when i'm trying to run the server is shows the error:
Caught ImportError while rendering: No module named book_index
{% load url from future %}
<div class="navbar-inner">
<ul class="nav">
<li class="active">Home</li>
<li>Books</li>
<li>Authors</li>
<li>Publishers</li>
<li>Contact</li>
</ul>
</div>
What can i do in order do to something similar for all the links?
Thanks in advance.
To use namespaces and the include statement, you must import another urls.py file with patterns in it.
You can't just include a view as you've done here.
Change your code to
{% url 'book_index' %}"
url(r'^books/$', 'books.views.book_index', name='books'))
Or to use namespaces for illustration purposes:
more_patterns = patterns('',
url(r'^$', 'book.views.book_index', name='book_index')),
)
urlpatterns = patterns('',
url(r'^books/', include(more_patterns, namespace='books')),
#^^^^^^^ note you would normally use a string that points
# to a python file containing urls.
)
{% url 'books:book_index' %} will now resolve.

Django dynamic links

I'm new to Django so this could be a stupid question, however I'm struggling to find an answer searching Goolge. my question is as follows...
In other languages when creating links within HTML pages you have something like a buildURL(page) function that creates a dynamic link i.e.
<a herf="buildURL(pageName,queryObjects) >link</a>
This would generate the URL with parameters.
Is there a function in Django that builds URLs in the correct format i.e. generate a fully-qualified URL to a Django page with parameters?
In your templates, you never should hard write url view. Instead of this, a good practice is to named each url and reference this name in templates.
To name a url:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
#...
url(r'^article/(\d+)/$', 'news.views.article', name='article_by_id'),
#...
)
To reference url in template:
<ul>
{% for article in articles %}
<li>Article {{article.name}} </li>
{% endfor %}
</ul>
Learn more in URL Distpatchar django documentation.
from what I see in the django documentation it should be
<ul>
{% for article in articles %}
<li>Article {{article.name}} </li>
{% endfor %}
</ul>
i.e. 'article_by_id' instead of article_by_id. This works for me

How does one put a link / url to the web-site's home page in Django?

In Django templates, is there a variable in the context (e.g. {{ BASE\_URL }}, {{ ROOT\_URL }}, or {{ MEDIA\_URL }} that one can use to link to the home url of a project?
I.e. if Django is running in the root of a project, the variable (let's call it R) {{ R }} in a template would be /. If the root url is a sub-folder http://host/X/ the variable {{ R }} would be /X/ (or http://host/X/).
It seems painfully simple, but I can't find an answer. :) Thank you!
You could give the URL configuration which you're using to handle the home page a name and use that:
urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('myproject.views',
url(r'^$', 'index', name='index'),
)
Templates:
<a href="{% url index %}">...
UPDATE: Newer versions of Django require quotation marks around the name of the view:
<a href="{% url 'index' %}">...
This note in the Django Book has some tips about deploying your applications to a subdirectory:
http://www.djangobook.com/en/1.0/chapter20/#cn43
I always use something like <a href="/"> (assuming your home is at the root, of course). I seem to recall looking this up once, and couldn't find a Django variable for this path; at any rate, / seemed pretty easy, anyway.
In your admin, go to "sites" and set the domain.
Pass context_instance=RequestContext(request) to the templates in question.
Now use {{ SITE_URL }} in any of those templates and you're golden.
Chapter 10 of the Django Book has more information than you'll need regading that context processor bit.
(r'^$', 'django.views.generic.simple.redirect_to', {'url': '/home/'}),
works fine :)

Categories