Ejemplo n.º 1
0
def new_comment(request):
    # saving the poted comment
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment()
            comment.book = form.cleaned_data.get("book")
            comment.chapter = form.cleaned_data.get("chapter")
            comment.example = form.cleaned_data.get("example")
            comment.page = form.cleaned_data.get("page")
            comment.title = form.cleaned_data.get("title")
            comment.body = form.cleaned_data.get("body")
            comment.email = form.cleaned_data.get("email", "Anonymous")
            comment.save()
            return HttpResponseRedirect(
                '/comments/get/?book={0}&chapter={1}&example={2}&page={3}'.format(
                    comment.book, comment.chapter, comment.example, comment.page
                )
            )
        else:
            book = request.POST.get('book', '')
            chapter = request.POST.get('chapter', '')
            example = request.POST.get('example', '')
            page = request.POST.get('page', '')
            return HttpResponseRedirect(
                '/comments/new/?book={0}&chapter={1}&example={2}&page={3}'.format(
                    book, chapter, example, page
                )
            )

    # retriving comment parameters
    book = request.GET.get('book', '')
    chapter = request.GET.get('chapter', '')
    example = request.GET.get('example', '')
    page = request.GET.get('page', '')
    initial_values = {
        'book': book,
        'chapter': chapter,
        'example': example,
        'page': page
    }
    form = CommentForm(initial = initial_values)
    context = {
        'form': form,
        'book': book,
        'chapter': chapter,
        'example': example,
        'page': page
    }
    context.update(csrf(request))
    return render(request, 'comments/new_comment.html', context)
Ejemplo n.º 2
0
def new_comment(request):
    # saving the poted comment
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment()
            comment.book = form.cleaned_data.get("book")
            comment.chapter = form.cleaned_data.get("chapter")
            comment.example = form.cleaned_data.get("example")
            comment.page = form.cleaned_data.get("page")
            comment.title = form.cleaned_data.get("title")
            comment.body = form.cleaned_data.get("body")
            comment.email = form.cleaned_data.get("email", "Anonymous")
            comment.save()
            return HttpResponseRedirect(
                '/comments/get/?book={0}&chapter={1}&example={2}&page={3}'.
                format(comment.book, comment.chapter, comment.example,
                       comment.page))
        else:
            book = request.POST.get('book', '')
            chapter = request.POST.get('chapter', '')
            example = request.POST.get('example', '')
            page = request.POST.get('page', '')
            return HttpResponseRedirect(
                '/comments/new/?book={0}&chapter={1}&example={2}&page={3}'.
                format(book, chapter, example, page))

    # retriving comment parameters
    book = request.GET.get('book', '')
    chapter = request.GET.get('chapter', '')
    example = request.GET.get('example', '')
    page = request.GET.get('page', '')
    initial_values = {
        'book': book,
        'chapter': chapter,
        'example': example,
        'page': page
    }
    form = CommentForm(initial=initial_values)
    context = {
        'form': form,
        'book': book,
        'chapter': chapter,
        'example': example,
        'page': page
    }
    context.update(csrf(request))
    return render(request, 'comments/new_comment.html', context)