<file_sep>from django.shortcuts import render,redirect,get_object_or_404
from blog.models import Post
from blog import forms
from django.http import HttpResponseRedirect
from taggit.models import Tag
from django.contrib.auth.decorators import login_required

# Create your views here.
def postlist_view(request,tag_slug=None):
    post_list=Post.objects.all()
    tag=None
    if tag_slug:
        tag=get_object_or_404(Tag,slug=tag_slug)
        post_list=post_list.filter(tags__in=[tag])
    return render(request,'blog/post_list.html',{'post_list':post_list,'tag':tag})

def postcreate_view(request):
    form=forms.CreateForm()
    if request.method=='POST':
        form=forms.CreateForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('/home')
    return render(request,'blog/post_create.html',{'form':form})

@login_required
def postdetail_view(request,year,month,day,post):
    post=get_object_or_404(Post,status='published',slug=post,publish__year=year,publish__month=month,publish__day=day)
    comments=post.comments.filter(active=True)
    csubmit=False
    if request.method=='POST':
        form=forms.CommentForm(request.POST)
        if form.is_valid():
            new_comment=form.save(commit=False)
            new_comment.post=post
            new_comment.save()
            csubmit=True
    else:
        form=forms.CommentForm()
    likes=post.likes.filter(active=True)
    click=False
    if request.method=='POST':
        form1=forms.LikeForm(request.POST)
        if form1.is_valid():
            new_like=form1.save(commit=False)
            new_like.post=post
            new_like.save()
            click=True
    else:
        form1=forms.LikeForm()
    return render(request,'blog/post_detail.html',{'post':post,'comments':comments,'csubmit':csubmit,'form':form,'form1':form1,'click':click,'likes':likes})

def postupdate_view(request,id):
    post_list=Post.objects.get(id=id)
    if request.method=='POST':
        form=forms.UpdateForm(request.POST,instance=post_list)
        if form.is_valid():
            form.save()
        return redirect('/home')
    return render(request,'blog/post_update.html',{'post_list':post_list})

def postdelete_view(request,id):
    post=Post.objects.get(id=id)
    post.delete()
    return redirect('/home')

def signup_view(request):
    form=forms.SignUpForm()
    if request.method=='POST':
        form=forms.SignUpForm(request.POST)
        if form.is_valid():
            user=form.save(commit=True)
            user.set_password(<PASSWORD>)
            user.save()
        return HttpResponseRedirect('/accounts/login')
    return render(request,'blog/signup.html',{'form':form})

def logout_view(request):
    return render(request,'blog/logout.html')
<file_sep>"""blogproject13 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/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,include
from blog import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/',views.postlist_view,name='home'),
    path('create/',views.postcreate_view),
    path('(?p<year>\d{4})/(?p<month>\d{2})/(?p<day>\d{2})/(?p<post>[-\w+])/$',views.postdetail_view,name='detail'),
    path('update/<int:id>',views.postupdate_view,name='list'),
    path('delete/<int:id>',views.postdelete_view),
    path('signup/',views.signup_view),
    path('accounts/',include('django.contrib.auth.urls')),
    path('logout/',views.logout_view),
    path('tag/(?p<tag_slug>[-\w]+)/$',views.postlist_view,name='post_tags'),
]
<file_sep>from django import forms
from blog.models import Post,Comment,Like
from django.contrib.auth.models import User

class CreateForm(forms.ModelForm):
    class Meta:
        model=Post
        fields='__all__'

class UpdateForm(forms.ModelForm):
    class Meta:
        model=Post
        fields=('title','body')

class CommentForm(forms.ModelForm):
    class Meta:
        model=Comment
        fields=('name','body')

class LikeForm(forms.ModelForm):
    class Meta:
        model=Like
        fields=('uname',)

class SignUpForm(forms.ModelForm):
    class Meta:
        model=User
        fields=('username','<PASSWORD>','email')
<file_sep>from django.contrib import admin
from blog.models import Post,Comment,Like

class PostAdmin(admin.ModelAdmin):
    list_display=['title','slug','author','body','publish','created','updated','status']
    list_filter=('status','created')
    prepopulated_fields={'slug':('title',)}
    search_fields=('title','body')
    raw_id_fields=('author',)
    date_hierarchy='publish'

class CommentAdmin(admin.ModelAdmin):
    list_display=['post','name','email','body','created','updated','active']
    list_filter=('created','active')
    date_hierarchy='created'

class LikeAdmin(admin.ModelAdmin):
    list_display=['post','uname','email','created','updated','active']
    list_filter=('created','active')
    date_hierarchy='created'

# Register your models here.
admin.site.register(Post,PostAdmin)
admin.site.register(Comment,CommentAdmin)
admin.site.register(Like,LikeAdmin)
<file_sep>from django.http import HttpResponse
class AppMaintainance(object):
    def __init__(self,get_response):
        self.get_response=get_response

    def __call__(self,request):
        return HttpResponse('<h1>currently our application is under maintainance ..please try again sometime.!</h1>')

           
<file_sep># Generated by Django 2.1 on 2021-02-05 16:09

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('blog', '0003_like'),
    ]

    operations = [
        migrations.RenameField(
            model_name='like',
            old_name='name',
            new_name='uname',
        ),
    ]
