Authentication Django Code
views.py
from django.core.checks import messages
from django.contrib import messages
from django.db.models import fields
from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404, render, redirect, HttpResponse
from django.contrib.auth.models import User
from django.utils import timezone
from .models import *
from django.contrib.auth import authenticate, login, logout
def home(request):
featured = Post.objects.filter(featured=True)
context = {
'featured': featured,
}
return render(request, "blog/index.html", context)
def about(request):
context = {
}
return render(request, "blog/about.html", context)
def contact(request):
if request.method=="POST":
name=request.POST['name']
email=request.POST['email']
phone=request.POST['phone']
content =request.POST['content']
if len(name)<2 -="" allposts="" been="" blog.html="" blog="" comment.save="" comment="" comments="" contact.html="" contact.save="" contact="Contact(name=name," content="content)" context="" correctly="" def="" else:="" email="email," f="" fill="" form="" found="" has="" httpresponse="" if="" len="" message="" messages.error="" messages.success="" not="" or="" our="" phone="phone," please="" post.html="" post.slug="" post="post)" postcomment="" posted="" postsno="" query="" redirect="" render="" request.method="=" request="" return="" search="" sent="" slug="" successfully="" the="" timestamp="" user="user,">78:
allPosts=Post.objects.none()
else:
allPostsTitle= Post.objects.filter(title__icontains=query)
allPostsAuthor= Post.objects.filter(author__icontains=query)
allPostsContent =Post.objects.filter(content__icontains=query)
allPostsshort_description =Post.objects.filter(content__icontains=query)
allPosts= allPostsTitle.union(allPostsContent, allPostsAuthor, allPostsshort_description)
if allPosts.count()==0:
messages.warning(request, "No search results found. Please refine your query.")
params={'allPosts': allPosts, 'query': query}
return render(request, 'blog/search.html', params)
def handleSignUp(request):
if request.method=="POST":
# Get the post parameters
username=request.POST['username']
email=request.POST['email']
fname=request.POST['fname']
lname=request.POST['lname']
pass1=request.POST['pass1']
pass2=request.POST['pass2']
# check for errorneous input
if len(username)<10: -="" 10="" account="" again="" already="" and="" be="" been="" characters="" code="" contain="" create="" created="" credentials="" def="" do="" else:="" email="" found="" get="" handellogout="" handelogin="" has="" his="" home="" httpresponse="" if="" in="" is="" letters="" logged="" login="" loginpassword="" loginusername="" logout="" match="" messages.error="" messages.success="" must="" myuser.first_name="fname" myuser.last_name="lname" myuser.save="" myuser="User.objects.create_user(username," name="" none:="" not="" numbers="" nvalid="" only="" out="" parameters="" pass1="" pass2="" password="loginpassword)" passwords="" please="" post="" redirect="" request.method="=" request="" return="" should="" successfully="" taken="" the="" try="" uccessfully="" under="" user.objects.filter="" user="" username.isalnum="" username="" your="">10:>2>
urls.py
from django.contrib import admin
from django.urls import path
from django.urls.conf import include
from . import views
urlpatterns = [
path('postComment', views.postComment, name="postComment"),
path('', views.home, name='home'),
path('signup', views.handleSignUp, name="handleSignUp"),
path('login', views.handeLogin, name="handleLogin"),
path('logout', views.handelLogout, name="handleLogout"),
path('search', views.search, name="search"),
path('contact', views.contact, name='contact'),
path('blog', views.blog, name='blog'),
path('about', views.about, name='about'),
path('', views.post, name="post"),
]
models.py
from django.db import models
from django.contrib.auth.models import User
from ckeditor.fields import RichTextField
from django.utils.timezone import now
class Contact(models.Model):
sno= models.AutoField(primary_key=True)
name= models.CharField(max_length=255)
phone= models.CharField(max_length=13)
email= models.CharField(max_length=100)
content= models.TextField()
timeStamp=models.DateTimeField(auto_now_add=True, blank=True)
def __str__(self):
return "Message from " + self.name + ' - ' + self.email
class Post(models.Model):
sno=models.AutoField(primary_key=True)
title=models.CharField(max_length=255)
short_description = models.CharField(max_length=200, null=True, blank=True)
author=models.CharField(max_length=25)
slug=models.CharField(max_length=130)
timeStamp=models.DateTimeField(blank=True)
content=RichTextField()
thumbnail = models.ImageField()
featured = models.BooleanField(null=True, blank=True)
# featured = models.BooleanField()
def __str__(self):
return self.title + " by " + self.author
# for comment model
class BlogComment(models.Model):
sno = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
comment =models.TextField()
post = models.ForeignKey(Post, on_delete=models.CASCADE)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
timestamp = models.DateTimeField(default=now)
# def __str__(self):
# return self.user
admin.py
from django.contrib import admin
from .models import *
admin.site.register(Contact)
admin.site.register(Post)
# admin.site.register(BlogComment)
@admin.register(BlogComment)
class BlogCommentAdmin(admin.ModelAdmin):
list_display = ['user', 'timestamp']
list_filter = ['timestamp']
# prepopulated_fields = {"slug": ("comment")}
# search_fields = ['foreign_key__related_user']
project folder
urls.py
from django.contrib import admin
from django.urls import path
from django.urls.conf import include
# from allauth.account.urls
from django.conf.urls.static import static
from django.conf import settings
admin.site.site_header = "Blog Website Admin"
admin.site.site_title = "Blog Website"
admin.site.index_title = "Welcome to Blog Admin Panel"
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('', include('blog.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Tags
django