コード例 #1
0
ファイル: views.py プロジェクト: piotrpalkiewicz/poetry
def add_chapter(request, object_pk):
    form = ChapterForm(request.POST or None, request.FILES or None)
    literature = None
    try:
        literature = Literature.objects.get(id=object_pk)
    except Literature.DoesNotExists:
        raise Http404
    if request.user.is_authenticated():
        if not request.user == literature.author:
            raise Http404

    if request.method == "POST":
        if form.is_valid():
            ct = ContentType.objects.get_for_model(literature)
            chapter = Chapter(
                content_type=ct, object_id=literature.id, content=form.instance.content, title=form.instance.title
            )
            try:
                chapter.save()
            except:
                render_to_response(
                    "literatures/add_chapter.html",
                    {"form": form, "literature": literature},
                    context_instance=RequestContext(request),
                )

            return HttpResponseRedirect(reverse("literature_detail_view", args=(literature.id,)))

    return render_to_response(
        "literatures/add_chapter.html",
        {"form": form, "literature": literature},
        context_instance=RequestContext(request),
    )
コード例 #2
0
ファイル: views.py プロジェクト: piotrpalkiewicz/poetry
def add_chapter(request, object_pk):
    form = ChapterForm(request.POST or None, request.FILES or None)
    fanfiction = None
    try:
        fanfiction = Fanfiction.objects.get(id=object_pk)
    except:
        raise Http404
    if request.user.is_authenticated():
        if not request.user == fanfiction.author:
            raise Http404


    if request.method == 'POST':
        if form.is_valid():
            ct = ContentType.objects.get_for_model(fanfiction)
            chapter = Chapter(content_type=ct, object_id=fanfiction.id, content=form.instance.content,
                              title=form.instance.title)
            try:
                chapter.save()
            except:
                render_to_response('fanfictions/add_chapter.html', {'form': form, 'fanfiction': fanfiction},
                                   context_instance=RequestContext(request))

            return HttpResponseRedirect(reverse('fanfiction_detail_view', args=(fanfiction.id,)))

    return render_to_response('fanfictions/add_chapter.html', {'form': form, 'fanfiction': fanfiction},
                              context_instance=RequestContext(request))