~By AbhinayHow to initiate Django project

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 :-
- Make a folder inside a main folder (ex foldername → Django_Project)
- cd Django_Project
- django-admin startproject sample_project
- django-admin startapp sample_app
- python manage.py runserver → will provide you a sever link in local
- (ctrl + c to break/stop the server )
- now, make two folders inside sample_app →
- static → JavaScript, css , bootstraps
- templates → website html pages
contact delivering network - cdn links (ex- bootstrap links in html of css and js)
- Now, in setting.py of sample_project
- 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
]
- template section add in →
'DIRS': [BASE_DIR / 'templates']
- At last write →
STATIC_URL = 'static/'
STATICFILES_DIR= [
BASE_DIR / 'static'
]
- make a function inside views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,"index.html")
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
]
- now, in urls.py of sample_project
from django.contrib import admin
from django.urls import path, includeurlpatterns = [
path('admin/', admin.site.urls),
path('',include('sample_app.urls'))
]
Migration
pip install mysqlclient
- change DATABASE of settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'trial_django',
'PASSWORD': "",
'PORT': "3306",
'HOST': "localhost",
'USER' : "root"
}
}
- In xampp make the same database of name mentioned above (i.e trial_django)
- run the command below in terminal → will get ok
py manage.py makemigrations py manage.py migrate
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"
- 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__"
- 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
- In trial_django you will see one file named userinfo where u will se
id | username | password | email | date