Hey guys, I am learning Django and after learning all the basics and some advanced topics, I started to develop projects. Here is my first Django blog project.
Codes Under "my_blog_project/App_Blog/admin.py" file
from django.contrib import admin
from App_Blog.models import Blog, Comment, Likes
# Register your models here.
admin.site.register(Blog)
admin.site.register(Comment)
admin.site.register(Likes)
Codes Under "my_blog_project/App_Blog/apps.py" file
from django.apps import AppConfig
class AppBlogConfig(AppConfig):
name = 'App_Blog'
Codes Under "my_blog_project/App_Blog/forms.py" file
from django import forms
from App_Blog.models import Blog,Comment,Likes
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('comment',)
Codes Under "my_blog_project/App_Blog/models.py" file
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Blog(models.Model):
author = models.ForeignKey(User, on_delete = models.CASCADE, related_name='post_author')
blog_title = models.CharField(max_length = 264, verbose_name = 'Input your title')
slug = models.SlugField(max_length = 264, unique = True)
blog_content = models.TextField(verbose_name = 'What is on your mind ?')
blog_image = models.ImageField(upload_to = 'blog_images',verbose_name = 'Image')
publish_date = models.DateTimeField(auto_now_add = True)
update_date = models.DateTimeField(auto_now = True)
class Meta:
ordering = ('-publish_date',)
def __str__(self):
return self.blog_title
class Comment(models.Model):
blog = models.ForeignKey(Blog, on_delete = models.CASCADE, related_name = 'blog_comment')
user = models.ForeignKey(User, on_delete = models.CASCADE, related_name = 'user_comment')
comment = models.TextField(max_length = 264)
comment_date = models.DateTimeField(auto_now_add = True)
def __str__(self):
return self.comment
class Likes(models.Model):
blog = models.ForeignKey(Blog, on_delete = models.CASCADE, related_name = 'liked_blog')
user = models.ForeignKey(User, on_delete = models.CASCADE, related_name = 'liker_user')
def __str__(self):
return self.user + ' likes ' + self.blog
Codes Under "my_blog_project/App_Blog/urls.py" file
from django.urls import path,include
from . import views
app_name = 'App_Blog'
urlpatterns = [
path('',views.BlogList.as_view(), name = 'blog_list'),
path('create/', views.CreateBlog.as_view(), name='create_blog'),
path('details/<str:slug>',views.blog_details, name='blog_details'),
path('liked/<pk>/',views.liked,name='liked'),
path('unliked/<pk>/',views.unliked,name='unliked'),
path('my_blogs/',views.MyBlogs.as_view(),name='my_blogs'),
path('edit_blog/<pk>/',views.UpdateBlog.as_view(),name='edit_blog'),
]
Codes Under "my_blog_project/App_Blog/views.py" file
from django.shortcuts import render,HttpResponseRedirect
from django.views.generic import CreateView, UpdateView, ListView, DetailView, View, TemplateView, DeleteView
from App_Blog.models import Blog,Comment,Likes
from django.urls import reverse, reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
import uuid
from App_Blog.forms import CommentForm
# Create your views here.
class CreateBlog(LoginRequiredMixin, CreateView):
model = Blog
template_name = 'App_Blog/create_blog.html'
fields = ('blog_title','blog_content','blog_image')
def form_valid(self,form):
blog_obj = form.save(commit=False)
blog_obj.author = self.request.user
title = blog_obj.blog_title
blog_obj.slug = title.replace(' ','-') + '-' + str(uuid.uuid4())
blog_obj.save()
return HttpResponseRedirect(reverse('index'))
class BlogList(ListView):
context_object_name = 'blogs'
model = Blog
template_name = 'App_Blog/blog_list.html'
@login_required
def blog_details(request, slug):
blog = Blog.objects.get(slug=slug)
comment_form = CommentForm()
already_liked = Likes.objects.filter(blog=blog,user=request.user)
if already_liked:
liked = True
else:
liked = False
if request.method == 'POST':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comm = comment_form.save(commit=False)
comm.user = request.user
comm.blog = blog
comm.save()
return HttpResponseRedirect(reverse('App_Blog:blog_details',kwargs={'slug':slug}))
return render(request,'App_Blog/blog_details.html',context={'blog':blog,'comment_form':comment_form,'liked':liked})
@login_required
def liked(request,pk):
blog = Blog.objects.get(pk=pk)
user = request.user
already_liked = Likes.objects.filter(blog=blog,user=user)
if not already_liked:
liked_post = Likes(blog=blog,user=user)
liked_post.save()
return HttpResponseRedirect(reverse('App_Blog:blog_details',kwargs={'slug':blog.slug}))
@login_required
def unliked(request,pk):
blog = Blog.objects.get(pk=pk)
user = request.user
already_liked = Likes.objects.filter(blog=blog,user=user)
already_liked.delete()
return HttpResponseRedirect(reverse('App_Blog:blog_details',kwargs={'slug':blog.slug}))
class MyBlogs(LoginRequiredMixin,TemplateView):
template_name = 'App_Blog/my_blogs.html'
class UpdateBlog(LoginRequiredMixin,UpdateView):
model = Blog
template_name = 'App_Blog/edit_blog.html'
fields = ('blog_title','blog_content','blog_image')
def get_success_url(self, **kwargs):
return reverse_lazy('App_Blog:blog_details',kwargs={'slug':self.object.slug})
To be continued...
Congratulations @sksksk! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):
Your next target is to reach 400 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Support the HiveBuzz project. Vote for our proposal!
@tipu curate 5
Upvoted 👌 (Mana: 1/51) Liquid rewards.