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)