<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zeelyha</title>
    <description>The latest articles on DEV Community by Zeelyha (@zeelyha).</description>
    <link>https://dev.to/zeelyha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F923631%2F9629eb9e-0fb5-420a-956f-b89ec3ae7a90.png</url>
      <title>DEV Community: Zeelyha</title>
      <link>https://dev.to/zeelyha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zeelyha"/>
    <language>en</language>
    <item>
      <title>Creating your First Django App</title>
      <dc:creator>Zeelyha</dc:creator>
      <pubDate>Sat, 10 Sep 2022 14:02:49 +0000</pubDate>
      <link>https://dev.to/zeelyha/creating-your-first-app-with-django-321b</link>
      <guid>https://dev.to/zeelyha/creating-your-first-app-with-django-321b</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Django is a free and open-source high-level Python framework that makes it faster and easier to build and maintain websites. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. &lt;/p&gt;

&lt;p&gt;To learn more about Django, it's documentation and community, visit the website &lt;a href="https://www.djangoproject.com/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;p&gt;Since Django is a Python framework, you need to have Python installed on your device before you can install Django. To check if you have Python installed open your command prompt and type in &lt;strong&gt;python&lt;/strong&gt;. You should get an output like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\Zee&amp;gt;python
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't, download the latest version of Python &lt;a href="https://www.python.org/downloads/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once that is done, we will now create a virtual environment for our Django project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Virtual Environment
&lt;/h3&gt;

&lt;p&gt;Virtual environments help to separate dependencies for different projects by creating an isolated environment for each project. There are various types like pipenv, venv, virtualenv and Pew (Python Environment Wrapper). I’ll be using virtualenv for this project. To install virtualenv, cd into a directory where you would like to store your code and run the following code in your shell&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
pip install virtualenv&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create virtual environment for the project named "env":&lt;br&gt;
&lt;code&gt;&lt;br&gt;
virtualenv env&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You should see an env folder in that directory now&lt;/p&gt;

&lt;p&gt;Activate the virtual environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;env/scripts/activate                  for windows
env/bin/activate                        for mac
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Install Django
&lt;/h3&gt;

&lt;p&gt;Install the latest version of Django by running&lt;br&gt;
&lt;code&gt;&lt;br&gt;
pip install django&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
or install django's main development branch&lt;br&gt;
&lt;code&gt;&lt;br&gt;
pip install -e django/&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
You can also install a specific version of django by running&lt;br&gt;
&lt;code&gt;&lt;br&gt;
pip install django==3.2&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating a Project
&lt;/h3&gt;

&lt;p&gt;Run the following command in the terminal to create a Django project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;django-admin startproject yourprojectname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Avoid naming your projects after Python or Django's built in functions to avoid conflicts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Running the above command would create a project directory with the following starter files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These files are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The outer myproject/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py &lt;a href="https://docs.djangoproject.com/en/4.1/ref/django-admin/"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The inner myproject/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. myproject.urls).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;myproject/&lt;strong&gt;init&lt;/strong&gt;.py: An empty file that tells Python that this directory should be considered a Python package. You can read more about packages in the &lt;a href="https://docs.djangoproject.com/en/4.1/ref/django-admin/"&gt;official Python Docs&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;myproject/settings.py: Settings/configuration for this Django project. &lt;a href="https://docs.djangoproject.com/en/4.1/topics/settings/"&gt;Django settings&lt;/a&gt; will tell you all about how settings work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;myproject/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs &lt;a href="https://docs.djangoproject.com/en/4.1/topics/http/urls/"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;myproject/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See &lt;a href="https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/"&gt;How to deploy with ASGI&lt;/a&gt; for more details.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;myproject/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See &lt;a href="https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/"&gt;How to deploy with WSGI&lt;/a&gt; for more details.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To verify that the project works, cd into the project directory run the following command:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
python manage.py runserver&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
You should get this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 10, 2022 - 13:50:50
Django version 4.1.1, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the link in your browser and voila, you have successfully created your first Django project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jZtajRhY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vd5gbsrdqdz8ld03ifkj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jZtajRhY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vd5gbsrdqdz8ld03ifkj.png" alt="Django Project Server Running" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above output, the server is running on port 8000, to change the port you can run&lt;br&gt;
&lt;code&gt;python manage.py runserver 8000&lt;br&gt;
&lt;/code&gt; instead, or &lt;br&gt;
&lt;code&gt;python manage.py runserver 0:8000&lt;br&gt;
&lt;/code&gt; to change the server's IP.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating an app
&lt;/h3&gt;

&lt;p&gt;To create an app, make sure you're in the same directory as the manage.py file and the run&lt;br&gt;
&lt;code&gt;&lt;br&gt;
django-admin startapp yourappname&lt;br&gt;
&lt;/code&gt; This should create a new folder with your app name that has the following structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;appname/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations!!! You have successfully created your first Django app.&lt;/p&gt;

&lt;p&gt;I hope this article was detailed and explanatory enough.&lt;br&gt;
If you have any questions or feedback, feel free to reach out to me on &lt;a href="https://www.twitter.com/zeelyha"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
