feat: article creation
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from .models import Article
|
||||
from django.shortcuts import render
|
||||
from django.http import Http404
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
|
||||
def archive(request):
|
||||
return render(request, 'archive.html', {"posts": Article.objects.all()})
|
||||
@@ -8,6 +9,23 @@ def archive(request):
|
||||
def get_article(request, article_id):
|
||||
try:
|
||||
post = Article.objects.get(id=article_id)
|
||||
return render(request, 'article.html', {"post": post})
|
||||
return render(request, 'article.html', {"post": post, "user": request.user})
|
||||
except Article.DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
def create_post(request):
|
||||
if request.method == "POST":
|
||||
form = {
|
||||
'text': request.POST["text"], 'title': request.POST["title"]
|
||||
}
|
||||
if form["text"] and form["title"]:
|
||||
try:
|
||||
article = Article.objects.create(text=form["text"], title=form["title"], author=request.user)
|
||||
return redirect('get_article', article_id=article.id)
|
||||
except:
|
||||
return render(request, 'article_invalid_name.html')
|
||||
|
||||
if not request.user.is_anonymous:
|
||||
return render(request, 'new_article.html')
|
||||
else:
|
||||
return HttpResponse('Unauthorized', status=401)
|
||||
|
||||
Reference in New Issue
Block a user