Skip to content
Snippets Groups Projects
Commit 2b9b0409 authored by Mansoor Saleem's avatar Mansoor Saleem
Browse files

modifiy and upto date comment section

parent 891cd28c
No related branches found
No related tags found
No related merge requests found
Pipeline #3291 canceled
Showing
with 71 additions and 185 deletions
......@@ -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
# 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')),
],
),
]
# 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=''),
),
]
# 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),
),
]
# 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)),
],
),
]
# 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',
),
]
# 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',
),
]
# 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',
),
]
# 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'),
),
]
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))
......@@ -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
......@@ -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>
......
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})
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment