How to create a small project using Django

In the last tutorial we learned how to install a Django so in this section we are going to discuss how to create a small project using Django. we already created Django admin users using the Django manage.py createsuperuser .In this “create a small project using Django” tutorial we are going to create a small blog that allow users to post their thoughts one by one lets see in detail

Steps to create a small project using Django

1) Create your app first

#python manage.py startapp tutorialshore

How to create a small project using Django step 1

See the automatically created folder structure

How to create a small project using Django step 1 project structure

__init__.py : package files
admin.py : This files is help to make the changes in the admin interface
models.py : This is one of the important files all the application are stored in this files
tests.py : Create your tests here.
views.py : This is another important files all the views interface managed by this files

 

2) We have to change URL pattern open myblog =>myblog=>urls.py

Add below code

from django.urls import include
from django.urls import path
urlpatterns += [
    path('tutorialshore/', include('tutorialshore.urls')),
]

Then urls.py become

"""myblog URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
	
]
from django.urls import include
from django.urls import path
urlpatterns += [
    path('tutorialshore/', include('tutorialshore.urls')),
]

3) Create a new urls.py file inside tutorialshore folder and add below lines

 

from django.urls import path
from . import views

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

4)  Open view.py files add below code

 

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

def post(request):
 text = """<h1>welcome to my post View !</h1>"""
 return HttpResponse(text)
 
def index(request):
 text = """<h1>welcome to my blog!</h1>"""
 return HttpResponse(text)

# Create your views here.



5) Run # python manage.py runserver

5) Open http://127.0.0.1:8000/tutorialshore/ in your browser

 

How to create a small project using Django step 2

6) open http://127.0.0.1:8000/tutorialshore/post/

How to create a small project using Django step 3

Next => How to create new admin user or super user in Django python

2 thoughts on “How to create a small project using Django”

Leave a Reply

Your email address will not be published. Required fields are marked *