Django Create App

Django is a Python-based web framework that is based on the Model View Template architecture. In this post, we will go through the installation and setup for our first Django app. In Django, there is a difference between a project and an app. The project refers to the whole application. The app, in contrast, represents the subparts of the project. To create the Django project, first, we need to install Python.

To install Python, go to the link https://www.python.org/downloads/click on the download option. After downloading the Python, you can verify the installation by running this command in your console.

Django Create App

Virtual environment

It is considered to be good practice to use a virtual environment for separate projects. Virtual environments keep the package separately, and with the help, we can keep different versions of a library for different projects.

Virtual Environments keep the different versions of a module separated from each other to not mess up with each other.

We can install the virtual environment by using this command:

pip install virtualenv
Django Create App

Install Django

After installing the Python, the next step is to install Django. Since we have already installed a virtual environment, subsequently, we will install Django in the virtual environment. We will install Django and create the project in a virtual environment called myenv. To create the environment, execute this command:

Virtualenvmyenv

To use the virtual environment we just created, we have first to activate the environments. To activate, just run this command:

newenv\Scripts\activate

If the environment is activated, we can see the environment name on the left side of the console.

Django Create App

Now the environment is running and ready to use. The next step is to install Django. We can easily install Django by using the python package pip. To install Django run this command in your command-line interface python –  m pip install Django.

Django Create App

To verify if Django is correctly installed in your local machine or not, execute the following command in the Python console:

Import Django
Django Create App

If this command does not show any error and Django is perfectly installed on your computer, we can check the version of the Django installed by running the below code in the python console.

Print(Django.get_version())

The above command will print the version number of Django.

Project Setup

To start the project, go to the directory in which you want to create your project in your terminal or cmd and type this command:

django-adminstartprojectmyproject

This command will create a new directory named myproject. The structure for this myproject will be like this:

myproject/
   manage.py
myproject/
      __init__.py
      settings.py
      urls.py
      wsgi.py

Now navigate to the project directory by cd myproject.

python manage.py runserver

The output of the command will be something like this in the terminal:

Performing system checks...
0 errors found
January 08, 2020 - 15:50:53
Django version 1.8, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C

This output confirms that we have done our first project successfully and have started the development server.

App Setup

As mentioned above in the post, a project is made up of many applications, and each application has a specific task. An application can be reused and function differently by just making some tweaks to it.

Although, Django already provides some pre-installed apps in the project. To check these apps, navigate to the settings.py file in myproject ->myproject. The file contains a section called INSTALLED_APPS, which consists of all the pre-installed apps.

Django Create App

To create our new app, go to the manage.py in your command line and run this command:

python manage.py startappmyappp

Just like creating the project, this command will create a directory named "myapp”, whose structure will be like this:

myapp/
   __init__.py
   admin.py
   models.py
   tests.py
   views.py

To use this app and activate it, we need to tell Django that we have created a new app by specifying the app name in the INSTALLED_APPS list. After that, the INSTALLED_APPS section should look like this.

INSTALLED_APPS=(
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
)

Now the next step after creating an app is to specify the app's URL to render it in the browser. To add the URL, go to the file myproject->myproject-> urls.pyand modify it as below:

fromDjango.contrib importadmin
fromDjango.urls importpath, include
 
urlpatterns =[
    path('admin/', admin.site.urls),
    path('', include("myapp.urls")),
]

By doing these steps, we have set up our app. Now, if we want to render the “Hello World” page, we have to do these steps.

All the rendering in Django is done by the views part of the MVT architecture. Create a new file named views.py in your myapp directory, and in this views.py write the code below:

import Django.http from HttpResponse


def helloworld(request):
      return HttpResponse("Hello World!")

Now create a file called urls.py in the same myapp folder and use this code in it:

from . import views
from Django.urls import path


urlpatterns = [
    path('', views.helloworld, name=”home”
]

That's all. We have created the first Django app, and now, to run this, just start the server by executing the command mentioned above also:

python manage.py runserver

Now go to your browser and browse to localhost:8000 to see the output:

Django Create App

Related Topics

Django Pagination

Django is an open-source free to use python based web framework. Django provides many inbuilt tools for web developers to provide rapid web development. One of these tools is pagination....

4 minutes read.

Django Celery

Django is a python-based web framework. It is an open-source and free-to-use framework. Django uses the Model View Template architecture. Django provides many functionalities to web developers. One of these...

3 minutes read.

Django Filters

Django is a high-level python based web framework. Django is mostly used as a backend engine for websites. Nowadays, every website communicates to a database to fetch all the information...

3 minutes read.

Django Image Upload

Django is a python based web framework that uses Model View Template as its architecture. Django provides really quick web development with its functionalities. While we are building a website,...

3 minutes read.

Django vs Laravel

Django Overview Django is a Python Programming Open Source framework for web development. It was released in 2008 and was developed by two web developers named Adrian Holovaty and Simon Willison....

4 minutes read.

Django Installation

Django Installation Django development environment consists of installing and setting up Python, Django, and a Database System since Django deals with a web application. Below the statement will guide you through installing Python 3.8.0 and...

2 minutes read.

Django CMS

Django is one of the most popular open-source Python web development framework. Developers use Django to build a website from scratch. Like WordPress, Django CMS is also a Content Management...

9 minutes read.

Django Admin

To manage the content of the website, e.g., adding and deleting posts, web developers need an admin portal on the website. The admin portal allows trusted users, also knows as...

3 minutes read.

Features of Django

Features of Django Offers High Security:- Django is super secure. To prove the feature, we can always take examples of lots of websites which are present worldwide and possess huge traffic. Django is secure because it covers the...

3 minutes read.

Django Create App

Django is a Python-based web framework that is based on the Model View Template architecture. In this post, we will go through the installation and setup for our first Django...

4 minutes read.

Django Model Fields

In Django, if we want to store the information about anything, we use Models. It contains various fields about the data. Generally, each model represents a database where all the...

4 minutes read.

Django Frontend

Django Django is a framework that can make web applications more accessible and faster. Django is an open-source collection of python libraries, and Django can be used for both the front...

3 minutes read.

Django Template System

Django Template System Django allows Python and HTML to be divided, the Python goes into views, and HTML goes into templates. Django relies on the render feature and the language of the Django Model...

5 minutes read.

Life Cycle of Django

Life Cycle of Django Request - Response life cycle in Django  Thebasic principle of http protocol is the client sends a request to the server based on the request data, and the server sends...

3 minutes read.

How to Implement Multiple User Types with Django

Django Framework: Django is a framework that can make web applications more accessible and faster. Django is an open-source collection of python libraries, and Django can be used for both the...

3 minutes read.

Best Django courses

In this tutorial, we will see various courses in python Django available on the internet. Django has become popular these days, so courses on it are also increasing day by...

7 minutes read.

Django Model

Django Model A template is a class representing a table or collection in our DB and where each class attribute is a table or collection area. In the app / models.py, models are...

6 minutes read.

Uses of Django

Django is a free and open-source Python-based web framework, which offers great functionalities to its users. Django was developed in 2003, and the final version was released in 2008 by...

3 minutes read.

Django Class Based Views

Class-based views were developed to solve the problem of customizing function-based generic views. We can arrange our view code Object-Oriented by using class views. Class-based views do more than function-based...

3 minutes read.

Django Create Superuser

For managing the website, we need an Admin while creating the Django application. In order to create the admin user, create superuser command is used. The superuser is called the...

3 minutes read.