from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Article
from django.contrib.auth.decorators import login_required
from . import forms

def Articles(request):
    articles = Article.objects.all().order_by('date')
    return render(request, 'articles/article_list.html', {'articles': articles})


def article_detail(request, slug):
    # return  HttpResponse(slug)
    article = Article.objects.get(slug=slug)
    return render(request, 'articles/article_details.html', {'article': article})


@login_required(login_url="/accounts/login")
def article_create(request):
    if request.method == 'POST':
        form = forms.CreateArticle(request.POST, request.FILES)
        if form.is_valid():
            #save article to DB
            instance = form.save(commit=False)
            instance.author = request.user
            instance.save( )
            return redirect ('articles:list')
    else:
        form = forms.CreateArticle()
    return render(request, 'articles/article_create.html', {'form':form})

[(18, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (9, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render, redirect\n'), (15, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render, redirect\n'), (30, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render, redirect\n'), (27, 'django.shortcuts.redirect', 'redirect', False, 'from django.shortcuts import render, redirect\n')]