In this tutorial, you will learn how to develop a web application with Django. The first part includes the steps to install the basic tools for application development.

0. Github repository of the project

https://github.com/openescuela/opsadjangoblog/tree/main/tuto1

 1. Project creation

$ mkdir myprojectdir
$ cd myprojectdir
$ virtualenv myenv
$ source myenv/bin/activate
$ (myenv) pip install django
$ (myenv) django-admin startproject mysite .
$ (myenv) django-admin startapp blogs .

 2. Project configuration

mysite/settings.py 

.
.
.
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     
    'blogs', # new
]
.
.
.

 3. Creating a view

In views.py add the following code :

blogs/views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello world !")

 4. Creating an url file 

In the blogs application directory create a new file named urls.py and add the following code to it

blogs/urls.py 

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index')
]

 5. Add the application urls to the url patterns of the project

Add the following code in the urls.py file of the mysite directory

mysite/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('blog/', include('blogs.urls')),
    path('admin/', admin.site.urls),
]

6. Start the server

python manage.py runserver

7. Go to the browser

Insert the localhost address : 127.0.0.1:8000/blog/

0 comment

There are no comments yet.

Log in to leave a reply

Related posts

Developing a Web Application with Django Part 2: Templates

15/12/2021

Developing a Web Application with Django Part 3 : Models

7/1/2022

Learn Python by Example | Basics

15/6/2022