示例#1
0
def add_page(request,full_slug=None):
    parent_page=None
    slug=None

    if full_slug:
        slug=full_slug.split('/')

    # If there is a slug in REQUEST(ex.'trance')->we need to add a new_page to her parent_page.
    # So get a parent page.
    if slug:
        if len(slug)>1:
            parent_page=get_object_or_404(WebPage,slug=slug[-1])
        elif len(slug)==1:
            parent_page=get_object_or_404(WebPage,slug=slug[0])

    # Create a new_page
    if request.method=='POST':
        form=WebPageForm(request.POST)
        if form.is_valid():
            new_page=form.save(commit=False)
            if parent_page:# If we trying to add a child page->we need to add her parent
                new_page.parent=parent_page
            new_page.save()
            return HttpResponseRedirect(new_page.get_absolute_url())

    # Return an unbounded form
    else:
        form=WebPageForm()

    return render_to_response('add_page.html',{'form':form,},context_instance=RequestContext(request))
示例#2
0
def edit_page(request,full_slug):
    slugs=full_slug.split('/')
    page=None

    if len(slugs)>1:
        page=get_object_or_404(WebPage,slug=slugs[-1])
    elif len(slugs)==1:
        page=get_object_or_404(WebPage,slug=slugs[0])

    if not page.get_absolute_url().strip('/') == full_slug:
        raise Http404

    # Send POST data for update an existing page.Update a page.
    if request.method=='POST':
        form=WebPageForm(request.POST, instance=page)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(page.get_absolute_url())

    # Render a form to edit data for existing page
    else:
        form=WebPageForm(instance=page)

    return render_to_response('edit_page.html',{'form':form,},context_instance=RequestContext(request))