<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Elias Prado on Medium]]></title>
        <description><![CDATA[Stories by Elias Prado on Medium]]></description>
        <link>https://medium.com/@pradoelias133?source=rss-a6be71af789e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KHD6c8KMR-Knr4HP.</url>
            <title>Stories by Elias Prado on Medium</title>
            <link>https://medium.com/@pradoelias133?source=rss-a6be71af789e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:51:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pradoelias133/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create A Blog Using Django]]></title>
            <link>https://medium.com/@pradoelias133/create-a-blog-using-django-507dfaff254e?source=rss-a6be71af789e------2</link>
            <guid isPermaLink="false">https://medium.com/p/507dfaff254e</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Elias Prado]]></dc:creator>
            <pubDate>Sun, 13 Nov 2022 01:03:04 GMT</pubDate>
            <atom:updated>2022-11-14T14:27:12.296Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Create A Blog App Using Django</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k_I2SecYinEbbuvD8g5TzA.png" /></figure><p>Django is a full-stack framework that allows developers to create new applications quickly using its MVT/MVC patterns. Therefore, this post will exemplify how you can create a blog application using Django and all its functionalities, allowing you to understand how model, view and controller work together with templates.</p><h4>Wha you will learn in this post:</h4><ul><li>How to create a python environment.</li><li>Install django in the environment.</li><li>Initiate a django project.</li><li>Model creation.</li><li>View to display the data.</li><li>Urls, template and many more…</li></ul><h4>You will need the following technologies to initiate this project</h4><ul><li>Python and pip</li><li>An IDE such as VSCODE</li></ul><h4><strong>Getting started with the django blog project</strong></h4><p>To begin this django project you will need to open your IDE and add an empty folder to it. I called this folder as a <strong>blog-project</strong> but you can name it as you wish.</p><p>Once you added the project into your IDE make sure that the terminal is in the right directory to begin the project. Since we need to add the code within it by the terminal using <strong>pip.</strong></p><h4><strong>1- Preparing the environment</strong></h4><p>In this part you will create a virtual environment to isolate the project from others virtual environments using a python command:</p><pre>$ python3 -m venv venv</pre><p>Once you have added the <strong>venv </strong>folder into your root directory you will need to activate it by the following command:</p><pre>$ source venv/bin/activate</pre><p>If you have done the following approach correctly your <strong>path directory </strong>will have the name of the virtual environment on the beginning of it. For example, if your path directory is <strong>computer-name/blog-project $</strong>, it will be changed to <strong>(venv) computer-name/blog-project $</strong>.</p><h4><strong>2- Installing Django framework</strong></h4><p>Now that you have the environment ready you have to install the django framework into it. Into your terminal add the following command:</p><pre>$ pip install django</pre><p>Once it is done, django will be installed, and you just need to create the blog-project within the root directory.</p><h4><strong>3- Creating the blog-project</strong></h4><p>Now that all the initial configuration is done, let’s start the project. You will need to add the following command to create the project into the main directory:</p><pre>$ django-admin startproject blog_project</pre><p>As soon as you add the following command into your terminal, a folder called <strong>blog_project </strong>will be added to your directory with another blog_project folder into it as the django’s controller and the manage.py file.</p><p>Therefore, if you have followed all the steps explained until now, your directory should look like this:</p><pre>blog-project/ <br> |<br> |__blog_project/<br> |         |<br> |         |__blog_project/<br> |         |     |___ __init__.py<br> |         |     |____settings.py<br> |         |     |____urls.py<br> |         |     |____wsgy.py<br> |         | <br> |         |__manage.py<br> |         <br> |<br> |__venv/</pre><h4><strong>4- Initiate your project</strong></h4><p>Now that all the main settings are already done, you can initiate your project. Go to the <strong>blog_project/ </strong>root<strong> </strong>folder with the command:</p><pre>$ cd blog_project</pre><p>Just a point here: For those who are beginners, make sure that you have three different folders: the root folder called <strong>blog-project/, </strong>the django project root folder called <strong>blog_project/ </strong>and the <em>controllers folder</em> within the django root folder which is also called <strong>blog_project/</strong>. The command above will direct you to the next folder<strong>, </strong>the django root <strong>blog_project/.</strong></p><p>Therefore, once you are within the django project root folder, (the same as the <strong>manage.py</strong>) file you can run the command to start your django project with the following command:</p><pre>$ python manage.py runserver</pre><p>With the following command your project will be initiated and in the terminal it will run and show which port the project is running:</p><pre>(venv) computer-name/blog_project $ python manage.py runserver<br>Watching for file changes with StatReloader<br>Performing system checks...<br><br>System check identified no issues (0 silenced).<br><br>You have 18 unapplied migration(s). Your project may not work properly <br>until you apply the migrations for app(s): admin, auth, contenttypes, <br>sessions.<br>Run &#39;python manage.py migrate&#39; to apply them.<br>February 07, 2021 - 17:52:22<br>Django version 3.1.6, using settings &#39;blog_project.settings&#39;<br>Starting development server at http://127.0.0.1:8000/<br>Quit the server with CONTROL-C.</pre><p>In my case the adress is 127.0.0.1:8000. Click on it or copy and paste into your browse to see the outcome:</p><figure><img alt="django launch." src="https://cdn-images-1.medium.com/max/1024/0*Zs7jzL0t0_tCxwiN" /><figcaption>Django initial display telling that the framework was proper created.</figcaption></figure><p>If everything went well you could see the image above from the address the terminal provided you.</p><h4><strong>5- Creating a new app for your django project</strong></h4><p>Now is time to create your first django app. But before you add the command to create the app, you need to open a new terminal to run the project, since the last terminal is being used to run the application.</p><p>Therefore, open a new terminal into IDE, activate again the virtual environment in the root directory and change the directory to the django root, which is a folder forward.</p><p>So, after activating your virtual environment and changed the directory again into your second terminal, add the following code to create the app:</p><pre>$ python manage.py startapp blog</pre><p>Now, after you have created the blog post your directory should look like this:</p><pre>blog-project/ <br> |  <br> |   <br> |___blog_project/<br> |   | <br> |   |__blog_project/<br> |   |    |__ __init__.py<br> |   |    |__settings.py<br> |   |    |__urls.py<br> |   |    |__wsgy.py<br> |   |<br> |   |<br> |   |___blog/<br> |   |   |<br> |   |   |__migrations/<br> |   |   |___ __init__.py<br> |   |   |___admin.py<br> |   |   |___apps.py<br> |   |   |___models.py<br> |   |   |___views.py<br> |   |<br> |   |__manage.py<br> |<br> |__venv/</pre><p>Now, you need to add the <strong>blog </strong>app in the project settings. Therefore, under the <strong>blog_project/settings.py </strong>add the blog in the INSTALLED_APP configuration:</p><pre>INSTALLED_APPS = [<br>&#39;django.contrib.admin&#39;,<br>&#39;Django.contrib.auth&#39;,<br>&#39;django.contrib.contenttypes&#39;, <br>&#39;django.contrib.sessions&#39;, <br>&#39;Django.contrib.messages&#39;,<br>&#39;Django.contrib.staticfiles&#39;,<br>&#39;Blog&#39;, #&lt;------ NEW<br> ]</pre><p>Like this the framework will recognize the application you will be developing.</p><h4><strong>6- Migrating tables and creating a super user</strong></h4><p>Before we jump into the code we need to create a <strong>superuser </strong>for the project. The <strong>superuser </strong>has the ability to manage models in the admin part of the website. Alright, let’s create it. In your terminal add the following command:</p><pre>$ python manage.py createsuperuser</pre><p>But when you applied it, you might got an error:</p><pre>You have 18 unapplied migration(s). Your project may not work properly <br>until you apply the migrations for app(s): admin, auth, contenttypes, <br>sessions.<br><br>Run &#39;python manage.py migrate&#39; to apply them.</pre><p>It is because you didn’t migrate the database, then let’s do this. If you read the message, it says to run a command, which is:</p><pre>$ python mana.py migrate</pre><p>I left this issue above for you to understand how developing an application is. Sometimes it will be normal receiving some errors like this to tell you what is not right. So, always keep your eyes on the terminal to see what django and python will tell you if there is something wrong.</p><p>Ok, let’s come back to the creation of the <strong>superuser. </strong>So, after fixing the migration issue, add the super user creation command again in your terminal, add a name for it, I used the name <strong>admin </strong>but you can choose whatever you like. Add your email and password twice.</p><p><em>NOTE: If it is your first time creating a django project understand that the password will not appear in the terminal. Just add it and press enter.</em></p><pre>(venv) computer-name/blog_project % python manage.py createsuperuser<br>Username (leave blank to use &#39;computer-name&#39;): admin<br>Email address: <br>Password: <br>Password (again): <br>Superuser created successfully.</pre><p>Now that you already created the super user, go to the <strong>/admin </strong>site to login. Therefore, you just need to add the /admin in front of the project url and it will redirect you to the login page.</p><p><a href="http://127.0.0.1:8000/admin/">http://127.0.0.1:8000/admin/</a></p><p>After you be redirected you will see the login page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YSHS6UDZfr6ZAR2w" /><figcaption>Login route to admin.</figcaption></figure><p>Then, login using the username and password you created before and you will be redirected to the admin site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GPmT8MGJujQreFWd" /><figcaption>Django admin.</figcaption></figure><p>Well done, you now have access to the admin as a super user and can make future editions in your future models.</p><h4><strong>7- Sending the first message to the frontend</strong></h4><p>Great, now you will see the part of the functionality of what django is capable of. We will send a <strong>hello world </strong>message to the frontend. So, like this you will have the idea about how django works with the MVC architecture (model, view, controller).</p><p>In your <strong>blog </strong>app, goes to the <strong>views.py </strong>file and add the following code:</p><p><strong>blog/views.py</strong></p><pre>from django.shortcuts import render <br>from django.http import HttpResponse <br><br># Create your views here. <br>def index(request): #&lt;------ NEW<br>    return HttpResponse(&#39;hello world&#39;)</pre><p>The highlighted area is the new code you should add in the views.py file. Now that you have added this code into your <strong>blog/views.py, </strong>you should tell django where this response should appear. Therefore, go to <strong>blog_project/urls.py </strong>and add the path of the view you have created in <strong>blog/views.py.</strong></p><p><strong>blog_project/urls.py:</strong></p><pre>from django.contrib import admin<br>from django.urls import path <br>from blog.views import index #&lt;------ NEW<br><br>urlpatterns = [ <br>path(&#39;&#39;, index, name=&#39;index&#39;), #&lt;------ NEW<br>path(&#39;admin/&#39;, admin.site.urls)<br>]</pre><p>In this section you will need to import the <strong>index </strong>function you have created in the <strong>blog/views.py </strong>and add it into the path as the name index on the middle of the path. You can also see that you have an empty string in the beginning of the path. It means that the function and page will be running as the main url.</p><p>Now, after you configure the views.py and urls.py go to the main url: <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000/</a> and refresh the page. If you did everything right you should see a “hello world” text at the top right corner of your screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kvt2rJcMDZB1-Qhy" /><figcaption>initial view displayed on localhost.</figcaption></figure><p><em>NOTE: If you faced any error, check the code above and try to find any spelling error, if you forgot to import the index function into the urls.py. Also, check the last message at the bottom of the error message in your terminal. There, you will see what is wrong. If there’s no error let’s go to the next part.</em></p><h4><strong>8- Create your first model</strong></h4><p>Now it is time for you to create your first model for this application. In your blog app go to the <strong>models.py </strong>and add the following code.</p><p><strong>blog/models.py:</strong></p><pre>from django.db import models <br><br># Create your models here.<br><br>class Article(models.Model): <br>    title = models.CharField(max_length=100) <br>    body = models.TextField() <br>    date = models.DateTimeField(auto_now_add=True) <br>    thumb = models.ImageField(blank=True)<br>  <br>    def __str__(self): <br>        return self.title</pre><p>The code above is a command to create a table called <strong>Article </strong>in django sqlite, with a title, body, date and thumb (image) and the <strong>__str__ </strong>function to display the <strong>title </strong>of the blog in the admin page. Having a close look to this class you will see that each of these fields has different methods.</p><p>For example:</p><ul><li>For the <strong>title</strong> you will see a <strong>Charfield()</strong> method with a length of 100 characters. This method will allow you to add simple (string) characters, such as titles, names or sentences.</li><li>The <strong>body </strong>has a different method called <strong>TextField()</strong>. As the name says, it is used to store larger amounts of characters, such as texts.</li><li>The <strong>date </strong>variable is assigned to the <strong>DateTimeField() </strong>method. This method stores the date time when a new object is created.</li><li>For the <strong>thumb </strong>(image), the method used is <strong>ImageField(). </strong>This field is created to store an image for the object.</li></ul><p><em>NOTE: For the </em><strong><em>ImageField() </em></strong><em>method, you will need to install </em><strong><em>Pillow </em></strong><em>to handle the images you added when creating the object.</em></p><h4><strong>8.1- Installing Pillow to handle images</strong></h4><p>When you save the <strong>models.py </strong>file that you created the Article model, you will get an error:</p><p><strong>Terminal:</strong></p><pre>ERRORS: <br>blog.Article.thumb: (fields.E210) Cannot use ImageField because Pillow is not <br>installed.<br>HINT: Get Pillow at https://pypi.org/project/Pillow/ or run <br>command &quot;python -m pip install Pillow&quot;.</pre><p>Therefore, following the hint from the message above, you will need to install Pillow to be able to add images to your articles. Add the following code in your terminal:</p><pre>$ pip install Pillow</pre><p>After installing it you will need to configure the <strong>settings.py </strong>to point where the images will be placed when you add it from the article form.</p><h4><strong>8.2- Configure the image path</strong></h4><p>On your <strong>settings.py </strong>add the following code:</p><p><strong>blog_project/settings.py:</strong></p><pre>#top of the file<br>Import os<br><br>...<br><br>#bottom of the file <br><br>#shows where the media is stored <br>MEDIA_URL = &#39;/media/&#39; <br><br>#specify the folder <br>MEDIA_ROOT = os.path.join(BASE_DIR, &#39;media&#39;)</pre><p>After this configuration is done, you need to register the model <strong>Article </strong>into your admin page.</p><h4><strong>8.3- Registering the model in admin</strong></h4><p>Go to your blog folder and at the <strong>admin.py </strong>file add the following code to register the <strong>Article </strong>model:</p><p><strong>blog/admin.py</strong></p><pre>from django.contrib import admin<br>from .models import Article #&lt;------ NEW<br><br># Register your models here. <br><br>admin.site.register(Article) #&lt;------ NEW</pre><p>You will need to import the Article model from .models file and register it into the admin page.</p><h4><strong>8.4- Creating your first blog post</strong></h4><p>Now that you already created the Article model, registered on admin page, go to the admin page and you will see a new section added into the admin page called <strong>Articles</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_gdb6iNKw1KVHNQl" /><figcaption>Blog app added into admin.</figcaption></figure><p>Click on the Articles link and on the next page, in the top right corner click on the button <strong>ADD ARTICLE +</strong> to add a new blog post:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*d3LHRqyptMBbxKrY" /><figcaption>Blog section.</figcaption></figure><p>Add a title, a text body, an image from the thumb and save. You will see that the Article with the name <strong>Hello World </strong>was added to your database and a new folder called <strong>media </strong>was added to your <strong>blog_project/ </strong>directory as <strong>blog_project/media </strong>with the image you chose for your article.</p><p>Since you have added a new folder, your directory should be look like this:</p><pre>blog-project/ <br> |  <br> |   <br> |___blog_project/<br> |   | <br> |   |__blog_project/<br> |   |    |__ __init__.py<br> |   |    |__settings.py<br> |   |    |__urls.py<br> |   |    |__wsgy.py<br> |   |<br> |   |<br> |   |___blog/<br> |   |   |<br> |   |   |__migrations/<br> |   |   |___ __init__.py<br> |   |   |___admin.py<br> |   |   |___apps.py<br> |   |   |___models.py<br> |   |   |___views.py<br> |   |<br> |   |___media/ #&lt;------- new<br> |   |     |__imgexp.png<br> |   |<br> |   |__manage.py<br> |__venv/</pre><h4><strong>9- Add templates and static folders to create the frontend</strong></h4><p>At this point we are ready to start our frontend part. Create a new folder called <strong>templates </strong>into your root directory.</p><p>Go to the <strong>/blog-project/blog_project/</strong> and add the following command:</p><pre>$ mkdir templates</pre><p>Like this the templates folder will be created, now you need to tell django where the template folder is located.</p><p>Go to <strong>settings.py</strong> and add the name ‘templates’ in the code in the <strong>TEMPLATES </strong>array:</p><p><strong>blog_project/settings.py</strong></p><pre>TEMPLATES = [<br>    {<br>        &quot;DIRS&quot;: [&quot;templates&quot;],<br>        &quot;BACKEND&quot;: &quot;django.template.backends.django.DjangoTemplates&quot;,<br>        &quot;APP_DIRS&quot;: True,<br>        &quot;OPTIONS&quot;: {<br>            &quot;context_processors&quot;: [<br>                &quot;django.template.context_processors.debug&quot;,<br>                &quot;django.template.context_processors.request&quot;,<br>                &quot;django.contrib.auth.context_processors.auth&quot;,<br>                &quot;django.contrib.messages.context_processors.messages&quot;<br>            ],<br>        },<br>    },<br>]</pre><p>You will also need to create a <strong>static </strong>folder to add static files such as css, javascript and static images. You will need to do almost the same approach you did to create the templates folder. In addition the static folder will be placed at the same place as the <strong>templates </strong>one. Therefore, in your terminal add the following command:</p><pre>$ mkdir static</pre><p>Like this your static folder will be created into your project root directory. The same as the templates one. So, right now, as same as the templates folder you will need to tell django that you have a static folder:</p><p>Go to <strong>settings.py </strong>and at the bottom of the file add the following code:</p><p><strong>blog_project/settings.py:</strong></p><pre>STATICFILES_DIRS = (os.path.join(BASE_DIR, &quot;static&quot;),)</pre><p>Now, since you created the static folder and configured it following the code above you are ready to go to the next step, which is adding the frontend of this project.</p><p>NOTE: <em>Greate, you are ready to begin the frontend part of this tutorial. If everything is working accordingly, well done. Now, is the step in which you need to create the HTML and CSS files where the code will be added. So, from this part until the end I will ask you for all of your attention. Because, if you miss a small step or detail you can face some errors</em>.</p><h4><strong>10- Add HTML and CSS files in the templates and static folders</strong></h4><p>In this part you will display the frontend of this project. From the next part we will start fetching data from the model <strong>Article </strong>you created at the part 8 of this tutorial and display to the frontend.]</p><h4><strong>10.1- Adding the HTML with django built in template tags</strong></h4><p>The templates and static folders you have added right now will be used to have the <strong>base.html </strong>file. The file in which will be used to hold all the other pages that will be created.</p><ul><li>Within the <strong>templates </strong>folder<strong> </strong>create a new file called <strong>base.html.</strong></li><li>Add the following code within the <strong>base.html</strong>:</li></ul><p><strong>templates/base.html</strong>:</p><pre>{% load static %}<br>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br><br>&lt;head&gt;<br>  &lt;meta charset=&quot;utf-8&quot;&gt;<br>  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;<br>  &lt;meta name=&quot;description&quot; content=&quot;&quot;&gt;<br>  &lt;meta name=&quot;author&quot; content=&quot;&quot;&gt;<br>  &lt;title&gt;Blog Project&lt;/title&gt;<br>  &lt;!-- Bootstrap core CSS --&gt;<br>  &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl&quot; crossorigin=&quot;anonymous&quot;&gt;<br>  &lt;!-- Custom styles from static folder --&gt;<br>  &lt;link href=&quot;{% static &#39;css/style.css&#39; %}&quot; rel=&quot;stylesheet&quot;&gt;<br>&lt;/head&gt;<br><br>&lt;body&gt;<br>  &lt;!-- Navigation --&gt;<br>  &lt;nav class=&quot;navbar fixed-top navbar-expand-lg navbar-default fixed-top shadow shadow-inset&quot;&gt;<br>    &lt;div class=&quot;container&quot;&gt;<br>      &lt;a class=&quot;navbar-brand&quot; href=&quot;&quot;&gt;Blog Project&lt;/a&gt;<br>      &lt;button class=&quot;navbar-toggler navbar-toggler-right&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarResponsive&quot; aria-controls=&quot;navbarResponsive&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;<br>        &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;<br>      &lt;/button&gt;<br>    &lt;/div&gt;<br>  &lt;/nav&gt;<br>  &lt;header&gt;<br>    &lt;div id=&quot;carouselExampleIndicators&quot; class=&quot;carousel slide&quot; data-ride=&quot;carousel&quot;&gt;<br>      &lt;div class=&quot;carousel-inner&quot; role=&quot;listbox&quot;&gt;<br>        &lt;!-- Slide One - Set the background image for this slide in the line below --&gt;<br>        &lt;div class=&quot;carousel-item active&quot; style=&quot;background-image: url(https://medium.com/&#39;https://cdn.pixabay.com/photo/2017/09/16/16/09/sea-2755908_1280.jpg&#39;)&quot;&gt;<br>          &lt;div class=&quot;carousel-caption d-none d-md-block&quot;&gt;<br>            &lt;h3&gt;Title&lt;/h3&gt;<br>            &lt;p&gt;This is a description for your blog.&lt;/p&gt;<br>          &lt;/div&gt;<br>        &lt;/div&gt;<br>  &lt;/header&gt;<br>  &lt;!-- Page Content --&gt;<br>  &lt;div class=&quot;container&quot;&gt;<br>    {% block content %}<br><br>  {% endblock content %}<br>    &lt;/div&gt;<br>  &lt;!-- /.container --&gt;<br>  &lt;!-- Footer --&gt;<br>  &lt;footer class=&quot;py-5 shadow-xs shadow-inset&quot;&gt;<br>    &lt;div class=&quot;container&quot;&gt;<br>      &lt;p class=&quot;m-0 text-center &quot;&gt;Copyright &amp;copy; Your Website 2021&lt;/p&gt;<br>    &lt;/div&gt;<br>    &lt;!-- /.container --&gt;<br>  &lt;/footer&gt;<br>  &lt;!-- Bootstrap core JavaScript --&gt;<br>  &lt;!-- JavaScript Bundle with Popper --&gt;<br>&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><ul><li>Now that you have added the code into <strong>base.html </strong>save it and let’s prepare to display this page in the frontend.</li><li>To add the css code, create a new folder called <strong>css </strong>within the <strong>static </strong>folder and within the css folder create a css file called <strong>style.css. </strong>The path for this file should be like this:</li></ul><p><strong>static/css/style.css</strong></p><ul><li>Now that you have added the style.css, add the following style within this file:</li></ul><pre>body {<br>    padding-top: 56px;<br>  }<br>  <br>.carousel-item {<br>    height: 65vh;<br>    min-height: 300px;<br>    background: no-repeat center center scroll;<br>    -webkit-background-size: cover;<br>    -moz-background-size: cover;<br>    -o-background-size: cover;<br>    background-size: cover;<br>  }<br>  <br>.portfolio-item {<br>    margin-bottom: 30px;<br>  }</pre><ul><li>After the creation the templates and css folders as well as the style.css file, your working directory should look like this:</li></ul><pre>blog-project/ <br> |  <br> |   <br> |___blog_project/<br> |   | <br> |   |__blog_project/<br> |   |    |__ __init__.py<br> |   |    |__settings.py<br> |   |    |__urls.py<br> |   |    |__wsgy.py<br> |   |<br> |   |<br> |   |___blog/<br> |   |   |<br> |   |   |__migrations/<br> |   |   |___ __init__.py<br> |   |   |___admin.py<br> |   |   |___apps.py<br> |   |   |___models.py<br> |   |   |___views.py<br> |   |<br> |   |___media/<br> |   |     |__imgexp.png<br> |   |<br> |   |___static/ #&lt;------ NEW<br> |   |     |__css/<br> |   |         |__style.css<br> |   |<br> |   |___templates/  #&lt;------ NEW<br> |   |     |__base.html<br> |   |<br> |   |__manage.py<br> |<br> |__venv/</pre><p><em>Note: make sure you have added the files within the right folders.</em></p><h4><strong>10.2 Explaining template tags</strong></h4><p>Before we move on, you might have seen some unusual tags in the <strong>base.html </strong>file. They are used for different purposes. For instance, the <strong>{% load static %}</strong> template tag.</p><p>It simply is to load the files that are placed into a static folder. In the HTML code above, in the <strong>&lt;head&gt;&lt;/head&gt;</strong> you can spot a <strong>href=”{% static ‘css/style.css’ %}</strong> within a link. Without the <strong>{% load static %}</strong> template tag at the top, your files won’t be loaded.</p><p>Other template tags that are placed at the middle of the <strong>base.html </strong>file are the <strong>{% block content %} {% endblock content %}</strong>. These two template tags, as the name says are to extend other templates within this html block. We will cover more about them upfront on this tutorial.</p><p>If you are new to django, you might be asking yourself, what do these template tags do? The answer is: they are used to bring different functionalities to html templates. Such as loops, conditionals, load custom functionalities, include other piece of html such as navbars or footers and etc. <a href="https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#std:templatetag-static">You can see more about django template tags here</a>.</p><h4><strong>11. Showing the template on frontend.</strong></h4><p>Now that you have already added the base.html<strong> </strong>template in templates and created the css folder and style.css file, it’s time to show the result on the frontend.</p><p>In your <strong>views.py </strong>you need to change the <strong>index </strong>function for the following code:</p><p><strong>blog/views.py:</strong></p><pre>from django.shortcuts import render<br><br># Create your views here.<br>def index(request):<br>    return render(request, &#39;base.html&#39;)</pre><p>The <strong>render() </strong>shortcut will request the <strong>base.html </strong>template and return the answer to the browser based on the url. Therefore, if you remember on parth 7, we added the index function to the root path. In other words, you will just need to save the views.py and reload the page on <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000/</a> url to see the result of the template on the frontend.</p><p>So, if you did everything right, the outcome should looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KSjP4KyHkYA_lDML" /><figcaption>Blog post example.</figcaption></figure><h4><strong>12. Showing blog posts on frontend</strong></h4><p>Fantastic, well done to stick until here. This is one of the hardest parts of the tutorial. Therefore, for the next parts, I recommend you pay double attention to the small details so you do not face any error.</p><h4><strong>12–1. Create a new templates folder into blog app</strong></h4><p>Go to the blog app and create a new folder called templates, and add a template called <strong>posts.html</strong>. For while it will be empty.</p><h4><strong>12–2. Update the index function to recognize the posts template and add the articles there</strong></h4><p>In your index function you can see that it is rendering the <strong>base.html </strong>template. Change it to <strong>posts.html. </strong>After that you need to import the <strong>Article </strong>model into your views.py and assign it as <strong>articles. </strong>Also, you need to create a dictionary with the name articles for the posts template recognize the posts that are added into this model.</p><p><strong>blog/views.py:</strong></p><pre>from django.shortcuts import render<br># import Article model <br>from .models import Article #&lt;------ NEW<br><br># Create your views here.<br><br>def index(request): <br># add article <br>articles = Article.objects.all() #&lt;------ NEW<br>return render(request, &#39;posts.html&#39;, {&#39;articles&#39;:articles}) #&lt;------ NEW CODE</pre><h4><strong>12–3. Displaying the posts on the frontend</strong></h4><p>Now is the time you will find the first posts appearing in the frontend of your project. So, if you already added an article in the <strong>Articles </strong>form in the admin page go there and add some with images. Otherwise they will not show when this step is implemented.</p><p>Now add this code into the <strong>posts.html </strong>template you created in step <strong>12–1:</strong></p><p><strong>blog/templates/posts.html:</strong></p><pre>{% extends &#39;base.html&#39; %}<br>{% load static %}<br>{% block content %}<br><br>&lt;br&gt;&lt;br&gt;<br>&lt;h2&gt;Portfolio Heading&lt;/h2&gt;<br><br>&lt;div class=&quot;row&quot;&gt;<br>  {% for article in articles %}<br>   &lt;div class=&quot;col-lg-4 col-sm-6 portfolio-item&quot;&gt;<br>     &lt;div class=&quot;card h-100&quot;&gt;<br>       &lt;a href=&quot;#&quot;&gt;&lt;img class=&quot;card-img-top&quot; src=&quot;{{article.thumb.url}}&quot; alt=&quot;&quot;&gt;&lt;/a&gt;<br>       &lt;div class=&quot;card-body&quot;&gt;<br>         &lt;h4 class=&quot;card-title&quot;&gt;<br>           &lt;a href=&quot;#&quot;&gt;{{article.title}}&lt;/a&gt;<br>         &lt;/h4&gt;<br>         &lt;p class=&quot;card-text&quot;&gt;{{article.body|slice:50}}&lt;/p&gt;<br>       &lt;/div&gt;<br>     &lt;/div&gt;<br>   &lt;/div&gt;<br>  &lt;br&gt;&lt;br&gt;<br>  {% endfor %}<br>&lt;/div&gt;<br>{% endblock content %}</pre><p>Did you realize that there is no function running the <strong>base.html </strong>template but it is running anyway? Actually it is being rendered because the <strong>posts.html </strong>template is extending it. Therefore, it can be used to load all of the other templates if you want.</p><p>Did you also realize that in this time the HTML code is within the <strong>{% block content %} {% endblock content %}</strong> template tags, not out as same as in the <strong>base.html? </strong>Now you may understand how these templates work. One is used to extend while the base template will be used to display the other templates.</p><h4><strong>12–4. Explaining the for loop in templates</strong></h4><p>The for loop will display all the Article posts added to it in the template using the django loop tag. Did you remember the variable articles and the dictionary you added on the index function? The articles variable will be added into the dictionary in views.py and the dictionary will be loaded into <strong>posts.html</strong> template as <strong>articles. </strong>Therefore, if you already have a good HTML knowledge you can understand that all the Article models fields were displayed in the right places or elements.</p><p>The<strong> article.body </strong>into &lt;p&gt; element, <strong>article.title </strong>into <strong>&lt;a&gt;</strong> element (without <strong>href</strong> for while) and the <strong>article.thum.url </strong>within the <strong>&lt;img src=” ”&gt;</strong>. However, you might realize that the image did not appear although it is implemented.</p><p><em>NOTE: The </em><strong><em>|slice:50 </em></strong><em>within the </em><strong><em>article.body </em></strong><em>is to not display all the blog content within a card. Only to show part of the body in it. Also be aware that the </em><strong><em>href </em></strong><em>will be added late to link to the post detail functionality and url.</em></p><h4><strong>12–5. Fixing the image not showing on posts.html template</strong></h4><p>To fix this issue you will need to add the following code into your <strong>urls.py.</strong></p><p><strong>blog_projects/url.py:</strong></p><pre>from django.contrib import admin <br>from django.urls import path <br>from blog.views import index <br><br># import for display image<br>from django.conf import settings #&lt;------ NEW<br>from django.conf.urls.static import static #&lt;------ NEW<br><br>urlpatterns = [ <br>    path(&#39;admin/&#39;, admin.site.urls),<br>    path(&#39;&#39;, index, name=&#39;index&#39;),<br>]<br><br>if settings.DEBUG: #&lt;------ NEW<br>    urlpatterns += static(settings.STATIC_URL, <br>    document_root=settings.STATIC_URL) <br>    urlpatterns += static(settings.MEDIA_URL, <br>    document_root=settings.MEDIA_ROOT)</pre><p>You will need to import <strong>settings </strong>and <strong>static </strong>from conf.url and iterate the urlpatterns with the <strong>STATIC_URL</strong>, <strong>MEDIA_URL </strong>and <strong>MEDIA_ROOT</strong> from settings. If you have any doubt you can check the <a href="https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development">django documentation here</a> to know more about these settings.</p><p>After this, refresh the page of your browser where the application is displayed and check if the posts appear. If so you will see the screen like this:</p><figure><img alt="displayed posts | django" src="https://cdn-images-1.medium.com/max/1024/0*dMFJ0dbFQSoz2fm9" /><figcaption>Displayed posts.</figcaption></figure><h4><strong>13 Creating the detail page</strong></h4><p>This is the last part of our tutorial. In this section you will learn how to display the detail page of each blog post individually clicking in one of the cards representing each post.</p><h4><strong>13–1. create the post_detail function</strong></h4><p>In blog/views.py you will need to create a function called <strong>post_detail</strong> and create a variable called <strong>details </strong>assigned to a method called <strong>get_object_or_404(ModelName, id=id).</strong></p><p>The functionality of this method works in a way that for example when you click on a card it will go to the ModelName it is assigned and search for the item <strong>id </strong>and return the object. <em>If you want to understand more how the method </em><strong><em>get_object_or_404 </em></strong><em>works you can have a look in the </em><a href="https://docs.djangoproject.com/en/3.1/topics/http/shortcuts/#id2"><em>documentation here</em></a><em>.</em></p><p>So, the <strong>post_detail </strong>function will look like this:</p><p><strong>blog/views.py</strong></p><pre>from django.shortcuts import render, get_object_or_404 #&lt;------ NEW IMPORT<br># import Article model<br>from .models import Article<br><br># Create your views here.<br>def index(request):<br>    # add article <br>    articles = Article.objects.all()<br>    return render(request, &#39;posts.html&#39;, {&#39;articles&#39;:articles})<br><br>def post_detail(request, id): #&lt;------ NEW<br>    # display detail page.<br>    details = get_object_or_404(Article, id=id)<br>    return render(request, &#39;details.html&#39;, {&#39;details&#39;:details})</pre><p>NOTE:<em> For the </em><strong><em>post_detail </em></strong><em>function, you will also need to add </em><strong><em>id </em></strong><em>as parameter for the function to return the result that is to create a detail page for each blog post. Also, you will need to add a new html template called </em><strong><em>details.html </em></strong><em>that will be created late and display the </em><strong><em>details </em></strong><em>variable in a dictionary to be recognized in the template. If it got a bit complicated for you, have a good look at this function and follow each step for you to understand. :)</em></p><h4><strong>13–2. add the post-details url and import the post_detail function</strong></h4><p>In this part you will need to create a new url path. Then go to <strong>blog_project/urls.py </strong>and add the following code highlighted:</p><p><strong>blog_project/urls.py:</strong></p><pre>from django.contrib import admin<br>from django.urls import path<br>from blog.views import index, post_detail #&lt;------ NEW IMPORT<br><br>from django.conf import settings<br>from django.conf.urls.static import static<br><br><br>urlpatterns = [<br>    path(&#39;admin/&#39;, admin.site.urls),<br>    path(&#39;&#39;, index, name=&#39;index&#39;),<br>    path(&#39;post-detail/&lt;int:id&gt;/&#39;, post_detail, name=&#39;postDetail&#39;) #&lt;------ NEW<br>]<br><br>if settings.DEBUG:<br>    urlpatterns += static(settings.STATIC_URL, <br>    document_root=settings.STATIC_URL) <br>    urlpatterns += static(settings.MEDIA_URL, <br>    document_root=settings.MEDIA_ROOT)</pre><p>In this part you will need to import the <strong>post_detail</strong> function you created in <strong>blog/views.py </strong>because it will need as functionality to be used in the new url you will create. Now, create this new url path called <strong>post-detail </strong>and add the <strong>&lt;int:id&gt; </strong>into the new url pattern since <strong>int </strong>is the format of the id (integer).</p><h4><strong>13–3. Adding the href to trigger the post_detail function and explanation</strong></h4><p>Before going to the explanation of how the url and post_detail works together, go to the <strong>posts.html </strong>and add the following code into each <strong>href:</strong></p><pre>href=&quot;{% url &#39;postDetail&#39; id=article.id %}&quot;</pre><p>This code will allow django to trigger the <strong>post_detail </strong>function when you click one of the cards from posts.html template. Sending you to the details.html page.</p><p>Here you can see what happens when you click a postcard:</p><ol><li>The post detail as seen above goes to the url and check what is the url pattern as well as which function handles it;</li><li>Then, after seeing that post_detail is the function that handles the url, it gets the object id into the database and returns it on the template.</li><li>You receive the answer on the browser with the exact post you clicked.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*emBDgtOnD9XTuUEW" /></figure><h4><strong>13–4. create the details.html template</strong></h4><p>Go to the empty <strong>details.html </strong>template and add the following code. If you did not created it yet, go to <strong>blog/templates </strong>and add the following code inside it.</p><p><strong>blog/templates/</strong></p><pre>{% extends &#39;base.html&#39; %}<br>{% load static %}<br>{% block content %}<br>&lt;!-- Page Heading/Breadcrumbs --&gt;<br>&lt;div class=&quot;row&quot;&gt;<br>    &lt;div class=&quot;col-lg-8 mx-auto&quot;&gt;<br>       &lt;h1 class=&quot;mt-4 mb-3&quot;&gt;{{details.title}}&lt;/h1&gt;<br>         &lt;ol class=&quot;breadcrumb&quot;&gt;<br>           &lt;li class=&quot;breadcrumb-item active&quot;&gt;Post Detail&lt;/li&gt;<br>         &lt;/ol&gt;<br>    &lt;/div&gt;<br>&lt;/div&gt;<br> &lt;div class=&quot;row&quot;&gt;<br>   &lt;!-- Post Content Column --&gt;<br>   &lt;div class=&quot;col-lg-8 mx-auto&quot;&gt;<br>     &lt;!-- Preview Image --&gt;<br>     &lt;img class=&quot;img-fluid rounded&quot; src=&quot;{{details.thumb.url}}&quot; alt=&quot;&quot;&gt;<br>     &lt;hr&gt;<br>     &lt;p&gt;Posted on {{details.date}}&lt;/p&gt;<br>     &lt;hr&gt;<br>     &lt;p class=&quot;lead&quot;&gt;{{details.body}}&lt;/p&gt;<br>     &lt;/div&gt;<br>  &lt;/div&gt;<br>  {% endblock content %}</pre><p>The idea of the code above is similar as you did on the <strong>posts.html </strong>template. However, in this template you don’t have the for loop. Instead, from the <strong>post_detail </strong>function you will get <strong>title, thumb, body </strong>and <strong>date.</strong></p><p>Therefore, if you have added the code correctly, when you click one of the cards from posts.html template on browser you will get this page:</p><figure><img alt="detail page | django" src="https://cdn-images-1.medium.com/max/1024/0*eUKj7K13qsy0yFpb" /><figcaption>detail page</figcaption></figure><p>Well done, you finished this tutorial using django. You can use this code to implement more features such as user authentication, add different categories or even create a cms. Use your imagination and what you have learned from this tutorial to do other application with django. Thank to stick until here and if you have any comment live a comment below and I will answer your question.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=507dfaff254e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why consistency is important to learn to code]]></title>
            <link>https://medium.com/@pradoelias133/why-consistency-is-important-to-learn-to-code-1fd3b38166c5?source=rss-a6be71af789e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1fd3b38166c5</guid>
            <category><![CDATA[consistency]]></category>
            <category><![CDATA[self-improvement]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Elias Prado]]></dc:creator>
            <pubDate>Wed, 12 Oct 2022 20:40:05 GMT</pubDate>
            <atom:updated>2022-10-12T21:15:50.042Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KU8DqauRwTPVJOXw" /></figure><p>Many people want to learn to code but few can continue. In this post, I will share what I’ve done to learn to programming and you will understand what you have to do to start and keep learning to program even if you came from a non-technical field like me.</p><h4><strong>1- keep coding until you learn.</strong></h4><p>Back in 2018, I was asking myself what would be the best position in which would give me freedom as same as a fair salary initially. After a good search and the invitation of some friends, I realized that working as a programmer would be a good idea. However, once I looked closely I realized how hard it was.</p><p>As same as everyone who begins in this industry, I got hooked and began my learning through free platforms such as FreeCodeCamp and CodeAcademy. But I was lost and did not see any advancement in my learning. Therefore, I was asking myself <strong>how could I keep learning and not give up? </strong>Searching around in different forums I found a history of a guy who went to South America and spent 100 days challenging himself to code and posting his progress on Twitter.</p><p>I thought, “If I start this challenge and could not finish?”. So, I will never know if I don’t try. As a result, I joined a coding Bootcamp to push me to learn and I started coding right away. I have to say that I had no idea what would be the next step and how would be the learning. The good thing is that studying through a coding Bootcamp I had a community of other people learning</p><p>with me in different levels not mentioning the alumnus who had more experience making all of us engaged in the course.</p><p>The <strong>#100DaysOfCode </strong>also was crucial in the initial moment of my learning. It pushed me to keep studying and publishing since I made it public on Twitter. The beginning of learning how to program is hard and this is the moment of many people give up thinking this is not for them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*sgY6ejnhf2mNnzevH5sI-w.png" /></figure><p><strong>So, if you are new to programming and you also want to keep on track and not give up, here is what I have done to keep learning and engaged:</strong></p><ol><li>Decide if you want to learn how to code. It is not physical force, rather it is pure logic. That’s when many people get frustrated. Take a decision and give it time to learn and understand that you can get stuck in a bug for two or three weeks.</li><li>Join a coding community where you can learn and engage with other students. This is a very important step for your learning. Without a community, for you to share the same doubts of a coding challenge, such as adding a payment API to a shopping cart, you can easily give up.</li><li>Make it public going into the #100DaysOfCode. It will challenge yourself to continue making your attention primarily into the task of coding.</li><li>Make coding a priority in your life.</li></ol><p>With these steps, you are locked into programming and hardly will give up.</p><h4><strong>2- Put what you have learned in practice.</strong></h4><p>Alright, you have learned the fundamentals of programming such as operators, variables, loops, conditionals, functions, etc.., but this is not enough. Only learning the fundamentals, despite being very important, is not enough, you have to practice to make concrete the knowledge into your head.</p><p>In my case, since I joined a coding-bootcamp I was forced to practice what I have learned and show the outcome. This is the time you will see that what you have learned through tutorials are not sufficient to give you initial confidence to make “that project” come to life.</p><p>You will see that many processes will be confusing and will make you stuck. <strong>Stuck, </strong>this is a magic word that means you are learning.</p><p>According to <a href="https://www.harvardbusiness.org/the-importance-of-practice-and-our-reluctance-to-do-it/#:~:text=Making%20a%20commitment%20to%20practice,continue%20to%20grow%20and%20improve.">J. Long</a>, <em>Committing to practice is essential to maximize the impact of training. After all, practice is the only way to become proficient in a new skill or behaviour. We need to embrace the discomfort of being beginners to continue to grow and improve.</em></p><p>You`ll realize that after you lose the fear to initiate a new project on your own you will face a lot of discomfort and bugs. However, this is the secret to learning programming. To, create that app, to learn a new framework, to implement a third-party API. They will be hard initially but with practice, and not giving up when you get stuck will lead you to another level. And, I have to say that’s what makes you sooner comfortable with the technology you are learning.</p><h4><strong>3- Measuring your progress.</strong></h4><p>That is another thing that the majority of us can`t keep well which is consistency. This is no problem, it is super normal. However, there are things that we want so bad to change or improve that without consistency to do them will be nearly impossible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/0*T4LGbNo4NpJcUJPF" /></figure><p>As same as the majority of people I was dragged to social media and many other things that sucked all of my attention. Therefore, to keep my attention I knew that I needed to do something otherwise I would never learn what is needed to become a software developer.</p><p>For instance, the 100 days challenge is great but there is something more needed, measuring your progress. You may code every day using this challenge but tracking and measuring your progress can give your 100 days challenge an extra power keeping you in the game.</p><p>In my case, I have the luck to come across a book called <strong>the 12 weeks year by Brian Moran </strong>which teaches you how to make one year in only 12 weeks (about 90 days). And one of the things that Bryan details: You have to measure your progress, score it and improve along the way. Like this, it’s more than clear that you will increase your knowledge into programming and finally become a software developer.</p><h4>Conclusion</h4><p>Therefore, this was the strategy I have used to learn what I know right now. If you are reading this post it means that you already took the decision to learn to program and need to be more consistent.</p><p>So, if this is your case follow these steps and see if they work for you. I am almost sure they will. So, challenge yourself, make it public, practice the knowledge you have learned out of tutorials and make sure you are measuring your progress and improve where you are not doing right skipping what is not working for you. Like this, you will always improve and sooner will be confident to apply for that job. I see you at the next level.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1fd3b38166c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a private Django package.]]></title>
            <link>https://medium.com/@pradoelias133/how-to-create-a-private-django-package-d2e62e6e6e56?source=rss-a6be71af789e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2e62e6e6e56</guid>
            <category><![CDATA[create-package-python]]></category>
            <category><![CDATA[python-packages]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Elias Prado]]></dc:creator>
            <pubDate>Mon, 06 Dec 2021 02:36:13 GMT</pubDate>
            <atom:updated>2021-12-06T19:27:28.780Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RKMEaUWQhEzMq-aoF2C3yw.png" /></figure><p>In this material, I will detail how you can create your own private Django package to be reused among different projects.</p><p>Therefore, to follow along we will be using an app called cartthat has the function of handling custom orders sending them to checkout. This app will be transformed into a package to be used by other Django projects, where every steps will be explained for you.</p><h4><strong>Initial directory:</strong></h4><pre>your_django_package<br>    └───cart<br>        │   admin.py<br>        │   apps.py<br>        │   contexts.py<br>        │   models.py<br>        │   urls.py<br>        │   views.py<br>        │   __init__.py<br>        │   <br>        ├───migrations<br>        │       __init__.py<br>        │       <br>        ├───templates<br>        │       cart.html<br>        │       header2.html<br>        │<br>        └───tests<br>                test_urls.py<br>                test_views.py<br>                __init__.py</pre><p>As you can see the initial directory has only the cart app within it. Therefore, to initiate your package you will need to create more files that will handle the package build process, as well as some requirements that you will need in case you, some day, want to make it public, maybe adding it toPYPI.org .</p><h4><strong>Important files to build the package:</strong></h4><p>In this section will be detailed the files needed to build the cart package. Therefore, to make it easy to follow along, the instruction will be followed as the <strong>file name </strong>+ i<strong>nstruction</strong> + <strong>code</strong>.</p><ol><li>Setup.py file.</li></ol><p>This is the most important file. <strong>setup.py</strong> represents __main__ when you call the install functions. Basically, inside setup.py you should add all the information that are needed to create your package. Such as the package name, version, license and etc:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfa6bc7d7cefb04e5fccf8007173e4df/href">https://medium.com/media/cfa6bc7d7cefb04e5fccf8007173e4df/href</a></iframe><p>There are other ways to configure the <strong>setup.py</strong> file with different approaches. However, I would suggest that this is the best and easy way to create your <strong>setup.py</strong> file.</p><p>2. README.rst file.</p><p>According to <a href="https://fileinfo.com/extension/rst">fileinfo.com</a> the .rst extension or <strong><em>reStructuredText markup language</em></strong> is a text file that contains and support code written and styled. In the <a href="https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#sections">Sphinx</a> website it is detailed that .rst extension is the main mark-up language for the creation and design of code documentation. Nevertheless, this is not a file that is really needed to create you private package, but it is a must if one day you want to make it public on pypi.org.</p><p><em>click </em><strong><em>view raw </em></strong><em>to see the mark-up format</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23b8689d0fd7ef2906ec5a40f4e005fa/href">https://medium.com/media/23b8689d0fd7ef2906ec5a40f4e005fa/href</a></iframe><p>3. LICENSE file.</p><blockquote>According to <a href="https://pro.arcgis.com/en/pro-app/latest/get-started/provisioning-files.htm">esri</a>, A license file is a plain text file that automatically supplies required information — such as product name, authorization number, and user contact information — to the Software Authorization Wizard.</blockquote><p>Depending the level of information you have in your app, I would advise you to have a well tailored LICENCE file to avoid any issue in future. However, the LICENSE file is also a must when uploading your package to pypi.org .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41ae3f27b1b9ed48a641208b3d7130e8/href">https://medium.com/media/41ae3f27b1b9ed48a641208b3d7130e8/href</a></iframe><p>In this example I will use a custom template that can be edited by yourself:</p><p>PS: <strong><em>I am not an attorney, therefore, the license example cannot be used as an advise or real world example.</em></strong></p><p>4. MANIFEST.in file.</p><p>MANIFEST.in is a text file that contains a serie of “commands” in a Distutils-defined format. Manifest commands allow you to include or exclude specific files and directories. Pincipally those that are not recognized by python as a directory because do not contains the __init__.py such astemplates/ directory in Django. And Files that do not have the .py extension and the files that is used to build the package such as LICENSE and README.rst.</p><p>Nevertheless, the MANIFEST.in is basically a set of commands to include in the package that will be recognized by sdist when building your package. Therefore, when the package be created if a directory or file is specified they will be there when you package be built 😉.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef266ccceb164ded2fe7d0559a620d4e/href">https://medium.com/media/ef266ccceb164ded2fe7d0559a620d4e/href</a></iframe><h4>Building the package</h4><p>At the time you are following this tutorial, in which we initiated with only the cart app , it has changed. Therefore, if you did everything fine your working directory should look like this:</p><pre>your_django_package<br>│   LICENSE<br>│   MANIFEST.in<br>│   README.rst<br>│   setup.py<br>│<br>└───cart<br>    │   admin.py<br>    │   apps.py<br>    │   contexts.py<br>    │   models.py<br>    │   urls.py<br>    │   views.py<br>    │   __init__.py<br>    │<br>    ├───migrations<br>    │       __init__.py<br>    │<br>    ├───templates<br>    │       cart.html<br>    │       header2.html<br>    │<br>    └───tests<br>            test_urls.py<br>            test_views.py<br>            __init__.py</pre><p>Now there are some few steps that need to be done to initiate the building process of your package:</p><ol><li>Install the sdist(source distribution) and whell packages:</li></ol><pre>pip install sdist &amp;&amp; pip install wheel</pre><p><em>note: I would consider creating a virtual environment and installing the sdist and whell packages there, (do not forget to add a .gitignore file).</em></p><p>2. build the package:</p><p>Into the package root directory add the following commands to finally build the package:</p><pre>python setup.py sdist &amp;&amp; python setup.py bdist_wheel</pre><p>Like this the package will finally build leaving you with the following directory:</p><pre>your_django_package<br>│   .gitignore<br>│   LICENSE<br>│   MANIFEST.in<br>│   README.rst<br>│   setup.py<br>│<br>├───build<br>│   ├───bdist.win-amd64<br>│   └───lib<br>│       └───cart<br>│           │   admin.py<br>│           │   apps.py<br>│           │   contexts.py<br>│           │   models.py<br>│           │   urls.py<br>│           │   views.py<br>│           │   __init__.py<br>│           │<br>│           ├───migrations<br>│           │       __init__.py<br>│           │<br>│           ├───templates<br>│           │       cart.html<br>│           │       header2.html<br>│           │<br>│           └───tests<br>│                   test_urls.py<br>│                   test_views.py<br>│                   __init__.py<br>│<br>├───cart<br>│   │   admin.py<br>│   │   apps.py<br>│   │   contexts.py<br>│   │   models.py<br>│   │   urls.py<br>│   │   views.py<br>│   │   __init__.py<br>│   │<br>│   ├───migrations<br>│   │       __init__.py<br>│   │<br>│   ├───templates<br>│   │       cart.html<br>│   │       header2.html<br>│   │<br>│   └───tests<br>│           test_urls.py<br>│           test_views.py<br>│           __init__.py<br>│<br>├───cart.egg-info<br>│       dependency_links.txt<br>│       PKG-INFO<br>│       SOURCES.txt<br>│       top_level.txt<br>│<br>└───dist<br>        cart-0.0.1-py3-none-any.whl<br>        cart-0.0.1.tar.gz</pre><h4>Installing the package</h4><p>The next step to use your package is sending it to a github repository to be used by yourself or your company.</p><p>Then, you can install the package by the following command:</p><p>For private repository:</p><pre>pip install git+ssh://git@github.com:your_account/your_django_package.git</pre><p>For public repository:</p><pre>pip install git+https://github.com/your_account/your_cart_package.git</pre><p>Do a <strong>pip list </strong>to check if the cart package is installed in your virtual environment.</p><pre>$ pip list</pre><pre>Package    Version<br>---------- -------<br>pip        21.2.4<br>cart        0.0.1</pre><p>Therefore, following this tutorial you learned how to build your own Django package that can be reused among different projects. Last but not the least, although this tutorial was exemplified a Django app that was transformed into a package, this approach you learned has the same process to create any Python package.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2e62e6e6e56" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>