diff --git a/myproject1/articles/forms.py b/myproject1/articles/forms.py index 3d30b166a780f783f707270ad6f6543e173c9e0e..0bc0e1bec62e14f19c0ac8993f521a201e840b76 100644 --- a/myproject1/articles/forms.py +++ b/myproject1/articles/forms.py @@ -15,8 +15,7 @@ class ArticleEditForm(forms.ModelForm): fields = ['title', 'body', 'slug', 'thumb','tags'] - class CommentForm(forms.ModelForm): class Meta: model = Comment - fields = ('content',) \ No newline at end of file + fields = ('text',) \ No newline at end of file diff --git a/myproject1/articles/migrations/0001_initial.py b/myproject1/articles/migrations/0001_initial.py index f00b5f18644cd6137e9045e9a475fc75e6662e6a..70fa4af3a9deceb892cb03cb20c0ad13a914de3e 100644 --- a/myproject1/articles/migrations/0001_initial.py +++ b/myproject1/articles/migrations/0001_initial.py @@ -1,6 +1,10 @@ -# Generated by Django 2.2 on 2019-05-19 12:29 +# Generated by Django 2.2 on 2019-06-28 21:36 +from django.conf import settings from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone +import taggit.managers class Migration(migrations.Migration): @@ -8,6 +12,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ + ('taggit', '0003_taggeditem_add_unique_index'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ @@ -19,6 +25,19 @@ class Migration(migrations.Migration): ('slug', models.SlugField()), ('body', models.TextField()), ('date', models.DateTimeField(auto_now_add=True)), + ('thumb', models.ImageField(blank=True, default='default.png', upload_to='')), + ('author', models.ForeignKey(default=None, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), + ('tags', taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags')), + ], + ), + migrations.CreateModel( + name='Comment', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('author', models.CharField(max_length=200)), + ('text', models.TextField()), + ('created_date', models.DateTimeField(default=django.utils.timezone.now)), + ('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='articles.Article')), ], ), ] diff --git a/myproject1/articles/migrations/0002_article_thumb.py b/myproject1/articles/migrations/0002_article_thumb.py deleted file mode 100644 index fab11d0cdbbf73184395dec188028fd47d94d3f9..0000000000000000000000000000000000000000 --- a/myproject1/articles/migrations/0002_article_thumb.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2 on 2019-06-02 10:59 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('articles', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='article', - name='thumb', - field=models.ImageField(blank=True, default='default.png', upload_to=''), - ), - ] diff --git a/myproject1/articles/migrations/0003_article_author.py b/myproject1/articles/migrations/0003_article_author.py deleted file mode 100644 index 7b983d3af77284b8fc3fbc7db242594904654cad..0000000000000000000000000000000000000000 --- a/myproject1/articles/migrations/0003_article_author.py +++ /dev/null @@ -1,21 +0,0 @@ -# Generated by Django 2.2 on 2019-06-02 14:01 - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('articles', '0002_article_thumb'), - ] - - operations = [ - migrations.AddField( - model_name='article', - name='author', - field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL), - ), - ] diff --git a/myproject1/articles/migrations/0004_comment.py b/myproject1/articles/migrations/0004_comment.py deleted file mode 100644 index 48d33fc22ef887501c4e95d586e3fddfd17a46a8..0000000000000000000000000000000000000000 --- a/myproject1/articles/migrations/0004_comment.py +++ /dev/null @@ -1,26 +0,0 @@ -# Generated by Django 2.2 on 2019-06-02 14:58 - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('articles', '0003_article_author'), - ] - - operations = [ - migrations.CreateModel( - name='Comment', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('content', models.TextField(max_length=150)), - ('timestamp', models.DateTimeField(auto_now_add=True)), - ('article', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='articles.Article')), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), - ], - ), - ] diff --git a/myproject1/articles/migrations/0005_auto_20190602_2247.py b/myproject1/articles/migrations/0005_auto_20190602_2247.py deleted file mode 100644 index da8ea25cceb95b94df330d194d76297d633a1155..0000000000000000000000000000000000000000 --- a/myproject1/articles/migrations/0005_auto_20190602_2247.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 2.2 on 2019-06-02 20:47 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('articles', '0004_comment'), - ] - - operations = [ - migrations.RenameField( - model_name='comment', - old_name='timestamp', - new_name='date', - ), - migrations.RenameField( - model_name='comment', - old_name='article', - new_name='post', - ), - ] diff --git a/myproject1/articles/migrations/0006_auto_20190603_0134.py b/myproject1/articles/migrations/0006_auto_20190603_0134.py deleted file mode 100644 index 893540efa946ef422252e1cbe622ede0a10602e0..0000000000000000000000000000000000000000 --- a/myproject1/articles/migrations/0006_auto_20190603_0134.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2 on 2019-06-02 23:34 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('articles', '0005_auto_20190602_2247'), - ] - - operations = [ - migrations.RenameField( - model_name='comment', - old_name='date', - new_name='timestamp', - ), - ] diff --git a/myproject1/articles/migrations/0007_auto_20190603_0240.py b/myproject1/articles/migrations/0007_auto_20190603_0240.py deleted file mode 100644 index 186917007c0ca76db150fb1d7c1ee2bdea337473..0000000000000000000000000000000000000000 --- a/myproject1/articles/migrations/0007_auto_20190603_0240.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2 on 2019-06-03 00:40 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('articles', '0006_auto_20190603_0134'), - ] - - operations = [ - migrations.RenameField( - model_name='comment', - old_name='post', - new_name='article', - ), - ] diff --git a/myproject1/articles/migrations/0008_article_tags.py b/myproject1/articles/migrations/0008_article_tags.py deleted file mode 100644 index 91b36f75ba70d2122551fbaa3620c988540b1f9a..0000000000000000000000000000000000000000 --- a/myproject1/articles/migrations/0008_article_tags.py +++ /dev/null @@ -1,20 +0,0 @@ -# Generated by Django 2.2.1 on 2019-06-14 09:12 - -from django.db import migrations -import taggit.managers - - -class Migration(migrations.Migration): - - dependencies = [ - ('taggit', '0003_taggeditem_add_unique_index'), - ('articles', '0007_auto_20190603_0240'), - ] - - operations = [ - migrations.AddField( - model_name='article', - name='tags', - field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'), - ), - ] diff --git a/myproject1/articles/models.py b/myproject1/articles/models.py index 64098bd510b244f4f7e4c1d71a82d3d7799f1d28..b80487833cb7fc79b362f7c5255e974f4a39be75 100644 --- a/myproject1/articles/models.py +++ b/myproject1/articles/models.py @@ -1,6 +1,7 @@ from django.db import models from django.contrib.auth.models import User from taggit.managers import TaggableManager +from django.utils import timezone # Create your models here. class Article(models.Model): @@ -22,11 +23,16 @@ class Article(models.Model): class Comment(models.Model): - article = models.ForeignKey(Article, on_delete=models.PROTECT) - user = models.ForeignKey(User, on_delete=models.PROTECT) - content = models.TextField(max_length=150) - timestamp = models.DateTimeField(auto_now_add=True) + post = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='comments') + author = models.CharField(max_length=200) + text = models.TextField() + created_date = models.DateTimeField(default=timezone.now) + #approved_comment = models.BooleanField(default=False) + + #def approve(self): + #self.approved_comment = True + #self.save() def __str__(self): - return '{}.{}'.format(self.article.title, str(self.user.username)) + return '{}.{}'.format(self.post.title, str(self.author)) diff --git a/myproject1/articles/templates/articles/article_detail.html b/myproject1/articles/templates/articles/article_detail.html index 97020b77d98a28b8bb9b0590716427e48c5f0ada..f46ea1082854e9393a4e26269cd63bb316168769 100644 --- a/myproject1/articles/templates/articles/article_detail.html +++ b/myproject1/articles/templates/articles/article_detail.html @@ -5,40 +5,29 @@ <div class="article"> <img src="{{ article.thumb.url }}" /> <h2>{{ article.title }}</h2> + <h3>Written by {{ article.author.username }}</h3> <p>{{ article.body }}</p> <p>{{ article.date }}</p> - <p>{{article.tags.all|join:", "}}</p> + <p>{{ article.tags }}</p> </div> </div> -{% if article.author == request.user %} -<div class="section-1" style="float: right;"> -<a href="{% url 'articles:article_edit' id=article.id %}"> - <button type="button" class="btn btn-outline-success">Edit</button> -</a> -<a href="{% url 'articles:article_delete' id=article.id %}"> - <button type="button" class="btn btn-outline-success">Delete</button> -</a> -</div> - {% endif %} -<br><br> <hr> -<form method="article"> - {% csrf_token %} - {{ comment_form.as_p }} - <input type="submit" value = "submit" class="btn btn-outline-success"> -</form> - -<div class="main-comment-section"> - {{ comments.count }} Comment{{ comments|pluralize }} - {% for comment in comments %} - <blockquote class="blockquote"> - <p class="mb-0">{{ comments.content }}</p> - <footer class="blockquote-footer">by <cite title="Source Title">{{ comment.user|capfirst }}</cite></footer> - </blockquote> - {% endfor %} +<form method="post"> + {% csrf_token %} + {{ comment_form.as_p }} + <input type="submit" value="Submit" class="btn btn-outline.success"> -</div> +</form> +{% for comment in article.comments.all %} + <div class="comment"> + <div class="date">{{ comment.created_date }}</div> + <strong>{{ comment.author }}</strong> + <p>{{ comment.text|linebreaks }}</p> + </div> +{% empty %} + <p>No comments here yet :(</p> +{% endfor %} {% endblock %} \ No newline at end of file diff --git a/myproject1/articles/templates/articles/article_list.html b/myproject1/articles/templates/articles/article_list.html index 0270f1a07559ffdad109ccb0b12ecfb78ecb7015..08cedf5b05ed65f98bde233de1492f8b1a5713bb 100644 --- a/myproject1/articles/templates/articles/article_list.html +++ b/myproject1/articles/templates/articles/article_list.html @@ -26,7 +26,7 @@ <div class="articles"> {% for article in articles %} <div class="article"> - <a href="{% url 'articles:detail' slug=article.slug %}"> + <h2><a href="{% url 'articles:detail' slug=article.slug %}">{{ article.title }}</a></h2> <h2><b><i><u>{{ article.title }}</u></i></b></h2> <p>{{ article.snippet }}</p> <p>{{ article.date }}</p> diff --git a/myproject1/articles/views.py b/myproject1/articles/views.py index 0ab2551cb2488cf57d9e1b4626f96927df58020f..3097ba4fe25814c5182064ae4b8acbfec20a67b3 100644 --- a/myproject1/articles/views.py +++ b/myproject1/articles/views.py @@ -1,13 +1,16 @@ +from django.urls import reverse from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.shortcuts import render, redirect, get_object_or_404 from taggit.models import Tag from django.views.generic import DetailView, ListView from .forms import * +from . import forms +from .forms import CreateArticle, CommentForm from .models import Article from django.contrib.auth.decorators import login_required -from . import forms + @@ -40,12 +43,26 @@ def article_list(request): return render(request, 'articles/article_list.html', { 'articles': articles }) +def article_detail(request, slug): # return HttpResponse(slug) + post = get_object_or_404(Article, slug=slug) + comments = Comment.objects.filter(post=post).order_by('.date') + if request.method == 'POST': + comment_form = CommentForm(request.POST or None) + if comment_form.is_valid(): + text = request.POST.get('text') + comment = Comment.objects.create(post=post, author=request.user.username, text=text) + comment.save() + return HttpResponseRedirect(request.path_info) + else: + comment_form = CommentForm() + context={ + 'article': post, + 'comments': comments, + 'comment_form': comment_form, + } + return render(request, 'articles/article_detail.html', context) -def article_detail(request, slug): - # return HttpResponse(slug) - article = Article.objects.get(slug=slug) - return render(request, 'articles/article_detail.html', {'article': article}) diff --git a/myproject1/db.sqlite3 b/myproject1/db.sqlite3 index 0663026b98a1dbb107902a27a2bff438f484a6f5..09747ac0b8bf0688c98ef51a8cf536f39085e370 100644 Binary files a/myproject1/db.sqlite3 and b/myproject1/db.sqlite3 differ