from django.shortcuts import render, redirect
from django.http import HttpResponse
from rango.models import Category, Page 
from rango.forms import CategoryForm, PageForm

def index(request):
    categories = Category.objects.order_by('-name')[:5]
    pages = Page.objects.order_by('-views')[:5]
    # Construct a dictionary to pass to the template engine as its context.
    # Note the key boldmessage is the same as {{ boldmessage }} in the template!
    context_dict = {'categories': categories, 'pages': pages }

    # Return a rendered response to send to the client.
    # We make use of the shortcut function to make our lives easier.
    # Note that the first parameter is the template we wish to use.

    return render(request, "rango/index.html", context_dict)

def about(request):
    return render(request, "rango/about.html")


def contact(request):
    context_dict = {'email': "myemail@email.com", 'username': "ronaldWasHere", 'age': 23}

    return render(request, "rango/contact.html", context_dict)

def category(request, category_name_url):
    context_dict = {}

    try:
        # If we can't find category, the .get() method raises DoesNotExist exception.
        # So the .get() method returns one model instance or raises an exception.
        category = Category.objects.get(slug = category_name_url)
        context_dict['category'] = category
        context_dict['category_name'] = category.name
        context_dict['category_slug'] = category_name_url

        context_dict['pages'] = Page.objects.filter(category=category)
    except Category.DoesNotExist:
        pass

    return render(request, "rango/category.html", context_dict)

# below process both GET and POST requests
def add_category(request):
    # a POST request?
    if request.method == 'POST':
        form = CategoryForm(request.POST)

        if form.is_valid():
            form.save(commit = True)
            return index(request)
        else:
            print(form.errors)
    else:
        form = CategoryForm()

    return render(request, "rango/add_category.html", {'form': form})

def page(request, page_name_url):
    context_dict = {}

    try:
        # If we can't find category, the .get() method raises DoesNotExist exception.
        # So the .get() method returns one model instance or raises an exception.
        page = Page.objects.get(slug = page_name_url)
        context_dict['page'] = page
        context_dict['page_title'] = page.title

    except Page.DoesNotExist:
        pass

    return render(request, "rango/page.html", context_dict)

def add_page(request, category_name_url):
    
    try: 
        category = Category.objects.get(slug = category_name_url)
    except Category.DoesNotExist:
                category = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if category:
                page = form.save(commit=False)
                page.category = category
                page.views = 0
                page.save()

                return redirect(category)
                # return redirect('/rango/category/'+category.slug+'/')

        else:
            print(form.errors)
    else:
        form = PageForm()

    context_dict = {'form': form, 'category': category}

    return render(request, "rango/add_page.html", context_dict)

@@ -163,16 +163,43 @@
 PageForm
+, UserForm, UserProfileForm
 %0A%0Adef in
@@ -1035,24 +1035,1013 @@
 text_dict)%0A%0A
+def register(request):%0A    # set to true if a success%0A    registered = False%0A%0A    if request.method == 'POST':%0A        user_form = UserForm(data=request.POST)%0A        profile_form = UserProfileForm(data=request.POST)%0A%0A        if user_form.is_valid() and profile_form.is_valid():%0A            user.set_password(user.password)%0A            user.save()%0A%0A            # create unpersisted UserProfile for linking to User%0A            profile = profile_form.save(commit=False)%0A            profile.user = user%0A%0A            if 'picture' in request.FILES:%0A                profile.picture = request.FILES%5B'picture'%5D%0A%0A            profile.save()%0A%0A            # switch to True after success%0A            registered = True%0A%0A        else:%0A            print(user_form.errors, profile_form.errors)%0A%0A    else: %0A        user_form = UserForm()%0A        profile_form = UserProfileForm()%0A%0A    render(request, 'rango/register.html', %7B'user_form': user_form, 'profile_form': profile_form, 'registered': registered%7D)%0A%0A%0A
 def category
@@ -4338,29 +4338,28 @@
 dd_page.html%22, context_dict)
-%0A

Add registration view