django 是否有更好的变体在我的项目中使用 CBV 方法?

django is there better variant to use CBV approach in my project?

抱歉这么长的问题和我糟糕的英语。我已经完成 Python 速成课程,这是 Eric Matthes 编写的入门编程书籍。之后决定继续研究Django,发现CBV方法更适合建站。我是从函数式写的书上通过CBV训练程序重写的,但是看了官方文档还是对CBV的方法有点迷茫。 有人能告诉我,我的 CBV 变体中有很多硬编码吗?还有可能做得更好吗?

每个变体都可以正常工作。

这里是带有注释的书中观点的变体,我插入了注释以了解代码的作用:

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import Http404
from .models import Topic, Entry
from .forms import TopicForm, EntryForm

# Create your views here.

def index(request):
    """Home page of Learning Log."""
    return render(request, 'learning_logs/index.html')

def more_inf(request):
    return render(request,'learning_logs/more_inf.html')

def topics(request):
    """List of topics"""
    public_topics = Topic.objects.filter(public=True).order_by('date_added')
    if request.user.is_authenticated:
        private_topics = Topic.objects.filter(owner=request.user).order_by('date_added')
        topics = public_topics | private_topics
    else:
        topics = public_topics
    
    context = {'topics': topics}
    return render(request, 'learning_logs/topics.html', context)

@login_required
def topic(request, topic_id):
    """Show one topic with details"""
    topic = get_object_or_404(Topic, id=topic_id)
    #Проверка того, что тема принадлежит текущему пользователю
    check_topic_owner(topic.owner, request)

    entries = topic.entry_set.order_by('-date_added')
    context = {'topic': topic, 'entries': entries}
    return render(request, 'learning_logs/topic.html', context)

@login_required
def new_topic(request):
    """Create new topic"""
    if request.method != 'POST':
        #data wasn't sent;create empty form
        form = TopicForm()
    else:
        #POST data sent; to process the data.
        form = TopicForm(data=request.POST)
        if form.is_valid():
            new_topic = form.save(commit=False)
            new_topic.owner = request.user
            new_topic.save()
            return redirect('learning_logs:topics')

    #Show empty or invalid form.
    context = {'form': form}
    return render(request, 'learning_logs/new_topic.html', context)

@login_required
def new_entry(request, topic_id):
    """Add new entry to the topic"""
    topic = get_object_or_404(Topic, id=topic_id)
    check_topic_owner(topic.owner, request)
    if request.method != 'POST':
        #data wasn't sent;create empty form
        form = EntryForm()
    else:
        #POST data sent; to process the data.
        form = EntryForm(data=request.POST)
        if form.is_valid():
            new_entry = form.save(commit=False)
            new_entry.topic = topic
            new_entry.save()
            return redirect('learning_logs:topic', topic_id=topic_id)

    #Show empty or invalid form.
    context = {'topic': topic, 'form': form}
    return render(request, 'learning_logs/new_entry.html', context)

@login_required
def edit_entry(request, entry_id):
    """Edit the current entry"""
    entry = get_object_or_404(Entry, id=entry_id)
    topic = entry.topic
    check_topic_owner(topic.owner, request)

    if request.method !='POST':
        #initial request; form was created by current data entries
        form = EntryForm(instance=entry)
    else:
        #POST data sent; to process the data.
        form = EntryForm(instance=entry, data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('learning_logs:topic', topic_id=topic.id)

    context = {'entry': entry, 'topic': topic, 'form': form}
    return render(request, 'learning_logs/edit_entry.html', context)

def check_topic_owner(owner, request):
    if owner != request.user:
        raise Http404

这是我的新 CBV 变体,也添加了 slug 和 absolute_url: 我可以在那里改进什么?提前谢谢你

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import Http404
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from .models import Topic, Entry
from .forms import TopicForm, EntryForm

# Create your views here.

def index(request):
    """Домашняя страница приложения Learning Log."""
    return render(request, 'learning_logs/index.html')

def more_inf(request):
    return render(request,'learning_logs/more_inf.html')

class TopicsHome(ListView):
    model = Topic
    template_name = 'learning_logs/topics.html'
    context_object_name = 'topics'
    
    def get_queryset(self):
        public_topics = Topic.objects.filter(public=True).order_by('date_added')
        if self.request.user.is_authenticated:
            private_topics = Topic.objects.filter(owner=self.request.user).order_by('date_added')
            topics = public_topics | private_topics
        else:
            topics = public_topics
        return topics


class ShowTopic(LoginRequiredMixin, DetailView):
    model = Topic
    template_name = 'learning_logs/topic.html'
    context_object_name = 'topic'
    slug_url_kwarg = 'topic_slug'
  
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        topic = get_object_or_404(Topic, slug=self.kwargs['topic_slug'])
        check_topic_owner(topic.owner, self.request)
        context['entries'] = topic.entry_set.order_by('-date_added')
        return context


class AddTopic(LoginRequiredMixin, CreateView):  
    form_class = TopicForm
    template_name = 'learning_logs/new_topic.html'  

    def form_valid(self, form):
        new_topic = form.save(commit=False)
        new_topic.owner = self.request.user
        new_topic.save()
        return redirect('learning_logs:topics')
    
class AddEntry(LoginRequiredMixin, CreateView):
    form_class = EntryForm
    template_name = 'learning_logs/new_entry.html'
    slug_url_kwarg = 'topic_slug'

    def form_valid(self, form):
        topic = get_object_or_404(Topic, slug=self.kwargs['topic_slug'])
        check_topic_owner(topic.owner, self.request)
        new_entry = form.save(commit=False)
        new_entry.topic = topic
        new_entry.save()
        return redirect('learning_logs:topic', topic_slug=topic.slug)


class EditEntry(LoginRequiredMixin, UpdateView):
    model = Entry
    form_class = EntryForm
    template_name = 'learning_logs/edit_entry.html'
    context_object_name = 'topic'
    slug_url_kwarg = 'entry_slug'

    def form_valid(self, form):
        entry = get_object_or_404(Entry, slug=self.kwargs['entry_slug'])
        topic = entry.topic
        check_topic_owner(topic.owner, self.request)
        form.save()
        return redirect('learning_logs:topic', topic_slug=topic.slug)


def check_topic_owner(owner, request):
    if owner != request.user:
        raise Http404

基于函数的视图 (FBV) 和 Class 基于视图 (CBV) 都在 Django 项目中占有一席之地。它通常归结为开发人员的偏好。就个人而言,我更喜欢 FBV,因为它的抽象性较低。虽然走CBV路线也没有错!

如果您想了解更多关于 FBV 的信息以及为什么人们会喜欢它,请查看本系列文章:https://spookylukey.github.io/django-views-the-right-way/