コード例 #1
0
ファイル: views.py プロジェクト: Abrahanfer/trango-tut
def category(request, category_name_url):
     # Request our context from the request passed to us.
    #context = RequestContext(request)
    # NOT ANYMORE WITH RENDER

    # Change underscores in the category name to spaces.
    # URLs don't handle spaces well, so we encode them as underscores.
    # We can then simply replace the underscores with spaces again to
    # get the name.
    category_name = Category.decode(category_name_url)

    # Create a context dictionary which we can pass to the template
    # rendering engine. We start by containing the name of the
    # category passed by the user.
    cat_list = get_category_list()
    context_dict = {'category_name': category_name, 'cat_list':
                    cat_list}
    context_dict['category_name_url'] = category_name_url
    try:
        # Can we find a category with the given name?
        # If we can't, the .get() method raises a DoesNotExist
        # exception. So the .get() method returns one model instance
        # or raises an exception.
        category = Category.objects.get(name=category_name)

        # Retrieve all of the associated pages.
        # Note that filter returns >= 1 model instance.
        pages = Page.objects.filter(category=category)

        # Adds our results list to the template context under name
        # pages.
        context_dict['pages'] = pages

        # We also add the category object from the database to the
        # context dictionary. We'll use this in the template to verify
        # that the category exists.
        context_dict['category'] = category

        #Adding likes of category to context dictionary
        context_dict['category_likes'] = category.likes
    except Category.DoesNotExist:
        # We get here if we didn't find the specified category.
        # Don't do anything - the template displays the "no category"
        # message for us.
        return add_category(request)

    # Go render the response and return it to the client.
    return render(request, 'rango/category.html', context_dict)
コード例 #2
0
ファイル: views.py プロジェクト: Abrahanfer/trango-tut
def add_page(request, category_name_url):

    category_name = Category.decode(category_name_url)
    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            # This time we cannot commit straight away.
            # Not all fields are automatically populated!
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it.
            # Wrap the code in a try block - check if the category actually exists!
            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                # If we get here, the category does not exist.
                # Go back and render the add category form as a way of saying the category does not exist.
                return render(request, 'rango/add_category.html', {})

            # Also, create a default value for the number of views.
            page.views = 0

            # With this, we can then save our new model instance.
            page.save()

            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print(form.errors)
    else:
        form = PageForm()

    cat_list = get_category_list()
    return render(request, 'rango/add_page.html',
            {'category_name_url': category_name_url,
             'category_name': category_name, 'cat_list': cat_list,
             'form': form})