繁体   English   中英

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

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

抱歉这么长的问题和我糟糕的英语。 我已经完成了 Python Crash Course,这是 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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM