quinta-feira, 19 de outubro de 2017

sexta-feira, 18 de agosto de 2017

DJANGO - Initial steps

If you're starting from scratch
pip install virtualenv
--------------------------------------

virtualenv [env_name]
pip install django
python Scripts\django-admin.py startproject [project_name]
python manage.py startapp [app_name]


sexta-feira, 11 de agosto de 2017

Without LoginRequiredMixin redirect, request and settings show an error 'not defined'


If you don't use this LoginRequiredMixin, you're gonna receive

name settings not defined
name request not defined
name redirect not defined



class BookListView(LoginRequiredMixin,generic.ListView):
    #login_url = '/login/'
    #redirect_field_name = 'redirect_to'
       
    model = Book
    paginate_by = 2  
    context_object_name = 'book_list'   # your own name for the list as a template variable
    #queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war
    queryset = Book.objects.all()
    template_name = 'books/book_list.html'  # Specify your own template name/location
   
    def get_context_data(self, **kwargs):
        if not request.user.is_authenticated:
            return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
        else:
            # Call the base implementation first to get a context
            context = super(BookListView, self).get_context_data(**kwargs)
            # Get the blog from id and add it to the context
            context['some_data'] = 'This is just some data'
            return context

domingo, 6 de agosto de 2017

The Zen of Python - Factorial

def calculate_factorial(n):
    def nfatorial(current,currentfactorial):
        if current < 2:
            return currentfactorial
        else:
            current = current - 1
            currentfactorial = currentfactorial * (current)
            return nfatorial(current,currentfactorial)
    return nfatorial(n+1,1)

The Zen of Python - Fibonacci

# Type your code here
def calculate_fibonacci(n):

    def iterativex(max1, previous, current,contador):
        newcurrent = current + previous
        contador = contador + 1
        if contador > max1:
           return current
        else:
           return iterativex(max1,current,newcurrent,contador)  

    return iterativex(n,0,1,1)

sexta-feira, 28 de julho de 2017

The Zen of Python - Bubble Sort

def return_sorted_list(unsorted_list):
    sorted_list = unsorted_list[:]
    for i in range(0,len(sorted_list)):
        for i in range(0,len(sorted_list)):
                if i < (len(sorted_list) - 1):
                    current_item = sorted_list[i]
                    next_item = sorted_list[i+1]
                    if current_item > next_item:
                            sorted_list[i] = next_item
                            sorted_list[i+1] = current_item
    return sorted_list

print(return_sorted_list([3,4,2,6,7]))


sexta-feira, 21 de julho de 2017

DJANGO - TIME_ZONE = 'America/Sao Paulo'


Para corrigir o problema das três horas a mais.

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Sao_Paulo'

USE_I18N = True

USE_L10N = True

USE_TZ = False