Django
Django
Install python
2.Install pip
3.Install virtual env
pip install virtual env
4.Create a dir
mkdir Dev
5.Create a project dir
mkdir mypro
6.Create a Virtual env
python -m virtualenv mypro
7.activate the virtualenv
.\mypro\Scripts\activate
8.install django
pip install Django==4.0.2
9.Create a project
django-admin startproject (project name)mypro .
10.run the project
python .\manage.py runserver
11.Manage the database
python .\manage.py migrate
12.open another terminal leave the get requests terminal activate virtualenv
with ...7... check for migrate
python .\manage.py migrate
13.acess the admin panel in django
<http://lo................./admin>
14.Create superuser for the web application
python manage.py createsuperuser
#enter the details
15.login
16.create a app
python .\manage.py startapp products
17.add the app to INSTALLED APPS to settings in the project
'products',
18.edit the models.py file in products
from django.db import models
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/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
from pages.views import home_view
urlpatterns = [
path('', home_view, name='home'),
path('admin/', admin.site.urls),
]
35.adding a html page to the project
1.Create a templates folder in the project
2.save the html file in that folder
3.update the return statement in the view.py
def contact_view(request,*args,**kargs):
return render(request,"contact.html",{})
4.update the location in the templates folder in the settings.py file
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,"templates")],
36.For loop in templates
{% for i in mylist %}
<li>{{ i }} </li>
{% endfor %}