Ejemplo n.º 1
0
def add_page(request, cat_name_slug):

    try:
        cat = Category.objects.get(slug=cat_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():

            if form.is_valid():
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()

                return category(request, cat_name_slug)

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

    return render(request, 'rango/add_page.html', {'form': form, 'category': cat})
Ejemplo n.º 2
0
def add_page(request, category_name_slug):

    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
                cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.date = timezone.now()
                page.save()
                # вероятно здесь лучше использовать redirect.
                return category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()

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

    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 3
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    category_name = decode_url(category_name_url)

    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():

            page = form.save(commit=False)

            cat = Category.objects.get(name=category_name)
            page.category = cat

            page.views = 0

            page.save()
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    return render_to_response('rango/add_page.html', {'category_name_url': category_name_url, 
        'category_name': category_name, 'form': form, 'cat_list': get_category_list() }, context)
Ejemplo n.º 4
0
def add_page(request, category_name_url):
    context = RequestContext(request)
    category_name = decode_category(category_name_url)
    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            page = form.save(commit=False)
            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                # If we get here, the category does not exist.
                # We render the add_page.html template without a context dictionary.
                # This will trigger the red text to appear in the template!
                return render_to_response('rango/add_page.html', {}, context)
            # 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()

    return render_to_response( 'rango/add_page.html',
            {'category_name_url': category_name_url,
             'category_name': category_name, 'form': form},
             context)
Ejemplo n.º 5
0
def add_pages(request, category_name_slug):
    category = Category.objects.get(slug=category_name_slug)
    category_list = {}
    category_list['category_name'] = category.name
    try:
        cat = Category.objects.get(slug=category_name_slug)

    except Category.DoesNotExist:
        cat = none

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                Page = form.save(commit=False)
                Page.category = cat
                Page.views = 0
                Page.save()
                return category(request, category_name_slug)
            else:
                print form.errors
    else:
        form = PageForm()

    context_dict = {'form': form, 'categories': cat}

    return render(request, 'rango/add_pages.html', context_dict)
Ejemplo n.º 6
0
def add_page(request, category_name_slug):
    
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None
        
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                return category(request, category_name_slug)
                #rerurn redirect(reverse('rango:category', args=('category_name_slug',)))
            else:
                print form.errors
        else:
            form = PageForm()
            
        context_dict = {'form':form, 'category':cat, 'category_name_slug':category_name_slug}
        
        return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 7
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    category_name = decode_url(category_name_url)

    if request.method == 'POST':
        form = PageForm(request.POST)

        if form is not None and form.is_valid():
            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                # category doesn't exist
                return render_to_response(
                    'rango/add_category.html', {}, context)

            # default value for no. of views
            page.views = 0

            page.save()

            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    return render_to_response(
        'rango/add_page.html',
        {'category_name_url': category_name_url,
         'category_name': category_name, 'form': form},
        context)
Ejemplo n.º 8
0
Archivo: views.py Proyecto: 400yk/tango
def add_page(request, category_name_url):
    context = RequestContext(request)
    category_name = decoding(category_name_url)

    if  request.method == "POST":
        form = PageForm(request.POST)

        if form.is_valid():
            # This is we don't commit here since not all fields are filled automatically
            page = form.save(commit = False)

            cat = Category.objects.get(name = category_name)
            page.category = cat
            page.views = 0
            page.save() # Without the commit = True, need to manually save the page
            return category(request, category_name_url)

        else: 
            print form.errors

    else:
        form = PageForm()

    return render_to_response('rango/add_page.html', {
        'category_name_url' : category_name_url,
        'category_name' : category_name,
        'form' : form,
        'categories': get_categories()
        }, context)
Ejemplo n.º 9
0
def add_page(request, category_name_slug=None):
    print 'add_page', request, category_name_slug
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None
    #print 'cat', cat, '--cat.id', cat.id
    if request.method == 'POST':
        form = PageForm(request.POST)
        print 'form-- POST', form
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                #return category(request, category_name_slug)
                return HttpResponseRedirect(reverse('rango:category', args=(cat.slug,)))
            else:
                 form.save(commit=True)
                 return index(request)
        else:
            print 'error' ,form.errors
    
    else:
        print 'GET'
        if cat:
            form = PageForm(initial={'category': cat.id})
            #form.fields['category'].initial = cat.id
        else:
            form = PageForm()
    
    context_dict = {'form': form, 'category': cat}
    url = 'rango/add_page.html'
    return render(request, url , context_dict)
Ejemplo n.º 10
0
def add_page(request, category_name_slug):
    categor = 9
    print "this is working"
    try:
        categor = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        categor = None
            
    if request.method =='POST':
        form = PageForm(request.POST)

        if form.is_valid():
            if categor:
                page =form.save(commit=False)
                page.category= categor
                page.views=0
                page.save()
                #return index(request)
                return category(request, category_name_slug)
        else:
                print form.errors
    else:
        form=PageForm()
    context_dictionary ={}
    context_dictionary['category_slug']= category_name_slug
    context_dictionary['form'] = form
    return render(request, 'rango/add_page.html', context_dictionary) 
Ejemplo n.º 11
0
def add_pages(request, category_name_url):
    context = RequestContext(request)

    category_name = decode_url(category_name_url)
    if request.method == "POST":
        form = PageForm(request.POST)

        if form.is_valid():
            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                return render_to_response('rango/add_category.html', {}, context)

            page.views = 0

            page.save()

            return category(request, category_name_url)

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

    return render_to_response("rango/add_page.html", {'form': form, 'category_name_url': category_name_url,
                                                      'category_name':category_name}, context)
Ejemplo n.º 12
0
def add_page(request, category_name_url):
	context = RequestContext(request)
	context_dict = {'cat_list': get_category_list()}
	category_name = decode_url(category_name_url)
	if request.method == 'POST':
		form = PageForm(request.POST)
		if form.is_valid():
			page = form.save(commit=False)
			
			try:
				cat = Category.objects.get(name=category_name)
				page.category = cat
			except Category.DoesNotExist:
				return render_to_response('rango/add_category.html', {}, context)
				
			page.views = 0
			page.save()
			return HttpResponseRedirect('/rango/category/' + category_name_url)
		else:
			context_dict['errors'] = form.errors
			print form.errors
	else:
		form = PageForm()
	
	context_dict['form'] = form
	context_dict['category_name_url'] = category_name_url
	if request.user.is_authenticated:
		context_dict['user'] = request.user
	return render_to_response('rango/add_page.html', context_dict, context)
Ejemplo n.º 13
0
def add_page(request, category_name_slug):

    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                # probably better to use a redirect here.
                return category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()

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

    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 14
0
def add_page(request, category_name_slug):
    #import pudb; pudb.set_trace()
    try:
        cat= Category.objects.get(slug = category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit= False)
                page.category= cat
                page.views=0
                #page.first_visit= datetime.now()
                #page.last_visit = datetime.now()
                page.save()
                return category(request,category_name_slug)
        else:
            print form.errors
    else:
        form= PageForm()

    context_dict = {'form':form, 'category':cat, 'catSlug':category_name_slug}

    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 15
0
def add_page(request, category_name_slug):
    # A HTTP POST?
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method =='POST':
        form = PageForm(request.POST)
        # Is the Form Valid?
        if form.is_valid():
            # Save the new page to the DB
            if cat:
                page = form.save(commit=False)
                page.category = cat
                # page.views = 0 from original, but doesn't seem necessary
                page.save()
            # Return user to category page; Tango with Django suggests a redirect
            # would be better here but does not show how to implement
            return category(request, category_name_slug)
        else:
            # Show any errors if the form is not valid
            print form.errors
    else:
        # If the request was a GET, render using the add_page template
        form = PageForm()

    # Need to pass category slug directly in the event that the cat does not exist,
    # but rango/category/<category_name_slug>/add_page has been navigated to
    context_dict = {'form': form, 'category': cat, 'category_slug': category_name_slug}
    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 16
0
def add_page (request, category_name_url):
	context = RequestContext(request)
	category_name = decode_url(category_name_url)
	if request.method == 'POST':
            if not request.user.is_authenticated():
                return HttpResponseRedirect('/rango/category/' + category_name_url)
            form = PageForm(request.POST)
            if form.is_valid():
                    page = form.save(commit=False)

                    try:
                            cat = Category.objects.get(name=category_name)
                            page.category = cat
                    except Category.DoesNotExist:
                            return render_to_response('rango/add_category.html', {}, context)

                    page.views = 0
                    page.save()
                    return category(request, category_name_url)
            else:
                    print form.errors
	else:
		form = PageForm()
	return render_to_response('rango/add_page.html',
			{'category_name_url' : category_name_url,
                            'category_name' : category_name, 'form' : form, 'categories' : load_categories()},
			context)
Ejemplo n.º 17
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    cat_list = get_category_list()

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

        if form.is_valid():
            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                return render_to_response('rango/add_category.html',{},context)

            # 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()

    return render_to_response('rango/add_page.html', {'category_name_url': category_name_url,'category_name': \
        category_name, 'form': form, 'cat_list': cat_list}, context)
Ejemplo n.º 18
0
def add_page(request, category_name_slug):
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        print "1!"
        form = PageForm(request.POST)

        if form.is_valid():
            print "2!"
            if cat:
                print "3!"
                page = form.save(commit=False)
                page.category = cat
                page.first_visit = timezone.now()
                page.last_visit = timezone.now()
                page.views = 0
                page.save()
                return redirect('/rango/category/' + category_name_slug)
        else:
            print "4!"
            print form.errors
    else:
        print "5!"
        form = PageForm()

    print "6!"
    context_dict = { 'category': cat, 'category_name_slug': category_name_slug, 'form': form }
    return render(request, 'rango/add_page.html', context_dict )
Ejemplo n.º 19
0
def add_page(request, category_name_slug):

    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.first_visit = timezone.now()
                page.save()
                # Redirect to appropriate category page.
                return category(request, category_name_slug)
        else:
            print(form.errors)  # pragma: no cover
    else:
        form = PageForm()

    context_dict = {'form': form,
                    'category': cat,
                    'cat_name_slug': category_name_slug}

    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 20
0
def add_page(request, category_name_slug):

	try:
		cat = Category.objects.get(slug=category_name_slug)
	except Category.DoesNotExist:
		cat = None
	
	if request.method == 'POST':		
		form = PageForm(request.POST)
		if form.is_valid():
			if cat:				
				page = form.save(commit=False)
				page.category = cat
				page.views = 0
				page.first_visit = timezone.now()
				page.last_visit = timezone.now()
				page.save()
				# probably better to use a redirect here.
				return HttpResponseRedirect(reverse('rango:category', kwargs={'category_name_slug': category_name_slug}))
				
		else:
			print form.errors
	else:
		form = PageForm()
		
		context_dict = {'form':form, 'category':cat, 'category_name_slug':category_name_slug}
	
		return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 21
0
def add_page(request, category_name_url):
    context = RequestContext(request)

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

        if form.is_valid():
            # This time we can't commit right away
            # We need to fill associated category manually.
            page = form.save(commit=False)
            # Retrieving Category object so we can add it
            cat = Category.objects.get(name=category_name)
            page.category = cat
            # Also we need to add the default number of views
            page.views = 0
            page.save()

            return category(request, category_name_url)
        else:
            print(form.errors)
    else:
        form = PageForm()
    category_list = get_category_list()
    context_dict = {'category_name_url': category_name_url, 'category_name': category_name,
                    'form': form, 'categories': category_list,}
    return render_to_response('rango/add_page.html', context_dict, context)
Ejemplo n.º 22
0
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()
                # Use a redirect here now that the page is saved, display the category.
                return category(request, category_name_url)
        else:
            print(form.errors)
    else:
        # If the request was not a POST (on submit), display the form to enter details.
        form = PageForm()

    context_dict = {
        'form': form,
        'category': category,
        'category_name': category.name
    }

    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 23
0
def add_page(request, category_name_slug):
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == "POST":
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                # print 'ok...page before category.html', page
                # Problem: didn't redirect to category.
                return category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {"form": form, "category": cat}
    # print 'ok...page before add_page.html'
    return render(request, "rango/add_page.html", context_dict)
Ejemplo n.º 24
0
def add_page(request,category_name_slug):
    print "6"
    try:
        cat=Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None
    print "7"
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                print "8"
                page = form.save(commit= False)
                page.category = cat
                page.views =0 
                page.save()
                print "9"
                return category(request,category_name_slug)
        else: print form.errors
    else: form = PageForm()
    print "10"
    context_dict={'form':form,'category':cat}
    print "11"
    return render(request,'add_page.html',context_dict)
    print "12"            
Ejemplo n.º 25
0
def add_page(request, category_name_slug):

    context = RequestContext(request)
    # category_name = category_name_slug.replace('_', ' ')

    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.
            cat = Category.objects.get(slug=category_name_slug)
            page.category = cat

            # 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_slug)
        else:
            print form.errors
    else:
        form = PageForm()

    return render_to_response('rango/add_page.html', {'category_name_slug': category_name_slug,
                                                      'form': form}, context)
Ejemplo n.º 26
0
def add_page(request,category_name_url):
    context = RequestContext(request)
    cat= decode_url(category_name_url)
    if request.method == "POST":
        form = PageForm(request.POST)
        if form.is_valid():
            #Not commiting the changes directly because we have to check wheter the category exists or not
            page = form.save(commit=False)

            try:
                #Searching the database for a category that matches the params.
                category_name = Category.objects.get(name=cat)
                page.category = category_name
            except Category.DoesNotExist:
                #Showing the New Category Page so that the User can add a new category
                return render_to_response('rango/add_category.html', {}, context)

            page.views = 0
            page.save()

            #Now as the page is added, display the category so that the user can view the page
            return category(request,category_name_url)

        else:
            print form.errors
    else:
        form = PageForm()
        return render_to_response('rango/add_page.html', {"category" : cat, "category_name_url" : category_name_url , "form" : form}, context)
Ejemplo n.º 27
0
def add_page(request, category_name_url):
    context = RequestContext(request)
    category_name = category_name_url.replace('_', ' ')
    cat_list = get_category_list()
    
    if request.method == 'POST':
        form = PageForm(request.POST)
        
        if form.is_valid():
            page = form.save(commit=False)
            
            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                return HttpResponseRedirect(reverse('add_category'))
            
            page.views = 0
            page.save()
            
            return HttpResponseRedirect(reverse('category', args=[category_name_url]))
        else:
            print form.errors
    else:
        form = PageForm()
    
    return render_to_response('rango/add_page.html', {'form':form,
            'category_name_url': category_name_url, 'category_name': category_name,
            'cat_list': cat_list}, context)
Ejemplo n.º 28
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    category_name = url_decode(category_name_url)
    if request.method == 'POST':
        form = PageForm(request.POST)
        
        if form.is_valid():
            # 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:
                return render_to_response('rango/add_category.html', {}, context)
            
            page.views = 0
            page.save()

            return category(request, category_name_url)
        else:
            print form.errors
            
    else:
        form = PageForm()
    context_dict = {'cat_list': get_category_list(),
                    'category_name_url': category_name_url,
                    'category_name': category_name,
                    'form': form}
    
    return render_to_response('rango/add_page.html', context_dict, context)
Ejemplo n.º 29
0
def add_page(request, category_name_slug):
    """
    User must be logged in to use this functionality.
    Adds a new page corresponding to the information in the form filled
    by the user with 'category_name_slug' as its category.
    If form empty, display form. If form filled and valid, create
    a new page. If form filled and not valid, display form errors.
    """

    # pylint: disable=E1103
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None
    # pylint: enable=E1103

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                return category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()

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

    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 30
0
def add_page(request, category_name_slug):
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == "POST":
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                # return category(request, category_name_slug)
                redirect_url = "/rango/category/" + category_name_slug + "/"
                return redirect(redirect_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {"form": form, "category": cat, "category_name_slug": category_name_slug}

    return render(request, "rango/add_page.html", context_dict)
Ejemplo n.º 31
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        category = None

    form = PageForm()
    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()
        else:
            print(form.errors)

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

    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 32
0
    def post(self, request, category_name_slug):
        form = PageForm(request.POST)
        category = self.get_category_name(category_name_slug)

        if category is None:
            return redirect(reverse('rango:index'))

        if form.is_valid():
            page = form.save(commit=False)
            page.category = category
            page.views = 0
            page.save()

            return redirect(
                reverse('rango:show_category',
                        kwargs={'category_name_slug': category_name_slug}))
        else:
            print(form.errors)

        context_dict = {'form': form, 'category': category}
        return render(request, 'rango/add_page.html', context=context_dict)
Ejemplo n.º 33
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    category_name = category_name_url.replace('_', ' ')
    context_dict = {'category_name': category_name}
    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_to_response('rango/add_page.html', {}, context)

            # 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()

    return render_to_response('rango/add_page.html', {
        'category_name_url': category_name_url,
        'form': form
    }, context)
Ejemplo n.º 34
0
def add_page(request, category_name_slug):

    category_name_slug = slugify(category_name_slug)

    try:
        category = Category.objects.get(slug=category_name_slug)

    except Category.DoesNotExist:
        category = None

    form = PageForm()

    url = request.POST['url']

    if str(url[1:19]) == 'http://omgili.com/':

        page = Page.objects.get_or_create(category=category,
                                          title=revreplace(
                                              request.POST['title']),
                                          url=url[1:-1],
                                          views=0)

    else:
        if request.method == 'POST':
            form = PageForm(request.POST)

            if form.is_valid():
                page = form.save(commit=False)
                page.views = 0
                page.url = url
                page.category = category
                page.save()
                return show_cat(request, category_name_slug)
            else:
                print(form.errors)

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

    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 35
0
def add_page(request, category_name_slug):
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                # probably better to use a redirect here.
                return category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()

    return render(request, 'rango/add_page.html', {'form':form, 'category': cat})
Ejemplo n.º 36
0
def add_page(request, category_name_slug):
    try:
        c = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        c = None
    form = PageForm()

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if c:
                p = form.save(commit=False)
                p.views = 0
                p.category = c
                p.save()
                return show_category(request, category_name_slug)
        else:
            print(form.errors)
    return render(request, 'rango/add_page.html', {
        'form': form,
        "category": c
    })
Ejemplo n.º 37
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        category = None

    form = PageForm()
    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()
                # переход на страницу category.html после отправки данных через форму
                return show_category(request, category_name_slug)
        else:
            print(form.errors)
    # если данные через форму не отправляли, открывется страница add_page.html
    context_dict = {'form': form, 'category': category}
    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 38
0
Archivo: views.py Proyecto: jliel/rango
def add_page(request, category_name_url):
    category_name = encode_url(category_name_url)
    if request.method == "POST":
        form = PageForm(request.POST)

        if form.is_valid():
            page = form.save(commit=False)
            cat = Category.objects.get(name=category_name)
            page.category = cat
            page.views = 0
            page.save()
            return category(request, category_name_url)
        else:
            print(form.errors)
    else:
        form = PageForm()
    return render(
        request, 'rango/add_page.html', {
            'category_name_url': category_name_url,
            'category_name': category_name,
            'form': form
        })
Ejemplo n.º 39
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        category = None

    form = PageForm()
    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 show_category(request, category_name_slug)
    else:
        print("[FORMERROR] User-submitted form contained errors:")
        print(form.errors)

    context_dict = {"form":form, "category": category}
    return render(request, "rango/add_page.html", context_dict)
Ejemplo n.º 40
0
def add_page(request, category_name_slug):
    form = PageForm()  # page 模型  forms.Models
    try:
        category = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        category = None

    if request.method == 'GET':
        context_dict = {'form': form, 'category': category}
        return render(request, 'rango/add_page.html', context_dict)
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if category:
                page = form.save(commit=False)
                page.category = category  # 后台自动添加分类和 views
                page.views = 0
                page.save()
                return HttpResponseRedirect(reverse('rango:all_pages'))
        else:  # 表单提交有错误, 返回表单, 让用户重新填写提交
            context_dict = {'form': form, 'category': category}
            return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 41
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
    except:
        category = None
    if category is None:
        return redirect('/rango/')
    form = PageForm()
    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(reverse('rango:show_category', kwargs={'category_name_slug': category_name_slug}))
        else:
            print(form.errors)
    context_dict = {'form': form, 'category': category}
    return render(request, 'rango/add_page.html', context=context_dict)
Ejemplo n.º 42
0
def add_page(request, category_name_slug):

    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:                 # 取得form数据之后进行相关修改再保存
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                return category(request, category_name_slug)  # 重定向,交给category页面

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

    return render(request, 'rango/add_page.html/', {'cat': cat, 'form': form})
Ejemplo n.º 43
0
def add_page(request, category_name_slug):
    category = Category.objects.get(slug=category_name_slug)
    form = PageForm()

    # A http post?
    if request.method == 'POST':
        print("Post recieved.")
        form = PageForm(request.POST)

        # Check validity
        if form.is_valid():
            print("Valid form.")
            page = form.save(commit=False)  # Save to database
            page.category = category
            page.views = 0
            page.save()
            return show_category(request, category_name_slug)
        else:
            print(form.errors)

    pages = Page.objects.filter(category=category)
    context_dict = {'form': form, 'category': category, 'pages': pages}
    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 44
0
def add_page(request, category_name_slug):
    """View that handles the form to add pages to category"""
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                return category(request, category_name_slug)
        else:
            print form.errors

    else:
        form = PageForm()
    context_dict = {'form': form, 'category': cat}
    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 45
0
def add_page(request, category_name_url):
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            page = form.save(commit=False)
            try:
                cat = Category.objects.get(name=category_name_url)
                page.category = category
            except Category.DoesNotExist:
                return render(request, 'rango/add_category.html', {})
            page.views = 0
            page.save()
            return category(request, category_name_url)
        else:
            print(form.errors)
    else:
        form = PageForm()
    return render(
        request, 'rango/add_page.html', {
            'category_name_url': category_name_url,
            'category_name': category_name,
            'form': form
        })
Ejemplo n.º 46
0
def fifth_add_page(request, category_name_slug):  #test_second_page
    context_dict = {}
    # A HTTP POST?
    try:
        category = Category.objects.get(slug=category_name_slug)
    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  #设置category外键
                page.views = 0
                page.save()  # 此时提交
                return third_category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()
    context_dict['form'] = form
    context_dict['category'] = category
    return render(request, 'rango/fifth_add_page.html', context_dict)
Ejemplo n.º 47
0
    def post(self, request, category_name_slug):
        category = self.getCategory(category_name_slug)
        if category is None:
            return redirect(reverse("rango:index"))

        form = PageForm(request.POST)
        # Have we been provided with a valid form?
        if form.is_valid():
            if category:
                page = form.save(commit=False)
                page.category = category
                page.views = 0
                page.save()
                return redirect(
                    reverse("rango:show_category",
                            kwargs={"category_name_slug": category_name_slug}))
        else:
            # The supplied form contained errors -
            # just print them to the terminal.
            print(form.errors)

        context_dict = {"form": form, "category": category}
        return render(request, "rango/add_page.html", context=context_dict)
Ejemplo n.º 48
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    if request.method == "POST":
        form = PageForm(request.POST)
        if form.is_valid():
            page = form.save(commit=False)
            cat = Category.objects.get(name=category_name)
            page.category = cat
            page.views = 0
            page.likes = 0
            page.save()
            return category(request, category_name_url)

        else:
            print(form.errors)

    else:
        form = PageForm()
    return render_to_response('rango/add_page.html', {
        'category_name_url': category_name_url,
        'form': form
    }, context)
Ejemplo n.º 49
0
def add_page(request, category_name_slug):
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                return HttpResponseRedirect('/rango/')  # vs. index(request)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {'form': form, 'category': cat, 'slug': category_name_slug}

    return render(request, 'rango/add_page.html/', context_dict)
Ejemplo n.º 50
0
def add_page(request):
    # A HTTP POST?
    if request.method == 'POST':
        form = PageForm(request.POST)

        # Have we been provided with a valid form?
        if form.is_valid():
            # Save the new category to the database.
            form.save(commit=True)

            # Now call the index() view.
            # The user will be shown the homepage.
            return index(request)
        else:
            # The supplied form contained errors - just print them to the terminal.
            print form.errors
    else:
        # If the request was not a POST, display the form to enter details.
        form = PageForm()

    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    return render(request, 'rango/add_page.html', {'form': form})
Ejemplo n.º 51
0
def add_page(request, category_name_url):
    context = RequestContext(request)
    cat_list = get_category_list()
    context_dict = {}
    context_dict['cat_list'] = cat_list

    category_name = decode_url(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.
            cat = Category.objects.get(name=category_name)
            page.category = cat

            # 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()

    context_dict['category_name_url'] = category_name_url
    context_dict['category_name'] = category_name
    context_dict['form'] = form

    return render_to_response('rango/add_page.html', context_dict, context)
Ejemplo n.º 52
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        category = None

    # Can't add a page to a category that doesn't exist
    if category is None:
        return redirect(reverse('rango:index'))

    form = PageForm()

    # A HTTP POST?
    if request.method == 'POST':
        form = PageForm(request.POST)

        # Have we been provided with a valid form?
        if form.is_valid():
            if category:
                # Save the page to the database.
                page = form.save(commit=False)
                page.category = category
                page.views = 0
                page.save()
                # Now that the page is saved, we could confirm this.
                # For now just redirect the user back to the index view.
                return redirect(
                    reverse('rango:show_category',
                            kwargs={'category_name_slug': category_name_slug}))
        else:
            # The supplied form contains errors - just print them to the terminal.
            print(form.errors)

    # Will handle the bad form, new form, or no form supplied cases.
    # Render the form with error messages (if any).
    context_dict = {'form': form, 'category': category}
    return render(request, 'rango/add_page.html', context=context_dict)
Ejemplo n.º 53
0
def add_page(request, category_name_url):
    context = RequestContext(request)
    category_name = category_name_url.replace(' ', '_')

    if request.method == 'POST':
        print "POST"
        form = PageForm(request.POST)

        if form.is_valid():
            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat  #Foreign Key

            except Category.DoesNotExist:
                return render_to_response('rango/add_category.html', {},
                                          context)

            page.views = 0
            page.save()

            return category(request, category_name_url)
        else:
            print form.errors

    else:
        form = PageForm()

    return render_to_response(
        'rango/add_page.html', {
            'form': form,
            'category_name_url': category_name_url,
            'category_name': category_name,
            'cat_list': get_category_list()
        }, context)
Ejemplo n.º 54
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    category_name = decode_url(category_name_url)

    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                return render_to_response('rango/add_category.html', {},
                                          context)

            page.views = 0

            page.save()

            return category(request, category_name_url)

        else:
            print name.errors

    else:
        form = PageForm()

    return render_to_response(
        'rango/add_page.html', {
            'category_name_url': category_name_url,
            'category_name': category_name,
            'form': form
        }, context)
Ejemplo n.º 55
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        category = None

    form = PageForm()
    if request.method == 'POST':
        form = PageForm(request.POST)
        # Have we been provided with a valid form?
        if form.is_valid():
            if category:
                page = form.save(commit=False)
                page.category = category
                page.views = 0
                page.save()
            return show_category(request, category_name_slug)
        else:
            # the supplied form contains errors -
            # just print them to the terminal
            print(form.errors)

    context_dict = {'form': form, 'category': category}
    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 56
0
def add_page(request, category_name_slug):
    try:
        # Get category slug
        category = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        # Set to None if not exist
        category = None

    form = PageForm()

    # POST Request
    if request.method == 'POST':
        form = PageForm(request.POST)
        # If form is valid and category isn't empty, save form then return template
        if form.is_valid():
            if category:
                print(category, '- Category from add_page')
                page = form.save(commit=False)
                page.category = category
                page.views = 0
                page.save()
                request.method = 'GET'
                return redirect('/rango/category/'+category_name_slug)
        else:
            # Return form erors
            print(form.errors)
    
    # Init context to be passed in the template
    context_dict = {
        'form': form,
        'category': category
    }

    return render(request,
                  'rango/add_page.html',
                  context_dict)
Ejemplo n.º 57
0
def add_page(request, category_name_slug):
    form = PageForm()
    if request.method == "POST":
        form = PageForm(request.POST)
        try:
            cat = Category.objects.get(slug=category_name_slug)
        except Category.DoesNotExist:
            cat = None
            
        if cat and form.is_valid():
            page = form.save(commit = False)
            page.category = cat
            page.views = 0
            page.first_visit = datetime.now()
            page.last_visit = datetime.now()
            page.save()
            return show_category(request, category_name_slug)
        else:
            print form.errors
    context_dict = {'form':form, 'slug':category_name_slug}
    return render(request, 'rango/add_page.html', context_dict)


    
Ejemplo n.º 58
0
def add_page(request, category_name_slug):
    context_dict = {}
    try:
            cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
            cat = None
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                    page = form.save(commit=False)
                    page.category = cat
                    page.views = 0
                    page.save()
                    # probably better to use a redirect here.
                    return HttpResponseRedirect('/rango/')
            else:
                    print (form.errors)
    else:
        form = PageForm()

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

    return render(request, "rango/add_page.html", context_dict)
Ejemplo n.º 59
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
    except:
        category = None
    form = PageForm()

    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 show_category(request, category_name_slug)
            else:
                print(form.errors)
    context_dict = {
        'form': form,
        'category': category
    }
    context_dict['cat_list']= Category.objects.order_by('-views')
    return render(request, 'rango/add_page.html', context_dict)
Ejemplo n.º 60
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
        #username = User.objects.get(slug=user_name_slug)
    except Category.DoesNotExist:
        category = None
        #username = None

    form = PageForm()
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if category:
                page = form.save(commit=False)
                page.category = category
                if request.user.is_authenticated():
                    page.username = request.user
                page.save()
                return show_category(request, category_name_slug)
        else:
            print(form.errors)

    context_dict = {'form': form, 'category': category}
    return render(request, 'rango/add_page.html', context_dict)