~By AbhinayHow to initiate Django project

title image

Before we start we need to do this few step in cammand:

  • pip install django
  • pip install mysql-client
  • pip install pillow
  • pip install numpy
  • pip install pandas
  • pip install matplotlib

Do this to start/initiate django project :-

  1. Make a folder inside a main folder (ex foldername → Django_Project)
  2. cd Django_Project
  3. django-admin startproject sample_project
  4. django-admin startapp sample_app
  5. python manage.py runserver → will provide you a sever link in local
  6. (ctrl + c to break/stop the server )
  7. now, make two folders inside sample_app →
    1. static → JavaScript, css , bootstraps
    2. templates → website html pages
contact delivering network - cdn links (ex- bootstrap links in html of css and js)
  1. Now, in setting.py of sample_project
    1. now in section of INSTALLED_APP

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'sample_app' #THIS LINE IS NEED TO BE ADDED
]

  1. template section add in →

'DIRS': [BASE_DIR / 'templates']

  1. At last write →

STATIC_URL = 'static/'
STATICFILES_DIR= [
BASE_DIR / 'static'
]

  1. make a function inside views.py

from django.shortcuts import render

# Create your views here.
def index(request):
return render(request,"index.html")

  1. in urls.py in sample_app we need to define this inside url.py of sample_project

from django.urls import path
from . import views
# this means we are importing views here . means from same directory
urlpatterns = [
path('',views.index,name="home") #index is function name here
]

  1. now, in urls.py of sample_project

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


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

Migration

pip install mysqlclient
  1. change DATABASE of settings.py

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'trial_django',
'PASSWORD': "",
'PORT': "3306",
'HOST': "localhost",
'USER' : "root"
}
}

  1. In xampp make the same database of name mentioned above (i.e trial_django)
  2. run the command below in terminal → will get ok
py manage.py makemigrations py manage.py migrate
  1. python manage.py createsuperuser
  2. models.py of sample_app write the code given below in it

from django.db import models

# Create your models here.
class Userdetails(models.Model):
id=models.AutoField(primary_key=True)
username = models.CharField(max_length=200)
password = models.CharField(max_length = 200)
email = models.EmailField(max_length=200)
date = models.DateTimeField(max_length=200)

class Meta:
db_table="userinfo"


  1. make forms.py in sample_app and write this code

from django import forms
from sample_app.models import Userdetails

class UserInfo(forms.ModelForm):
class Meta:
model = Userdetails
fields = "__all__"

  1. Now run the server http://localhost/phpmyadmin/index.php?route=/sql&db=trial_django&table=userinfo&pos=0 this one if you don’t receive this just after running the server
  2. In trial_django you will see one file named userinfo where u will se

id | username | password | email | date