From d576fd969388f7593eed71ebd6c4055ddee8ebde Mon Sep 17 00:00:00 2001 From: Laurent Morvillier Date: Thu, 10 Oct 2019 11:47:38 +0200 Subject: [PATCH] Added slug to post urls --- news/forms.py | 2 +- news/migrations/0008_auto_20191010_0909.py | 23 ++++++++++++++++++++++ news/migrations/0009_auto_20191010_0910.py | 18 +++++++++++++++++ news/migrations/0010_auto_20191010_0945.py | 18 +++++++++++++++++ news/models.py | 17 ++++++++++++++-- news/static/news/css/app.css | 11 ++++------- news/templates/news/index.html | 8 ++++---- news/urls.py | 3 ++- news/views.py | 5 ++++- 9 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 news/migrations/0008_auto_20191010_0909.py create mode 100644 news/migrations/0009_auto_20191010_0910.py create mode 100644 news/migrations/0010_auto_20191010_0945.py diff --git a/news/forms.py b/news/forms.py index 6de3f19..386bdc7 100644 --- a/news/forms.py +++ b/news/forms.py @@ -10,7 +10,7 @@ from datetime import datetime, date, time, timedelta class PostForm(forms.Form): style = forms.ChoiceField(label='Style',choices=STYLE_CHOICES, required=True) - title = forms.CharField(label='Title', max_length=200, required=False) + title = forms.CharField(label='Title (required for slug/SEO)', max_length=200, required=True) url = forms.CharField(label='URL', max_length=200, required=False) body = forms.CharField(label='Optional body', max_length=10000, required=False) diff --git a/news/migrations/0008_auto_20191010_0909.py b/news/migrations/0008_auto_20191010_0909.py new file mode 100644 index 0000000..6fce27c --- /dev/null +++ b/news/migrations/0008_auto_20191010_0909.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.6 on 2019-10-10 09:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0007_auto_20191009_1009'), + ] + + operations = [ + migrations.AddField( + model_name='post', + name='slug', + field=models.SlugField(default='', unique=True), + ), + migrations.AlterField( + model_name='tag', + name='post', + field=models.ManyToManyField(blank=True, to='news.Post'), + ), + ] diff --git a/news/migrations/0009_auto_20191010_0910.py b/news/migrations/0009_auto_20191010_0910.py new file mode 100644 index 0000000..ad315f8 --- /dev/null +++ b/news/migrations/0009_auto_20191010_0910.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.6 on 2019-10-10 09:10 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0008_auto_20191010_0909'), + ] + + operations = [ + migrations.AlterField( + model_name='post', + name='slug', + field=models.SlugField(default=''), + ), + ] diff --git a/news/migrations/0010_auto_20191010_0945.py b/news/migrations/0010_auto_20191010_0945.py new file mode 100644 index 0000000..5cbb56e --- /dev/null +++ b/news/migrations/0010_auto_20191010_0945.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.6 on 2019-10-10 09:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0009_auto_20191010_0910'), + ] + + operations = [ + migrations.AlterField( + model_name='post', + name='slug', + field=models.SlugField(default='', max_length=200), + ), + ] diff --git a/news/models.py b/news/models.py index 80b7ef8..e5288df 100644 --- a/news/models.py +++ b/news/models.py @@ -1,8 +1,9 @@ from django.db import models from django.contrib.auth.models import User from django.conf import settings -from enum import Enum from django.db.models import Q +from django.utils.text import slugify +from enum import Enum # Create your models here. class PostState(Enum): @@ -20,6 +21,11 @@ class Post(models.Model): state = models.IntegerField(default=0) style = models.IntegerField(default=0) image_url = models.CharField(max_length=100) + slug = models.SlugField(default='', max_length=200) + + def save(self, *args, **kwargs): + self.slug = slugify(self.title) + super(Post, self).save(*args, **kwargs) def __str__(self): string = self.title @@ -29,6 +35,13 @@ class Post(models.Model): string = "Youtube: " + self.title return string + def get_absolute_url(self): + kwargs = { + 'pk': self.id, + 'slug': self.slug + } + return reverse('post_detail', kwargs=kwargs) + def top_comments(self): return self.comment_set.filter(Q(parent_comment=None)).order_by('-voters').all()[:1] @@ -45,7 +58,7 @@ class Post(models.Model): def html_title(self): if self.title: - return title + return self.title return 'Poker Rumble - Amazing content' class Comment(models.Model): diff --git a/news/static/news/css/app.css b/news/static/news/css/app.css index a574c88..e8543b5 100644 --- a/news/static/news/css/app.css +++ b/news/static/news/css/app.css @@ -46,10 +46,6 @@ header { } } -/* header a { - text-decoration: none; -} */ - .title { background-color: var(--header-bg-color); } @@ -59,8 +55,7 @@ header h1 { margin-bottom: 0px; font-family: "HelveticaNeue-CondensedBlack", "HelveticaNeueBlackCondensed", "HelveticaNeue-Black-Condensed", "Helvetica Neue Black Condensed", "HelveticaNeueBlack", "HelveticaNeue-Black", "Helvetica Neue Black", "HelveticaNeue", "Helvetica Neue", 'TeXGyreHerosCnBold', "Helvetica", "Tahoma", "Geneva", "Arial Narrow", "Arial", sans-serif; font-weight:900; - font-stretch:condensed; /* font-family: 'LibreBaskervilleBold'; */ - /* letter-spacing: -1px; */ + font-stretch:condensed; text-transform: uppercase; font-size: 32px; } @@ -75,6 +70,9 @@ header h1 a { color: #fff; } +header h1 a:hover { + color: #BFA030; +} header h2 { margin-top: -10px; padding: 0; @@ -83,7 +81,6 @@ header h2 { nav { padding: 4px; - /* margin-top: 4px; */ font-size: 14px; background-color: #333; } diff --git a/news/templates/news/index.html b/news/templates/news/index.html index c2dd151..0cb554f 100644 --- a/news/templates/news/index.html +++ b/news/templates/news/index.html @@ -34,7 +34,7 @@
- {% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %} + {% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}
@@ -54,7 +54,7 @@
@@ -74,7 +74,7 @@
@@ -94,7 +94,7 @@
diff --git a/news/urls.py b/news/urls.py index 48f2a90..1cd3c2a 100644 --- a/news/urls.py +++ b/news/urls.py @@ -11,7 +11,8 @@ urlpatterns = [ path('about', TemplateView.as_view(template_name='news/static/about.html'), name='about'), # Posts path('', views.index, name='index'), - path('', views.post, name='post'), + path("/", views.post, name="post"), + # path('', views.post, name='post'), path('/comment/', views.comment_with_parent, name='comment_with_parent'), path('/comment', views.comment, name='comment'), path('comment//delete', views.delete_comment, name='delete_comment'), diff --git a/news/views.py b/news/views.py index 1384a91..477dda5 100644 --- a/news/views.py +++ b/news/views.py @@ -11,6 +11,7 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import PasswordChangeForm from django.db.models import Q from django.utils.html import strip_tags +from django.utils.text import slugify from .models import Post, Comment, PostState, Tag from .forms import PostForm, CustomUserCreationForm, SigninForm from datetime import datetime @@ -89,7 +90,7 @@ def index(request): context = { 'latest_post_list' : posts } return render(request, 'news/index.html', context) -def post(request, post_id): +def post(request, post_id, post_slug): post = get_object_or_404(Post, pk=post_id) return render(request, 'news/post.html', {'post': post, 'comments': post.flat_comments(request.user) }) @@ -109,6 +110,8 @@ def submission(request): tags = parsed_tags(form.cleaned_data['tags']) post.tag_set.add(*tags) + post.slug = slugify(post.title) + file = request.FILES.get('image', None) if file is not None: filename = str(uuid.uuid4())