1 from django.shortcuts import render 2 from bbs import models 3 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 4 5 from bbs import forms 6 # Create your views here. 7 8 9 def index(request): 10 11 12 categories = models.Category.objects.filter(set_as_top_menu=True) 13 return render(request,'index.html', {"categories":categories}) 14 15 16 def category(request,category_id): 17 categories = models.Category.objects.filter(set_as_top_menu=True) 18 19 20 articles = models.Article.objects.filter(category_id=category_id) 21 22 paginator = Paginator(articles, 5) # Show 25 contacts per page 23 24 page = request.GET.get('page') 25 try: 26 objs = paginator.page(page) 27 except PageNotAnInteger: 28 # If page is not an integer, deliver first page. 29 objs = paginator.page(1) 30 except EmptyPage: 31 # If page is out of range (e.g. 9999), deliver last page of results. 32 objs = paginator.page(paginator.num_pages) 33 34 return render(request,'index.html', {"categories":categories, "articles":objs}) 35 36 37 def article_detail(request,article_id): 38 39 article_obj = models.Article.objects.get(id=article_id) 40 41 42 return render(request,"article.html",{"article":article_obj}) 43 44 45 def new_article(request): 46 47 if request.method == "POST": 48 print("request post:",request.POST) 49 print("request files:",request.FILES,request.user ) 50 51 52 article_form = forms.ArticleForm(data=request.POST,files=request.FILES) 53 if article_form.is_valid(): 54 print("formdata", article_form.cleaned_data) 55 # article_form.cleaned_data['author_id'] = request.user.id 56 # # #article_form.save() 57 # # obj = models.Article(**article_form.cleaned_data) 58 # # obj.save() 59 else: 60 61 article_form = forms.ArticleForm() 62 63 return render(request,"new_article.html",{"form":article_form})
1 from django.shortcuts import render,HttpResponse,redirect 2 from bbs import models 3 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 4 from django.contrib.auth import authenticate,logout,login 5 from django.contrib.auth.decorators import login_required 6 7 from bbs import forms 8 # Create your views here. 9 10 11 def index(request): 12 return redirect("/category/all/") 13 14 def acc_logout(request): 15 16 logout(request) 17 18 return redirect("/account/login/") 19 20 def acc_auth(request): 21 errors = {} 22 if request.method == "POST": 23 username = request.POST.get("username") 24 password = request.POST.get("password") 25 user = authenticate(username=username,password=password) 26 print("auth res", user) 27 if user: 28 print("authenticate success") 29 login(request,user) 30 return redirect(request.GET.get("next") or "/") 31 else: 32 errors = {"error":"Wrong username or password!"} 33 34 return render(request,"login.html", errors) 35 36 37 def category(request,category_id): 38 categories = models.Category.objects.filter(set_as_top_menu=True) 39 40 if category_id == "all": 41 articles = models.Article.objects.all().order_by("-id") 42 else: 43 articles = models.Article.objects.filter(category_id=category_id).order_by("-id") 44 45 paginator = Paginator(articles, 5) # Show 25 contacts per page 46 47 page = request.GET.get('page') 48 try: 49 objs = paginator.page(page) 50 except PageNotAnInteger: 51 # If page is not an integer, deliver first page. 52 objs = paginator.page(1) 53 except EmptyPage: 54 # If page is out of range (e.g. 9999), deliver last page of results. 55 objs = paginator.page(paginator.num_pages) 56 57 return render(request,'index.html', {"categories":categories, "articles":objs}) 58 59 60 def article_detail(request,article_id): 61 62 article_obj = models.Article.objects.get(id=article_id) 63 64 65 return render(request,"article.html",{"article":article_obj}) 66 67 @login_required 68 def new_article(request): 69 70 if request.method == "POST": 71 print("request post:",request.POST) 72 print("request files:",request.FILES,request.user ) 73 74 75 article_form = forms.ArticleForm(data=request.POST,files=request.FILES) 76 if article_form.is_valid(): 77 print("formdata", article_form.cleaned_data) 78 article_form.cleaned_data['author_id'] = request.user.id 79 # # #article_form.save() 80 tags = article_form.cleaned_data.pop("tags") 81 obj = models.Article(**article_form.cleaned_data) 82 obj.save() 83 obj.tags.add(*tags) 84 85 obj.save() 86 87 return HttpResponse('''<h3><a href="/article/%s/">%s</a></h3>''' % (obj.id,obj.title) ) 88 89 else: 90 91 article_form = forms.ArticleForm() 92 93 return render(request,"new_article.html",{"form":article_form})
1 {% extends "index.html" %} 2 3 {% block extra-head-resources %} 4 5 <script src="/static/plugins/ckeditor/ckeditor.js"></script> 6 {% endblock %} 7 8 {% block container %} 9 10 <div style="min-height: 600px;padding-bottom: 50px"> 11 12 <form method="post" enctype="multipart/form-data"> 13 14 {% for field in form %} 15 <div class="form-group"> 16 <label class="col-sm-2 control-label">{{ field.name }}</label> 17 <div class="col-sm-10"> 18 {{ field }} 19 <span style="color: red">{{ field.errors }}</span> 20 </div> 21 </div> 22 23 {% endfor %} 24 <input type="submit" class="col-lg-offset-5 btn btn-sm btn-success" value="提交"> 25 </form> 26 </div> 27 28 29 <script> 30 // Replace the <textarea id="editor1"> with a CKEditor 31 // instance, using default configuration. 32 CKEDITOR.replace( 'id_body' ); 33 </script> 34 35 36 {% endblock %}