Privacy policyContactsAbout siteOpinionsGitHubDonate
© EVILEG 2015-2018
Recommend hosting

Django - Tutorial 010. Getting the variables in the template file in Django from

simple_tag, SITE_URL, settings

In file in Django we store all the necessary configuration information of our applications, which may include the site's URL or path to certain directories, which store specific files and more information.

And sometimes you want to use these variables in the template, the same site URL. But just to put this constant in the template through {{SITE_URL}} , use this variable will not work. One way of solving this problem is the registration template tag that will perform a specific functionality, for example to take the attribute of the configuration file by its name.

Register template tag

If we are talking about using SITE_URL in the template, the agreement that we have registered this variable in file.


And then we go into the app, which will register the tag to retrieve the attributes of the file. In my case, this is the app home . In this application will need to create templatetags catalog and place it in two files: and such .

We obtain the following structure of the application:


And now look contents.

from django import template
from django.conf import settings

register = template.Library()

def get_attribute(name):
    return getattr(settings, name, "")

Using in template

And the use of the template will be even easier. You will need only to load the template tag to expand and take advantage of this tag, giving it the name of the attribute, which want to receive.

{% load home_extras %}
{% get_attribute 'SITE_URL' %}"

For Django I recommend VDS-server of Timeweb hoster .


Only authorized users can post comments.
Please, Log in or Sign up
Jan. 17, 2019, 11:51 a.m.

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10
Jan. 17, 2019, 11:49 a.m.

C++ - Test 002. Constants

  • Result:50points,
  • Rating points-4
Jan. 17, 2019, 11:13 a.m.

C++ - Тест 003. Условия и циклы

  • Result:28points,
  • Rating points-10
Last comments
Jan. 16, 2019, 8:06 a.m.

Заработало. Забыл model->select(); вписать.
Jan. 16, 2019, 8:02 a.m.

Всё равно пусто, хотя строка с данными в базу добавляется.
Jan. 16, 2019, 7:51 a.m.
Евгений Легоцкой

потому, что нужно сохранять информацию для всех остальных ролей и столбцов через вызов переопределённого метода. Да к тому же вы ещё и зациклили вызов метода data. QVariant MySqlTableModel:...
Jan. 16, 2019, 7:43 a.m.

Сделал вот так. В tableView ничего нет, кроме заголовка. QVariant MySqlTableModel::data(const QModelIndex &index, int role) const{ if (role == Qt::DisplayRole) { QTime ...
Now discuss on the forum
Jan. 18, 2019, 11:26 a.m.

Для Desktop делал так: void pause(int ms){ QTimer timer; timer.setInterval( qBound(1, ms, 3600000) ); timer.setSingleShot(true); QEventLoop loop; QObject::connect(&...
Jan. 17, 2019, 12:01 p.m.
Алексей Внуков

у меня просто есть отдельное поле с чекбоксамими какие колонки нужно отображать CheckBox { id: checkBox text: qsTr("some text") checked: true onC...
Jan. 15, 2019, 4:53 p.m.

Спасибо, заработало.Но выдало обычный текст без форатирования HTML.Придется искать дальше
Jan. 15, 2019, 12:52 p.m.

Я же вам выше написал CLion умеет работать с ремоут машинами. И Qt так же собирает.
Join us in social networks

For registered users on the site there is a minimum amount of advertising