post_update.html
{% extends 'dota2/wrapper.html' %} {% block title %} Edit - {{ post.title }} {% endblock %} {% block content %} <form action="{{ post.get_update_url}}" method="post"> {% csrf_token %} {% for field in form %} <div class='form-group'> {% if field.errors %} <div class='alert alert-danger'> {{ field.errors }} </div> {% endif %} {{ field.label }} {{ field }} </div> {% endfor %} <button type="submit" class='btn btn-primary' >Update Post</button> </form> {% endblock %}
urls.py
from django.urls import path, include from .models import Post from .views import * urlpatterns = [ path('', posts_list, name='post_list_url'), path('post/<str:slug>/', PostDetail.as_view(), name="post_detail_url"), path('post/<str:slug>/update/', PostUpdate.as_view(), name="post_update_url"), path('create/', PostCreate.as_view(), name='post_create_url'), ]
models.py
from django.db import models from django.shortcuts import reverse from django.utils.text import slugify from time import time def gen_slug(s): new_slug = slugify(s, allow_unicode=True) return new_slug + '-' + str(int(time())) class Post(models.Model): title = models.CharField(max_length=120, db_index=True) slug = models.SlugField(max_length=150, unique=True) body = models.TextField(blank=True, db_index=True) date = models.DateTimeField(auto_now_add=True) #images = models.ImageField(upload_to='main/static/main/accsimages/', blank=True, verbose_name='подпись') #images = models.ImageField(upload_to='main/static/main/accsimages/', blank=True, height_field=None, width_field=None, max_length=100) def get_absolute_url(self): return reverse('post_detail_url', kwargs={'slug': self.slug}) def get_update_url(self): return reverse('post_update_url', kwargs={'slug': self.slug}) def save(self, *args, **kwargs): if not self.id: self.slug = gen_slug(self.title) super().save(*args, **kwargs) def __str__(self): return self.title
utils.py
from django.shortcuts import render from django.shortcuts import redirect from django.shortcuts import get_object_or_404 from django.views.generic import ListView, CreateView, UpdateView from .models import * class ObjectDetailMixin: model = None template = None def get(self, request, slug): #post = Post.objects.get(slug__iexact=slug) obj = get_object_or_404(self.model, slug__iexact=slug) return render(request, self.template, context={self.model.__name__.lower():obj}) class ObjectCreateMixin: model_form = None template = None def get(self, request): form = self.model_form() return render(request, self.template,context={'form': form}) def post(self, request): bound_form = self.model_form(request.POST) if bound_form.is_valid(): new_obj = bound_form.save() return redirect(new_obj) return render(request, self.template, context={'form': bound_form}) class ObjectUpdateMixin: model = None model_form = None template = None def get(self, request, slug): obj = self.model.objects.get(slug__iexact=slug) bound_form = self.model_form(instance=obj) return render(request, self.template, context={'form': bound_form, self.model.__name__.lower(): obj}) def post(self, request, slug): obj = self.model.objects.get(slug__iexact=slug) bound_form = self.model_form(request.POST, instance=obj) if bound_form.is_valid(): new_obj = bound_form.save() return redirect(new_obj) return render(request, self.template, context={'form': bound_form, self.model.__name__.lower(): obj})
views.py
from django.shortcuts import render from django.shortcuts import redirect from django.shortcuts import get_object_or_404 from django.views.generic import ListView, CreateView, UpdateView, View from .models import Post from .forms import PostForm from .utils import * def posts_list(request): posts = Post.objects.all() return render(request, 'dota2/posts.html', context={'posts': posts}) class PostCreate(View): def get(self, request): form = PostForm() return render(request, 'dota2/post_create.html', context={'form':form}) def post(self, request): bound_form = PostForm(request.POST) if bound_form.is_valid(): new_post = bound_form.save() return redirect(new_post) return render(request, 'dota2/post_create.html', context={'form', bound_form}) class PostDetail(ObjectDetailMixin, View): model = Post template = 'dota2/post_detail.html' # def get(self, request, slug): # post = get_object_or_404(Post, slug__iexact=slug) # return render(request, 'dota2/post_detail.html', context={'post':post}) class PostUpdate(ObjectUpdateMixin, View): model = Post model_form = PostForm template = 'dota2/post_update.html'