コード例 #1
0
def save(request):

    library_form = LibraryForm(request.POST or None)

    if library_form.is_valid():

        form_clean = library_form.cleaned_data
        pk = form_clean.get('id', None)

        if not pk:
            library_model = Library(**form_clean)
            library_model.save()
        else:
            library_model = get_object_or_404(Library, pk=pk)

            library_model.title = form_clean.get('title', '')
            library_model.description = form_clean.get('description', '')
            library_model.status = form_clean.get('status', '')
            library_model.friend_name = form_clean.get('friend_name', '')
            library_model.friend_email = form_clean.get('friend_email', '')

            library_model.save()

        return HttpResponseRedirect(reverse('index'))

    book_list = Library.objects.all()
    context = {
        'book_list': book_list,
        'library_form': library_form,
    }
    return render(request, 'library/index.html', context)
コード例 #2
0
def book_details(book_id):
    book = books.get(book_id - 1)
    form = LibraryForm(data=book)

    if request.method == "POST":
        if form.validate_on_submit():
            books.update(book_id - 1, form.data)
        return redirect(url_for("library_list"))
    return render_template("book.html", form=form, book_id=book_id)
コード例 #3
0
def library_list():
    form = LibraryForm()
    error = ""
    if request.method == "POST":
        if form.validate_on_submit():
            books.create(form.data)
            books.save_all()
        return redirect(url_for("library_list"))

    return render_template("library.html", form=form,  books=books.all(),error=error)
コード例 #4
0
def index(request):
    book_list = Library.objects.all()
    context = {
        'book_list': book_list,
        'library_form': LibraryForm(),
    }
    return render(request, 'library/index.html', context)
コード例 #5
0
def edit(request, book_id):

    book_list = Library.objects.all()
    book = model_to_dict(get_object_or_404(Library, pk=book_id))
    library_form = LibraryForm(initial=book)

    context = {
        'book_list': book_list,
        'library_form': library_form,
    }
    return render(request, 'library/index.html', context)
コード例 #6
0
def remove(request, book_id):

    book = get_object_or_404(Library, pk=book_id)
    book.delete()

    library_form = LibraryForm()
    book_list = Library.objects.all()

    context = {
        'book_list': book_list,
        'library_form': library_form,
    }

    return render(request, 'library/index.html', context)
コード例 #7
0
def library():
    form = LibraryForm()
    return render_template("library.html", form=form)