I am using Pycharm editor (Download Pycharm and install)
Let’s Start
Create a new pycharm project, The name of my pycharm project is project.
Now we will install django
Right-click and open console or terminal
pip3 install django
Hit Enter...
django-admin startproject myfirstdjangoproject
Run server
cd myfirstdjangoproject
python3 manage.py runserver
Our first work is complete. Now you have to create an application under this project. There can be many apps under a project.
python3 manage.py startapp myfirstapp
Now you have to create a file under my first app "File name – urls.py". We will create a file "urls.py " inside the app folder. Next Open urls.py from myfirstdjangoproject.
"""myfirstdjangoproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.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,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('myfirstapp.urls')),
]
Next Open urls.py from myfirstapp
from django.contrib import admin
from django.urls import path,include
from myfirstapp import views
urlpatterns = [
path("",views.index,name='myfirstapp'),
]
Now we need to create a template folder. Right-click on the project folder and make a new dir name as a template. And under the template create an html file. name index.html
Open views.py file
from django.shortcuts import render,HttpResponse
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request,"index.html")
Next, Open settings.py from myfirstdjangoproject and set the template path
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR/"templete",],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !