Setting up django project and creating home page.



In this tutorial we will start by creating a django project then we will create an accounts app in it. In this accounts app we will create our home page template.

We will start by creating a django project "website".

django-admin startproject website

 

Open the website folder and create "accounts" app.

python manage.py startapp accounts

 

Add accounts app in installed apps in settings.py

 

Edit website urls.py as below

from django.contrib import admin

from django.urls import path,include

urlpatterns = [

    path('admin/', admin.site.urls),

    path('',include('accounts.urls'))

]

 

In accounts, create urls.py file and add following code.

from django.urls import path

from . import views

urlpatterns = [

    path('',views.home,name='home'),

 ]

 

In views.py file of accounts we will define a home function.

from django.shortcuts import render

def home(request):

    return render(request,'accounts/home.html')

 

In accounts, create a folder and name it as templates, inside this templates folder create a folder and name it as accounts. In this accounts folder create an html file and name it as home.html. Add following html code in home.html file.

 

<html>

<head>

  <title>Home</title>

</head>

<body align="center">

  <br/>

  <h2>This is our home page</h2>

</body>

</html>

 

Now runserver with following command

python manage.py runserver

Now visit http://127.0.0.1:8000/. Home page will look like below.


Published : July 18, 2020