简体   繁体   中英

Convert Function Based View to Class Based View

I have a Django 2.1 views.py that I wanted to convert to Class Based View, I am NOT Django advanced user, and I know that I did translate the Functions the wrong way. The code was ment to serve a Bootstrap Modal Ajax form, and don't ask me to go see some solution already built out there, I tried everything, even the ones that worked fine in standalone, they are not compatible/conflict with my template (Maybe jquery or ..). I did convert List View and so far Create View, but I keep getting this Error :

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 "

Here's my views.py that I want to convert to CBV :

 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) 

I've been strugling since 9 days now. asking around, asked even the developper of that code ... no answer.. I really appreciate it, thank you guys

You can try this to change you view's to class based view's

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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