<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>django | Alex Hildebrandt</title><link>https://alexhildebrandt.de/tag/django/</link><atom:link href="https://alexhildebrandt.de/tag/django/index.xml" rel="self" type="application/rss+xml"/><description>django</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><lastBuildDate>Wed, 20 May 2020 00:00:00 +0000</lastBuildDate><image><url>https://alexhildebrandt.de/images/icon_hu4c6883ab8262edd297260ea54e77bd73_39834_512x512_fill_lanczos_center_2.png</url><title>django</title><link>https://alexhildebrandt.de/tag/django/</link></image><item><title>Django admin backend</title><link>https://alexhildebrandt.de/post/django-admin-backend/</link><pubDate>Wed, 20 May 2020 00:00:00 +0000</pubDate><guid>https://alexhildebrandt.de/post/django-admin-backend/</guid><description>
&lt;div id="create-an-admin-user-for-the-backend" class="section level2">
&lt;h2>Create an admin user for the backend&lt;/h2>
&lt;p>When starting a django project an admin backend is created automatically. To hace access to this backend first a superuser has to be created. Before this can be done you have to migrate databases with the following command (virtual environement activated):&lt;/p>
&lt;pre>&lt;code>pyhton3 manag3.py makemigrations
pyhton3 manag3.py migrate&lt;/code>&lt;/pre>
&lt;p>This ensures that all tables are available in the backend. Next you need to create a superuser with:&lt;/p>
&lt;pre>&lt;code>python3 manage.py createsuperuser&lt;/code>&lt;/pre>
&lt;p>After typing name and password of the backend admin you can launch it.&lt;/p>
&lt;hr />
&lt;/div></description></item><item><title>Django templates</title><link>https://alexhildebrandt.de/post/django/</link><pubDate>Wed, 20 May 2020 00:00:00 +0000</pubDate><guid>https://alexhildebrandt.de/post/django/</guid><description>
&lt;div id="html-templates-in-django-how-to-inherit-layout" class="section level2">
&lt;h2>.html templates in Django &amp;amp; how to inherit layout&lt;/h2>
&lt;p>In Django it is a good practice to inherit layout. Therefor you first need to make a template directory in your app. There you can save a base.html. This site has the base layout of all sites which are extended wwith it. This is how a base.html file can look like:&lt;/p>
&lt;pre>&lt;code> {% load static %}
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang=&amp;quot;en&amp;quot; dir=&amp;quot;ltr&amp;quot;&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta charset=&amp;quot;utf-8&amp;quot;&amp;gt;
&amp;lt;!-- Required meta tags --&amp;gt;
&amp;lt;meta charset=&amp;quot;utf-8&amp;quot;&amp;gt;
&amp;lt;meta name=&amp;quot;viewport&amp;quot; content=&amp;quot;width=device-width, initial-scale=1, shrink-to-fit=no&amp;quot;&amp;gt;
&amp;lt;!-- Bootstrap CSS --&amp;gt;
&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css&amp;quot; integrity=&amp;quot;sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm&amp;quot; crossorigin=&amp;quot;anonymous&amp;quot;&amp;gt;
&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; href=&amp;quot;{% static &amp;#39;blog/main.css&amp;#39; %}&amp;quot;&amp;gt;
{% if title %}
&amp;lt;title&amp;gt; Django Blog - {{ title }} &amp;lt;/title&amp;gt;
{% else %}
&amp;lt;title&amp;gt; Django Blog &amp;lt;/title&amp;gt;
{% endif %}
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;header class=&amp;quot;site-header&amp;quot;&amp;gt;
&amp;lt;nav class=&amp;quot;navbar navbar-expand-md navbar-dark bg-steel fixed-top&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;container&amp;quot;&amp;gt;
&amp;lt;!-- clicking on django blog in the nav bar will go to blog-home of urls.py - blog --&amp;gt;
&amp;lt;a class=&amp;quot;navbar-brand mr-4&amp;quot; href=&amp;quot;{% url &amp;#39;blog-home&amp;#39; %}&amp;quot;&amp;gt;Django Blog&amp;lt;/a&amp;gt;
&amp;lt;button class=&amp;quot;navbar-toggler&amp;quot; type=&amp;quot;button&amp;quot; data-toggle=&amp;quot;collapse&amp;quot; data-target=&amp;quot;#navbarToggle&amp;quot; aria-controls=&amp;quot;navbarToggle&amp;quot; aria-expanded=&amp;quot;false&amp;quot; aria-label=&amp;quot;Toggle navigation&amp;quot;&amp;gt;
&amp;lt;span class=&amp;quot;navbar-toggler-icon&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;
&amp;lt;/button&amp;gt;
&amp;lt;div class=&amp;quot;collapse navbar-collapse&amp;quot; id=&amp;quot;navbarToggle&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;navbar-nav mr-auto&amp;quot;&amp;gt;
&amp;lt;!-- clicking on django blog in the nav bar will go to blog-home of urls.py - blog --&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;{% url &amp;#39;blog-home&amp;#39; %}&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;!-- clicking on django blog in the nav bar will go to blog-about of urls.py - blog --&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;{% url &amp;#39;blog-about&amp;#39; %}&amp;quot;&amp;gt;About&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;!-- Navbar Right Side --&amp;gt;
&amp;lt;div class=&amp;quot;navbar-nav&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Login&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Register&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/nav&amp;gt;
&amp;lt;/header&amp;gt;
&amp;lt;main role=&amp;quot;main&amp;quot; class=&amp;quot;container&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;row&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;col-md-8&amp;quot;&amp;gt;
{% block content %}{% endblock %}
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;col-md-4&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content-section&amp;quot;&amp;gt;
&amp;lt;h3&amp;gt;Our Sidebar&amp;lt;/h3&amp;gt;
&amp;lt;p class=&amp;#39;text-muted&amp;#39;&amp;gt;You can put any information here you&amp;#39;d like.
&amp;lt;ul class=&amp;quot;list-group&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;list-group-item list-group-item-light&amp;quot;&amp;gt;Latest Posts&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;list-group-item list-group-item-light&amp;quot;&amp;gt;Announcements&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;list-group-item list-group-item-light&amp;quot;&amp;gt;Calendars&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;list-group-item list-group-item-light&amp;quot;&amp;gt;etc&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/main&amp;gt;
&amp;lt;!-- Optional JavaScript --&amp;gt;
&amp;lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&amp;gt;
&amp;lt;script src=&amp;quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&amp;quot; integrity=&amp;quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&amp;quot; crossorigin=&amp;quot;anonymous&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src=&amp;quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js&amp;quot; integrity=&amp;quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&amp;quot; crossorigin=&amp;quot;anonymous&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src=&amp;quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&amp;quot; integrity=&amp;quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&amp;quot; crossorigin=&amp;quot;anonymous&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/code>&lt;/pre>
&lt;p>The file refers to online available bootstrap and CSS stylesheets as well as to &lt;em>JacaScript&lt;/em> files.&lt;/p>
&lt;pre>&lt;code>&amp;lt;!-- Bootstrap CSS --&amp;gt;
&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css&amp;quot; integrity=&amp;quot;sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm&amp;quot; crossorigin=&amp;quot;anonymous&amp;quot;&amp;gt;
&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; href=&amp;quot;{% static &amp;#39;blog/main.css&amp;#39; %}&amp;quot;&amp;gt;
&amp;lt;!-- Optional JavaScript --&amp;gt;
&amp;lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&amp;gt;
&amp;lt;script src=&amp;quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&amp;quot; integrity=&amp;quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&amp;quot; crossorigin=&amp;quot;anonymous&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src=&amp;quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js&amp;quot; integrity=&amp;quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&amp;quot; crossorigin=&amp;quot;anonymous&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src=&amp;quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&amp;quot; integrity=&amp;quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&amp;quot; crossorigin=&amp;quot;anonymous&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code>&lt;/pre>
&lt;p>It has a navigation bar in the header of the page:&lt;/p>
&lt;pre>&lt;code>&amp;lt;header class=&amp;quot;site-header&amp;quot;&amp;gt;
&amp;lt;nav class=&amp;quot;navbar navbar-expand-md navbar-dark bg-steel fixed-top&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;container&amp;quot;&amp;gt;
&amp;lt;!-- clicking on django blog in the nav bar will go to blog-home of urls.py - blog --&amp;gt;
&amp;lt;a class=&amp;quot;navbar-brand mr-4&amp;quot; href=&amp;quot;{% url &amp;#39;blog-home&amp;#39; %}&amp;quot;&amp;gt;Django Blog&amp;lt;/a&amp;gt;
&amp;lt;button class=&amp;quot;navbar-toggler&amp;quot; type=&amp;quot;button&amp;quot; data-toggle=&amp;quot;collapse&amp;quot; data-target=&amp;quot;#navbarToggle&amp;quot; aria-controls=&amp;quot;navbarToggle&amp;quot; aria-expanded=&amp;quot;false&amp;quot; aria-label=&amp;quot;Toggle navigation&amp;quot;&amp;gt;
&amp;lt;span class=&amp;quot;navbar-toggler-icon&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;
&amp;lt;/button&amp;gt;
&amp;lt;div class=&amp;quot;collapse navbar-collapse&amp;quot; id=&amp;quot;navbarToggle&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;navbar-nav mr-auto&amp;quot;&amp;gt;
&amp;lt;!-- clicking on django blog in the nav bar will go to blog-home of urls.py - blog --&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;{% url &amp;#39;blog-home&amp;#39; %}&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;!-- clicking on django blog in the nav bar will go to blog-about of urls.py - blog --&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;{% url &amp;#39;blog-about&amp;#39; %}&amp;quot;&amp;gt;About&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;!-- Navbar Right Side --&amp;gt;
&amp;lt;div class=&amp;quot;navbar-nav&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Login&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Register&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/nav&amp;gt;
&amp;lt;/header&amp;gt;&lt;/code>&lt;/pre>
&lt;p>And the main page consists of a sidebar and the individual content of each site:&lt;/p>
&lt;pre>&lt;code>&amp;lt;main role=&amp;quot;main&amp;quot; class=&amp;quot;container&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;row&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;col-md-8&amp;quot;&amp;gt;
{% block content %}{% endblock %}
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;col-md-4&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content-section&amp;quot;&amp;gt;
&amp;lt;h3&amp;gt;Our Sidebar&amp;lt;/h3&amp;gt;
&amp;lt;p class=&amp;#39;text-muted&amp;#39;&amp;gt;You can put any information here you&amp;#39;d like.
&amp;lt;ul class=&amp;quot;list-group&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;list-group-item list-group-item-light&amp;quot;&amp;gt;Latest Posts&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;list-group-item list-group-item-light&amp;quot;&amp;gt;Announcements&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;list-group-item list-group-item-light&amp;quot;&amp;gt;Calendars&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;list-group-item list-group-item-light&amp;quot;&amp;gt;etc&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/main&amp;gt;&lt;/code>&lt;/pre>
&lt;p>Thereby the content of each site is refered with:&lt;/p>
&lt;pre>&lt;code>&amp;lt;div class=&amp;quot;col-md-8&amp;quot;&amp;gt;
{% block content %}{% endblock %}
&amp;lt;/div&amp;gt;&lt;/code>&lt;/pre>
&lt;p>The individual sites are also stored in the templates directory of your app and are extended with the base.html file. Sytax look like this:&lt;/p>
&lt;pre>&lt;code>{% extends &amp;quot;blog/base.html&amp;quot; %}
{% block content %}
&amp;lt;h1&amp;gt;About page!&amp;lt;/h1&amp;gt;
{% endblock content %}&lt;/code>&lt;/pre>
&lt;p>The navabar header is referencing his links with:&lt;/p>
&lt;pre>&lt;code>&amp;lt;div class=&amp;quot;navbar-nav mr-auto&amp;quot;&amp;gt;
&amp;lt;!-- clicking on django blog in the nav bar will go to blog-home of urls.py - blog --&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;{% url &amp;#39;blog-home&amp;#39; %}&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;!-- clicking on django blog in the nav bar will go to blog-about of urls.py - blog --&amp;gt;
&amp;lt;a class=&amp;quot;nav-item nav-link&amp;quot; href=&amp;quot;{% url &amp;#39;blog-about&amp;#39; %}&amp;quot;&amp;gt;About&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;&lt;/code>&lt;/pre>
&lt;p>Thereby href=“{% url ‘blog-home’ %} refers to the urls.py file of the app. Syntax of the urls.py file looks like this:&lt;/p>
&lt;pre>&lt;code>from django.urls import path
from . import views
urlpatterns = [
path(&amp;#39;&amp;#39;, views.home, name=&amp;#39;blog-home&amp;#39;),
path(&amp;#39;about/&amp;#39;, views.about, name=&amp;#39;blog-about&amp;#39;),
]&lt;/code>&lt;/pre>
&lt;p>This file itself refers to the views.py file in the app directory. This file contains this code:&lt;/p>
&lt;pre>&lt;code> from django.shortcuts import render
posts = [
{
&amp;#39;author&amp;#39;:&amp;#39;Alex&amp;#39;,
&amp;#39;title&amp;#39; : &amp;#39;Blog Post 1&amp;#39;,
&amp;#39;content&amp;#39;: &amp;#39;First post content&amp;#39;,
&amp;#39;date_posted&amp;#39;: &amp;#39;May 20, 2020&amp;#39;
},
{
&amp;#39;author&amp;#39;:&amp;#39;Bettina&amp;#39;,
&amp;#39;title&amp;#39; : &amp;#39;Blog Post 2&amp;#39;,
&amp;#39;content&amp;#39;: &amp;#39;second post content&amp;#39;,
&amp;#39;date_posted&amp;#39;: &amp;#39;May 21, 2020&amp;#39;
}
]
def home(request):
context = {
&amp;#39;posts&amp;#39;: posts
}
return render(request, &amp;#39;blog/home.html&amp;#39;, context)
def about(request):
return render(request, &amp;#39;blog/about.html&amp;#39;, {&amp;#39;title&amp;#39;: &amp;#39;About&amp;#39;})
&lt;/code>&lt;/pre>
&lt;p>Each app has its own urls.py file. And the django project refers to this urls in the settings.py file in the project directory with this code:&lt;/p>
&lt;pre>&lt;code>....
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path(&amp;#39;admin/&amp;#39;, admin.site.urls),
path(&amp;#39;&amp;#39;, include(&amp;#39;blog.urls&amp;#39;)),
]
....&lt;/code>&lt;/pre>
&lt;hr />
&lt;div id="references-youtube-django-series" class="section level3">
&lt;h3>References: &lt;a href="https://youtu.be/qDwdMDQ8oX4">Youtube django series&lt;/a>&lt;/h3>
&lt;hr />
&lt;/div>
&lt;/div></description></item></channel></rss>