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})

[((548, 591), 'django.contrib.auth.decorators.login_required', 'login_required', ([], {'login_url': '"""/accounts/login"""'}), "(login_url='/accounts/login')\n", (562, 591), False, 'from django.contrib.auth.decorators import login_required\n'), ((278, 347), 'django.shortcuts.render', 'render', (['request', '"""articles/article_list.html"""', "{'articles': articles}"], {}), "(request, 'articles/article_list.html', {'articles': articles})\n", (284, 347), False, 'from django.shortcuts import render, redirect\n'), ((474, 544), 'django.shortcuts.render', 'render', (['request', '"""articles/article_details.html"""', "{'article': article}"], {}), "(request, 'articles/article_details.html', {'article': article})\n", (480, 544), False, 'from django.shortcuts import render, redirect\n'), ((1001, 1064), 'django.shortcuts.render', 'render', (['request', '"""articles/article_create.html"""', "{'form': form}"], {}), "(request, 'articles/article_create.html', {'form': form})\n", (1007, 1064), False, 'from django.shortcuts import render, redirect\n'), ((916, 941), 'django.shortcuts.redirect', 'redirect', (['"""articles:list"""'], {}), "('articles:list')\n", (924, 941), False, 'from django.shortcuts import render, redirect\n')]