将基于函数的视图转换为基于类的视图

时间:2018-08-16 08:29:25

标签: python django

我有一个Django 2.1 views.py,我想转换为基于类的视图,我不是Django高级用户,而且我知道我确实以错误的方式转换了Function。代码只是为了提供Bootstrap Modal Ajax表单而写的,不要让我去看看那里已经建立了一些解决方案,我尝试了所有方法,甚至是那些在独立环境下都能正常工作的方法,它们与我的模板不兼容/冲突(也许是jquery或..)。我确实转换了列表视图,到目前为止却没有创建视图,但是我一直收到此错误:

UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext. "A {% csrf_token %} was used in a template, but the context "

这是我要转换为CBV的views.py:

from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from django.template.loader import render_to_string
from .models import Book
from .forms import BookForm
from django.views.generic import TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView


#The Old function List view
#def book_list(request):
#    books = Book.objects.all()
#    return render(request, 'books/book_list.html', {'books': books})


#The New Class List view
class Book_list(ListView):
    template_name = 'books/book_list.html'
    model = Book

def save_book_form(request, form, template_name):
    data = dict()
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            data['form_is_valid'] = True
            books = Book.objects.all()
            data['html_book_list'] = render_to_string('books/includes/partial_book_list.html', {
                'books': books
            })
        else:
            data['form_is_valid'] = False
    context = {'form': form}
    data['html_form'] = render_to_string(template_name, context, request=request)
    return JsonResponse(data)


def book_create(request):
    if request.method == 'POST':
        form = BookForm(request.POST)
    else:
        form = BookForm()
    return save_book_form(request, form, 'books/includes/partial_book_create.html')



def book_update(request, pk):
    book = get_object_or_404(Book, pk=pk)
    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
    else:
        form = BookForm(instance=book)
    return save_book_form(request, form, 'books/includes/partial_book_update.html')


def book_delete(request, pk):
    book = get_object_or_404(Book, pk=pk)
    data = dict()
    if request.method == 'POST':
        book.delete()
        data['form_is_valid'] = True
        books = Book.objects.all()
        data['html_book_list'] = render_to_string('books/includes/partial_book_list.html', {
            'books': books
        })
    else:
        context = {'book': book}
        data['html_form'] = render_to_string('books/includes/partial_book_delete.html', context, request=request)
    return JsonResponse(data)

from django.shortcuts import render, get_object_or_404 from django.http import JsonResponse from django.template.loader import render_to_string from .models import Book from .forms import BookForm from django.views.generic import TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView #The Old function List view #def book_list(request): # books = Book.objects.all() # return render(request, 'books/book_list.html', {'books': books}) #The New Class List view class Book_list(ListView): template_name = 'books/book_list.html' model = Book def save_book_form(request, form, template_name): data = dict() if request.method == 'POST': if form.is_valid(): form.save() data['form_is_valid'] = True books = Book.objects.all() data['html_book_list'] = render_to_string('books/includes/partial_book_list.html', { 'books': books }) else: data['form_is_valid'] = False context = {'form': form} data['html_form'] = render_to_string(template_name, context, request=request) return JsonResponse(data) def book_create(request): if request.method == 'POST': form = BookForm(request.POST) else: form = BookForm() return save_book_form(request, form, 'books/includes/partial_book_create.html') def book_update(request, pk): book = get_object_or_404(Book, pk=pk) if request.method == 'POST': form = BookForm(request.POST, instance=book) else: form = BookForm(instance=book) return save_book_form(request, form, 'books/includes/partial_book_update.html') def book_delete(request, pk): book = get_object_or_404(Book, pk=pk) data = dict() if request.method == 'POST': book.delete() data['form_is_valid'] = True books = Book.objects.all() data['html_book_list'] = render_to_string('books/includes/partial_book_list.html', { 'books': books }) else: context = {'book': book} data['html_form'] = render_to_string('books/includes/partial_book_delete.html', context, request=request) return JsonResponse(data)

自9天以来,我一直在努力。到处问,甚至问那个代码的开发者..没有答案.. 非常感谢,谢谢大家

1 个答案:

答案 0 :(得分:0)

您可以尝试将此视图更改为基于类的视图

class BookList(ListView):
    template_name = 'books/book_list.html'
    model = Book


class BookCreate(CreateView):
    template_name = "books/includes/partial_book_list.html"
    model = Book
    form_class = BookForm
    success_url = 'success' #name of url you want to redirect to


class BookUpdate(UpdateView):
    model = Book
    form_class = BookForm
    fields = ['name',] #field's you want to update
    template_name = 'books/includes/partial_book_update.html'
    success_url = 'success' #name of url you want to redirect to


class BookDelete(DeleteView):
    model = Book
    template_name = 'books/includes/partial_book_delete.html'
    success_url = 'success'