parent
a5d55d1f6b
commit
cf693a8a33
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,6 +1,6 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
|
|
||||||
class PubBlogForm(forms.form):
|
class PubBlogForm(forms.Form):
|
||||||
title = forms.CharField(max_length=200, min_length=2)
|
title = forms.CharField(max_length=200, min_length=2)
|
||||||
content = forms.CharField(min_length=2)
|
content = forms.CharField(min_length=2)
|
||||||
category = forms.ImageField()
|
category = forms.IntegerField()
|
@ -1,30 +1,56 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render, redirect, reverse
|
||||||
from django.http import JsonResponse
|
from django.http.response import JsonResponse
|
||||||
from django.urls.base import reverse_lazy
|
from django.urls.base import reverse_lazy
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.views.decorators.http import require_http_methods
|
from django.views.decorators.http import require_http_methods, require_POST, require_GET
|
||||||
from .models import BlogCategory, Blog, BlogComment
|
from .models import BlogCategory, Blog, BlogComment
|
||||||
from .forms import PubBlogForm
|
from .forms import PubBlogForm
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
def index(request):
|
def index(request):
|
||||||
return render(request, 'index.html')
|
blogs = Blog.objects.all()
|
||||||
|
return render(request, 'index.html', context={'blogs': blogs})
|
||||||
|
|
||||||
def blog_detail(request, blog_id):
|
def blog_detail(request, blog_id):
|
||||||
return render(request,'blog_detail.html')
|
try:
|
||||||
|
blog = Blog.objects.get(pk=blog_id)
|
||||||
|
except Exception as e:
|
||||||
|
blog = None
|
||||||
|
|
||||||
|
return render(request,'blog_detail.html', context={'blog':blog})
|
||||||
|
|
||||||
@require_http_methods(['GET','POST'])
|
@require_http_methods(['GET', 'POST'])
|
||||||
@login_required()
|
@login_required()
|
||||||
def pub_blog(request):
|
def pub_blog(request):
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
categories = BlogCategory.objects.all()
|
categories = BlogCategory.objects.all()
|
||||||
return render(request,'pub_blog.html', context={"categories": categories})
|
return render(request, 'pub_blog.html', context={"categories": categories})
|
||||||
else:
|
else:
|
||||||
form = PubBlogForm(request.POST)
|
form = PubBlogForm(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
title = form.cleaned_data.get('title')
|
title = form.cleaned_data.get('title')
|
||||||
content = form.cleaned_data.get('content')
|
content = form.cleaned_data.get('content')
|
||||||
category_id = form.cleaned_data.get('category')
|
category_id = form.cleaned_data.get('category')
|
||||||
Blog.objects.create(title=title, content=content, category_id=category_id, author=request.user)
|
blog = Blog.objects.create(title=title, content=content, category_id=category_id, author=request.user)
|
||||||
return JsonResponse({"code":200, "message":"参数错误!"})
|
return JsonResponse({"code": 200, "message": "博客发布成功!", "data": {"blog_id": blog.id}})
|
||||||
|
else:
|
||||||
|
print(form.errors)
|
||||||
|
return JsonResponse({'code': 400, "message": "参数错误!"})
|
||||||
|
|
||||||
|
|
||||||
|
@require_POST
|
||||||
|
@login_required()
|
||||||
|
def pub_comment(request):
|
||||||
|
blog_id = request.POST.get('blog_id')
|
||||||
|
content = request.POST.get('content')
|
||||||
|
BlogComment.objects.create(content=content, blog_id=blog_id, author=request.user)
|
||||||
|
# 重新加载博客详情页
|
||||||
|
return redirect(reverse("blog:blog_detail", kwargs={"blog_id": blog_id}))
|
||||||
|
|
||||||
|
|
||||||
|
@require_GET
|
||||||
|
def search(request):
|
||||||
|
q = request.GET.get('q')
|
||||||
|
blogs = Blog.objects.filter(Q(title__icontains=q)|Q(content__icontains=q)).all()
|
||||||
|
return render(request, 'index.html', context={"blogs": blogs})
|
Loading…
Reference in new issue