0% found this document useful (0 votes)
19 views

Django

The document outlines the steps to create a basic Django web application. It covers installing Django and required packages, creating and configuring a project, generating models and migrations, creating views and templates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Django

The document outlines the steps to create a basic Django web application. It covers installing Django and required packages, creating and configuring a project, generating models and migrations, creating views and templates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

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

# Create your models here.


class products(models.Model):
title=models.TextField()
description=models.TextField()
price=models.TextField()
summary=models.TextField(default='this is cool! ')
19.do migrations whenever changes are done
python manage.py migrate
python manage.py makemigrations
20.add the products to the admin.py
from django.contrib import admin

# Register your models here.


from .models import products
admin.site.register(products)
21.again do the migrations
python manage.py migrate
python manage.py makemigrations
22.activate the python shell
python ./manage.py shell
23.acess the objects using shell
#import products
from products.models import products
24.acess the objects
products.objects.all()
25.add a object
products.objects.create(title='hi',description='hello',price='1991',summary='sweet'
)
and now.........................
delete the files in pycache not init.py
and delete the dbsqlite.3

26.change the textfields in the models.py


from django.db import models

# Create your models here.


class products(models.Model):
title=models.CharField(max_length=120)
description=models.TextField(blank=True,null=True)
price=models.DecimalField(decimal_places=2,max_digits=1000)
summary=models.TextField(default='this is cool! ')
27.create database with
python manage.py createsuperuser
28.apply migrations
python manage.py makemigrations
python manage.py migrate
29.add a newfield to the models.py
featured=models.BooleanField()
30.apply migrations
enter 1
enter True
31.apply migrations
32.#change the view from default view to custom view
create a app
python manage.py startapp pages
add it to the applications in the setting.py
33.change the view.py in the pages
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.


def home_view(*args,**kwargs):
return HttpResponse("<h1>........Fuck You........</h1>")
34.update the urls.py folder
"""mypro URL Configuration

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 %}

You might also like