Added slug to post urls

master
Laurent Morvillier 6 years ago
parent 9643962883
commit d576fd9693
  1. 2
      news/forms.py
  2. 23
      news/migrations/0008_auto_20191010_0909.py
  3. 18
      news/migrations/0009_auto_20191010_0910.py
  4. 18
      news/migrations/0010_auto_20191010_0945.py
  5. 17
      news/models.py
  6. 11
      news/static/news/css/app.css
  7. 8
      news/templates/news/index.html
  8. 3
      news/urls.py
  9. 5
      news/views.py

@ -10,7 +10,7 @@ from datetime import datetime, date, time, timedelta
class PostForm(forms.Form): class PostForm(forms.Form):
style = forms.ChoiceField(label='Style',choices=STYLE_CHOICES, required=True) 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) url = forms.CharField(label='URL', max_length=200, required=False)
body = forms.CharField(label='Optional body', max_length=10000, required=False) body = forms.CharField(label='Optional body', max_length=10000, required=False)

@ -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'),
),
]

@ -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=''),
),
]

@ -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),
),
]

@ -1,8 +1,9 @@
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.conf import settings from django.conf import settings
from enum import Enum
from django.db.models import Q from django.db.models import Q
from django.utils.text import slugify
from enum import Enum
# Create your models here. # Create your models here.
class PostState(Enum): class PostState(Enum):
@ -20,6 +21,11 @@ class Post(models.Model):
state = models.IntegerField(default=0) state = models.IntegerField(default=0)
style = models.IntegerField(default=0) style = models.IntegerField(default=0)
image_url = models.CharField(max_length=100) 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): def __str__(self):
string = self.title string = self.title
@ -29,6 +35,13 @@ class Post(models.Model):
string = "Youtube: " + self.title string = "Youtube: " + self.title
return string return string
def get_absolute_url(self):
kwargs = {
'pk': self.id,
'slug': self.slug
}
return reverse('post_detail', kwargs=kwargs)
def top_comments(self): def top_comments(self):
return self.comment_set.filter(Q(parent_comment=None)).order_by('-voters').all()[:1] 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): def html_title(self):
if self.title: if self.title:
return title return self.title
return 'Poker Rumble - Amazing content' return 'Poker Rumble - Amazing content'
class Comment(models.Model): class Comment(models.Model):

@ -46,10 +46,6 @@ header {
} }
} }
/* header a {
text-decoration: none;
} */
.title { .title {
background-color: var(--header-bg-color); background-color: var(--header-bg-color);
} }
@ -59,8 +55,7 @@ header h1 {
margin-bottom: 0px; 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-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-weight:900;
font-stretch:condensed; /* font-family: 'LibreBaskervilleBold'; */ font-stretch:condensed;
/* letter-spacing: -1px; */
text-transform: uppercase; text-transform: uppercase;
font-size: 32px; font-size: 32px;
} }
@ -75,6 +70,9 @@ header h1 a {
color: #fff; color: #fff;
} }
header h1 a:hover {
color: #BFA030;
}
header h2 { header h2 {
margin-top: -10px; margin-top: -10px;
padding: 0; padding: 0;
@ -83,7 +81,6 @@ header h2 {
nav { nav {
padding: 4px; padding: 4px;
/* margin-top: 4px; */
font-size: 14px; font-size: 14px;
background-color: #333; background-color: #333;
} }

@ -34,7 +34,7 @@
</div> </div>
<div class="details"> <div class="details">
<a href="{% url 'news:post' post.id %}">{% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}</a> <a href="{% url 'news:post' post.id post.slug %}">{% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}</a>
</div> </div>
<!-- Some comments --> <!-- Some comments -->
<div class="comments"> <div class="comments">
@ -54,7 +54,7 @@
<div class="post_padding"> <div class="post_padding">
<div class="details info"> <div class="details info">
<a href="{% url 'news:post' post.id %}">{% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}</a> <a href="{% url 'news:post' post.id post.slug %}">{% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}</a>
</div> </div>
<!-- Some comments --> <!-- Some comments -->
<div class="comments"> <div class="comments">
@ -74,7 +74,7 @@
<div class="post_padding"> <div class="post_padding">
<div class="details info"> <div class="details info">
<a href="{% url 'news:post' post.id %}">{% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}</a> <a href="{% url 'news:post' post.id post.slug %}">{% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}</a>
</div> </div>
<!-- Some comments --> <!-- Some comments -->
<div class="comments"> <div class="comments">
@ -94,7 +94,7 @@
<div class="post_padding"> <div class="post_padding">
<div class="details info"> <div class="details info">
<a href="{% url 'news:post' post.id %}">{% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}</a> <a href="{% url 'news:post' post.id post.slug %}">{% if post.comment_set.count > 0 %}{{ post.comment_set.count }} comment{% if post.comment_set.count > 1 %}s{% endif %}{% else %}write comment{% endif %}</a>
</div> </div>
<!-- Some comments --> <!-- Some comments -->
<div class="comments"> <div class="comments">

@ -11,7 +11,8 @@ urlpatterns = [
path('about', TemplateView.as_view(template_name='news/static/about.html'), name='about'), path('about', TemplateView.as_view(template_name='news/static/about.html'), name='about'),
# Posts # Posts
path('', views.index, name='index'), path('', views.index, name='index'),
path('<int:post_id>', views.post, name='post'), path("<int:post_id>/<slug:post_slug>", views.post, name="post"),
# path('<int:post_id>', views.post, name='post'),
path('<int:post_id>/comment/<int:comment_id>', views.comment_with_parent, name='comment_with_parent'), path('<int:post_id>/comment/<int:comment_id>', views.comment_with_parent, name='comment_with_parent'),
path('<int:post_id>/comment', views.comment, name='comment'), path('<int:post_id>/comment', views.comment, name='comment'),
path('comment/<int:comment_id>/delete', views.delete_comment, name='delete_comment'), path('comment/<int:comment_id>/delete', views.delete_comment, name='delete_comment'),

@ -11,6 +11,7 @@ from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.forms import PasswordChangeForm
from django.db.models import Q from django.db.models import Q
from django.utils.html import strip_tags from django.utils.html import strip_tags
from django.utils.text import slugify
from .models import Post, Comment, PostState, Tag from .models import Post, Comment, PostState, Tag
from .forms import PostForm, CustomUserCreationForm, SigninForm from .forms import PostForm, CustomUserCreationForm, SigninForm
from datetime import datetime from datetime import datetime
@ -89,7 +90,7 @@ def index(request):
context = { 'latest_post_list' : posts } context = { 'latest_post_list' : posts }
return render(request, 'news/index.html', context) 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) post = get_object_or_404(Post, pk=post_id)
return render(request, 'news/post.html', {'post': post, 'comments': post.flat_comments(request.user) }) 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']) tags = parsed_tags(form.cleaned_data['tags'])
post.tag_set.add(*tags) post.tag_set.add(*tags)
post.slug = slugify(post.title)
file = request.FILES.get('image', None) file = request.FILES.get('image', None)
if file is not None: if file is not None:
filename = str(uuid.uuid4()) filename = str(uuid.uuid4())

Loading…
Cancel
Save