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

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 Channels

Channels is a project that uses Django and adds some functionalities beyond the traditional HTTP by using WSGI and ASGI.  We can use both synchronous and asynchronous requests with ASGI...

5 minutes read.

Django DRF

Django Rest Framework, which is also referred to as DRF, is a very powerful application to build RESTful APIs in a Django application. DRF uses the Django framework. Similar to...

3 minutes read.

Django Tutorial

Django Introduction It is a web application framework written in a python programming language. It based on the MVT( Model View Templet) design pattern. It takes a short time to build an application...

2 minutes read.

Django Forms

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. It...

3 minutes read.

Django CSRF

Django is a high-level python based web framework. Django is open source and free to use for all. Django provides many features to web developers like scalability, security, rapid development,...

4 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 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 Messages

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...

6 minutes read.

How to Uninstall Django from cmd

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

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.

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.

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.

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 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 Authentication

Django is an open-source free to use web framework which is based on Python. Django uses Model-View-Template as its architecture. The final version of Django was released in 2008. Django...

4 minutes read.

Creating View in Django

Creating View in Django    Django views are a key component of the frameworkbased applications.We are using Python function or class at their simplest, which takes up a web query and returns a web response.Views are used to get objects from the server, change objects if needed, render types, return HTML, and much more. Class Based View and Function based Django has two types of views: views based on functions (FBVs) and views based on class (CBVs).Initially, Django began with only FBVs, but then introduced CBVs as a way to model features so we didn't have to write boilerplate (i.e. the same software) code again and again.CBVs would be a way to increase efficiency, so you don't have to write as much code as you can. CBVs are classes of Python at their heart. Django ships with a range of "model" CBVs with pre-configured features that can be reused and often expanded.Then helpful names are given to these classes that explain what kind of functionality they provide.These are often referred to as "universal views" because they provide solutions to specific needs. The classes have a journal View function, or "view" for short, is simply a Python program that uses a web query and returns a web response. This response may include the HTML content of a web page, or a redirect, or a 404 error, or an XML file, or an image, etc. Example: You use a view to create a web page, remember that you need to connect a view to a URL to see it as a web page. Simple View We will create a simple view in myapplication to say “Welocome to django application” See the following view – from django.http import HttpResponse ...

4 minutes read.

Django MVT

 MVT(Model View Template) MVT means Model View Template is a software design pattern The View is used to implement the business logic and to communicate with a model for data transmission and to...

2 minutes read.

Django CMS vs WordPress

Both Django CMS and WordPress belong "Self-Hosted Blogging" or "Content Management System (CMS)" category, that is used to manage digital content. Bloggers usually use these platforms. Django CMS Django is one of...

4 minutes read.