Skip to content

Commit

Permalink
Finish buttons functionality and adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
finziamit committed Jan 18, 2023
1 parent 741ab8a commit 2455548
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
17 changes: 8 additions & 9 deletions projboard/templates/editArticle/edit_article.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
<title>Smarticle</title>
</head>

<body>
<div class="article-container">
<h1 class="edit-article">Edit Article</h1>
<form method="POST">
{% csrf_token %} {{ form.as_p }}
<input type="submit" value="Publish" />
</form>
</div>
</body>
<div class="article-container">
<h1 class="edit-article">Edit Article</h1>
<form method="POST">
{% csrf_token %} {{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</div>
{% endblock %}
35 changes: 19 additions & 16 deletions projboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,33 +122,36 @@ def sign_up(request):
def delete_article(request, article_pk):
try:
article = Article.objects.get(id=article_pk)
user = article.user_id
article.delete()
return redirect(home_page)
nickname = user.nickname
return redirect(f"/my_articles/{nickname}/")

except User.DoesNotExist:
except Article.DoesNotExist:
raise Http404()


def edit_article(request, article_pk):
def edit_article(request, article_pk=None):
try:
article = Article.objects.get(article_pk)

initial = {'article_id': article, 'title': article.title,
'subject': article.subject_id, 'content': article.content}
article = Article.objects.get(id=article_pk)
user_id = article.user_id
title = article.title
subject_id = article.subject_id
content = article.content
initial = {'user_id': user_id, 'title': title, 'subject_id': subject_id, 'content': content}
if request.method == "POST":
form = EditArticleForm(request.POST, initial=initial)
form = EditArticleForm(request.POST, initial)
if form.is_valid():
form.save()
return render(request, 'editArticle/edit_article.html', initial)
# elif request.method == "DELETE":
# raise Http404()
article.edit(form.data['title'], form.data['content'], form.cleaned_data['subject_id'])
return redirect(f"/my_articles/{user_id.nickname}/")
elif request.method == "DELETE":
raise Http404()
else:
form = EditArticleForm(initial=initial)

return render(request, 'editArticle/edit_article.html', {
'article_pk': article_pk,
'user_id': user_id,
'form': form,
})
except User.DoesNotExist:
# raise Http404()
return redirect(home_page)
except Article.DoesNotExist:
raise Http404()

0 comments on commit 2455548

Please sign in to comment.