pip install Django
django-admin Startproject blog
python manage.py runserver
from django.contrib import admin
from django.urls import path, include
from .import views
urlpatterns = [
path('admin/', admin.site.urls),
# path('articles/', include('articles.urls')),
path(r'about/', views.about),
path(r'', views.homepage)
]
from django.http import HttpResponse
from django.shortcuts import render
def about(request):
# return HttpResponse("about")
return render(request, 'about.html')
def homepage(request):
# return HttpResponse("homepage")
return render(request, 'home.html')
The requesr go to urls python file and search for correct url and relevant view function is called which render the html file and send a response.
python manage.py startapp articles
By usintg this command new folder and files will be created representing the articles part of the project.
This folder contains its own views.py(you can create its own urls.py and Template file).
from django.db import models
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField()
body = models.TextField()
date = models.DateField(auto_now_add=True)
#
#
def __str__(self):
return self.title
To connect and communicate with database we need to migrate the model.
- To migrate the built-in module :
python manage.py migrate
- Make a migration file
python manage.py makemigrations
- To migrate our own module after creating migration file :
python manage.py migrate
After any change in the migration i.e change in model or adding new module we need to migrate to update.
- Open a new python shell.
python manage.py shell
- import Article model to use it.
from articles.modles import Article
- To display all objects.
Article.objects.all()
- To create a object.
article=Article()
- Insert values in a object.
article.title="hello india"
- To save the changes of the object in the database.
article.save()
- This is a builtin function to display object with one if its value.
def __str__(self):
return self.title
- We can use modal methods which will help in customizing the data and to other functionality.
def snippet(self):
return self.body[:50]...
- To create a admin .
python manage.py createsuperuser
- Enter the details .
Username(leave blank for '[yourname]'):
Email address:
password:
- set our module for admin area (admin python file).
from .models import Article
# Register your models here.
admin.site.register(Article)
- Sending the data in the response .
from django.shortcuts import render
from .models import Article
# Create your views here.
def articleHomepage(request):
# return HttpResponse("homepage")
articles = Article.objects.all().order_by('date')
return render(request, 'articles/articleHome.html', {'articles': articles})
-
Using The template Tags in Html file .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Articles</title>
<link rel="stylesheet" href="/static/style.css" />
</head>
<body>
<div class="articles">
{% for article in articles %}
<div class="article">
<h2><a href="#"> {{article.title}} </a></h2>
<p>{{article.snippet}}..</p>
</div>
{% endfor %}
</div>
</body>
</html>
- In the above we used a model methods in model file for show only a glimps of the paragraph and called that function in the html file
def snippet(self):
return self.body[:50]
- URLS file .
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
- Settings python file .
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'assets')
]
We can create our own url pattern to capture the a url with perticular pattern and we can send that captured url to view file which will customize the output with respective to the url .
-
- Add a re_path will a regular expression.
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
# path('admin/', admin.site.urls),
# path(r'about/', views.about),
# path(r'', views.articleHomepage, ),
re_path(r'(?P<slug>[\w-]+)/$', views.articleDetailpage)
]
-
- In views file add the module for the url pattens with a pattern a function argument .
from django.http import HttpResponse
from django.shortcuts import render
def articleDetailpage(request, slug):
# return HttpResponse(slug)
# articles = Article.objects.all().order_by('date')
# return render(request, 'articles/articleHome.html', {'articles': articles})
article = Article.objects.get(slug=slug)
return render(request, 'articles/articleDetail.html', {'article': article})
-
- Add third parameter to the path with name as identifiers.
app_name = 'article'
urlpatterns = [
# path('admin/', admin.site.urls),
path(r'about/', views.about),
path(r'', views.articleHomepage, name="list"),
re_path(r'(?P<slug>[\w-]+)/$', views.articleDetailpage, name="detail")
]
-
- Use the name of the url in the href of the element .
{% for article in articles %}
<div class="article">
<h2>
<a href="{% url 'article:detail' slug=article.slug%}">
{{article.title}}
</a>
</h2>
<p>{{article.snippet}}..</p>
</div>
{% endfor %}
We can add media to the database and store it in some file location.The process is similar to adding a static files.
-
- Add Media url and media root path.
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
-
- Use the name of the url in the href of the element .
from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.shortcuts import render ,redirect
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
from django.contrib.auth import login,logout
# Create your views here.
def signup_view(request):
# return HttpResponse("about")
if request.method == 'POST':
form=UserCreationForm(request.POST)
if form.is_valid():
user=form.save()
#log the user
login(request,user)
return redirect('article:list')
else:
form=UserCreationForm()
return render(request, 'accounts/signup.html',{'form':form})
def login_view(request):
# return HttpResponse("about")
if request.method == 'POST':
form=AuthenticationForm(data=request.POST)
if form.is_valid():
# form.save()
#log the user
user=form.get_user()
login(request,user)
if 'next' in request.POST:
return redirect(request.POST.get('next'))
else:
return redirect('article:list')
else:
return render(request, 'accounts/login.html',{'form':form})
else:
form=AuthenticationForm()
return render(request, 'accounts/login.html',{'form':form})
def logout_view(request):
# return HttpResponse("about")
if request.method == 'POST':
logout(request)
return redirect('article:list')
from django.urls import path, re_path
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from .import views
# from django.contrib import admin
app_name = 'accounts'
urlpatterns = [
# path('admin/', admin.site.urls),
path(r'signup/', views.signup_view,name='signup'),
path(r'login/', views.login_view,name='login'),
path(r'logout/', views.logout_view,name='logout'),
# re_path(r'(?P<slug>[\w-]+)/$', views.articleDetailpage, name="detail")
]
urlpatterns += staticfiles_urlpatterns()
# import user creation form
from django.contrib.auth.forms import UserCreationForm
#create a user creation form
form=UserCreationForm()
# pass the form in render template return
return render(request, 'accounts/signup.html',{'form':form})
<!-- user the form -->
{% extends 'baseLayout.html' %}
{% block content %}
<h1>Signup</h1>
<form class="site-form" action="{% url 'accounts:signup' %}" method="post">
<!-- csrf is user for security -->
{% csrf_token %}
{{form}}
<input type="submit" value="Signup">
</form>
{% endblock %}
# check for the post request
if request.method == 'POST':
form=UserCreationForm(request.POST)
# check for valid form
if form.is_valid():
# add user
user=form.save()
#log the user
login(request,user)
# go to home page
return redirect('article:list')
# import user Authenticationform
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
#create a authenticate form
form=AuthenticationForm()
# pass the form in render template return
return render(request, 'accounts/login.html',{'form':form})
<!-- user the form -->
{% extends 'baseLayout.html' %}
{% block content %}
<h1>Login</h1>
<form class="site-form" action="{% url 'accounts:login' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Login">
</form>
{% endblock %}
# check for the post request
if request.method == 'POST':
form=AuthenticationForm(data=request.POST)
if form.is_valid():
# create a user
user=form.get_user()
# log the user in
login(request,user)
return redirect('article:list')
def logout_view(request):
if request.method == 'POST':
logout(request)
return redirect('article:list')
<!-- add the logout tag-->
<form class="logiut-link" action="{% url 'accounts:logout' %}" method="POST">
{% csrf_token %}
<button type="submit">Logout</button>
</form>