I am to write simple Django blog application.
Django Version: 2.0.4
http://dpaste.com/0H62TQY - this is error output. ( I hope it's safe to show this file all over the Internet =) )
models.py look like
from django.db import models
from django.shortcuts import reverse
from django.template.defaultfilters import slugify
from django.utils import timezone
class Blog(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.PROTECT)
title = models.CharField(max_length=500)
body = models.TextField()
created_at = models.DateTimeField(default=timezone.now)
slug = models.SlugField(default='', editable=False, unique=True, blank=False, null=False)
class Meta:
verbose_name_plural = "blog"
def __str__(self):
return self.title
def get_absolute_url(self):
kwargs = {'slug': self.slug}
return reverse('blog_detail', kwargs=kwargs)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super().save(*args, **kwargs)
views.pylook like
from django.views.generic import DetailView, ListView
from .models import Blog
class HomeView(ListView):
template_name = 'blog/blog.html'
queryset = Blog.objects.order_by('-created_at')
class BlogDetail(DetailView):
model = Blog
template_name = 'blog/blog-detail.html'
urls.py look like
from django.urls import path, re_path
from . import views
urlpatterns = [
path(r'', views.HomeView.as_view(), name='home'),
re_path(r'^(?P<slug>[-\w]*)/$', views.BlogDetail.as_view(), name='blog_detail'),
]
This is my blog.html
{% for blog in blog_list %}
<div class="item-blog-txt p-t-33">
<h4 class="p-b-11">
<a class="m-text24" href="{{ blog.get_absolute_url }}">
{{ blog.title }}
</a>
</h4>
<div class="s-text8 flex-w flex-m p-b-21">
<span>
{{ blog.author }}
<span class="m-l-3 m-r-6">|</span>
</span>
</div>
<p class="p-b-12">
{{ blog.body|linebreaksbr }}
</p>
<a class="s-text20" href="{{ blog.get_absolute_url }}">
Continue Reading
<i aria-hidden="true" class="fa fa-long-arrow-right m-l-8"></i>
</a>
</div>
{% endfor %}
blog-detail.html look like <a href="{{ blog.get_absolute_url }}"> and {{ blog.title }} and can say everything.
If you need something else to show, email me.
makemigrations and migrate was made.
Thank you very much.
It was the problem in database.
It was a new project so I could also just delete and start a new database.
Related
I am really new to Django and I am building a website while learning it. As per requirements, I have to show some news articles on both the home page and on another page that I navigate from the navbar. I have shown the articles list and individual details with the loop in my template files. Problem is, although I have written the for loop for rendering the news article on the home page, nothing is showing up. Following are my models.py, views.py, app/urls.py, and project/urls.py files :
#models.py
from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from taggit.managers import TaggableManager
from ckeditor.fields import RichTextField
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
# slug = models.SlugField(max_length=250, unique_for_date='publish')
slug = models.CharField(_("Slug"), max_length=200)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
body = RichTextField()
# image_header = models.ImageField(unique=True, default=timezone.now)
publish = models.DateTimeField(default=timezone.now)
# created = models.DateTimeField(auto_now_add=True)
# updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:details', args=[self.slug])
class News(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
news_title = models.CharField(max_length=250) unique_for_date='nw_publish', unique=True, null=True)
slug = models.SlugField(max_length=300, unique_for_date='nw_publish')
news_author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='news_posts')
news_body = RichTextField()
image_header = models.ImageField(upload_to='featured_image/%Y/%m/%d/', null=True, blank=True)
nw_publish = models.DateTimeField(default=timezone.now)
nw_status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
news_tags = TaggableManager()
class Meta:
ordering = ('nw_publish',)
def __str__(self):
return self.news_title
def get_absolute_url(self):
return reverse('news:news_detail', args=[self.slug])
#views.py
from django.views import generic
from django.views.generic import ListView, DetailView
from django.http import Http404
from .models import Post, Report, News, Member, Project
class Home(ListView):
model = Post
context_object_name = 'home'
template_name = 'blog/home.html'
class HomeDetail(generic.DetailView):
model = Post
context_object_name = 'details'
template_name = 'blog/details.html'
class NewsListView(ListView):
model = News
context_object_name = 'newss'
template_name = 'blog/news.html'
class NewsDetailView(DetailView):
model = News
context_object_name = 'news'
template_name = 'blog/news_detail.html'
#app/urls.py
from django.urls import path, re_path
from . import views
from .views import Home, HomeDetail, Contact, ReportListView, ReportDetailView, NewsListView, NewsDetailView
app_name = 'blog'
urlpatterns = [
# post views
path('', Home.as_view(), name='home'),
path('home/<slug:slug>/', HomeDetail.as_view(), name='details'),
path('news/', NewsListView.as_view(), name='news_list'),
path('news/<slug:slug>/', NewsDetailView.as_view(), name='news_detail'),
path('contact/', Contact.as_view(), name='contact'),
]
#project/urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls', namespace='blog')),
path('ckeditor/', include('ckeditor_uploader.urls')),
path('members/', include('blog.urls', namespace='members')),
path('news/', include('blog.urls', namespace='news')),
path('contact/', include('blog.urls', namespace='contact')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now my template file for new list has the following block of codes and the objects are rendered :
<div class="row">
{% for news in newss %}
<div class="col-lg-4 wow fadeInUp" data-wow-delay="100ms">
<!--News Two Single-->
<div class="news-two__single">
<div class="news-two__img-box">
<div class="news-two__img">
{% if news.image_header %}
<img src={{ news.image_header.url }} alt="{{ news.news_title }}">
{% endif %}
<a href={{ news.get_absolute_url }}>
<i class="fa fa-plus"></i>
</a>
</div>
<div class="news-two__date">
<p>{{ news.nw_publish |date:"M d, Y" }}</p>
</div>
</div>
<div class="news-two__content">
<ul class="list-unstyled news-two__meta">
<li><i class="far fa-user-circle"></i> {{ news.news_author }}</li>
</ul>
<h3>
<a href={{ news.get_absolute_url }}>{{ news.news_title }}</a>
</h3>
<p class="news-two__text">{{ news.news_body | truncatechars:110 | safe }}</p>
</div>
</div>
</div>
{% endfor %}
</div>
On my home page, I need to achieve something like the following:
news page
I added the following code to render the object, but it's blank:
<div class="row">
<div class="col-xl-6 col-lg-6">
{% for news in newss %}
<div class="news-one__left">
<div class="news-one__img">
<img src={% static "assets/images/blog/news-one-img-1.jpg" %} alt="">
<a href="{{news.get_absolute_url}}">
<i class="fa fa-plus"></i>
</a>
</div>
<div class="news-one__bottom">
<ul class="list-unstyled news-one__meta">
<li>20 Jan, 2021</li>
<li><span>/</span></li>
</ul>
<h3 class="news-one__title">
{{ news.news_title }}
</h3>
</div>
</div>
{% endfor %}
</div>
</div>
The output looks like this:
homepage
My Django version is 3.27.2 and my python version is 3.6.8.
Sorry for such a silly question. I am trying to achieve:
All the news articles both on the home page and news page as a list
Show the first news in the left box and the rest of the 3 or 4 news in the right side box
But the output of the loop is not visible as per the attached image. Please point me to what I am doing wrong here. I am stuck for a while. Thanks in advance.
I tried searching for this problem here but I didn't find anything, I hope someone can help answering or sending a link to a solution on stackoverflow...
I have been creating a blog, and when I click on the post title, it goes to 'post_details.html' and in the URL shows a slug with the post title. Like here:
examplename.com/blog/brooklyn-nets-at-miami-heat-preview/
But when I click to write a comment on the post it takes me to a form page, but the slug title disappear, it just shows the post number. Like this:
examplename.com/blog/3/add-comment
Can someone explain why is this happening? I want to know how to solve this and understand from where it comes this number that shows up.
Post_detail.html
{% block content %}
<h2 class="title">
<a class="post-title" href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Publicado em {{ post.created }} por {{ post.author }}
</p>
{{ post.body|linebreaks }}
<hr>
<h3>Comments</h3>
{% if not post.comments.all %}
Nobody commented yet...Add a comment
{% else %}
Add a comment
{% for comment in post.comments.all %}
{{ comment.name }}
{{ comment.date }}
<br>
{{ comment.body }}
{% endfor %}
{% endif %}
<a class="swen" href="{% url 'blog:list' %}">More Articles</a>
{% endblock %}
urls.py
from unicodedata import name
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path("", views.PostListView.as_view(), name="list"),
path("<slug:slug>/", views.PostDetailView.as_view(), name="detail"),
path("<slug:slug>/add-comment", views.CommentView.as_view(), name="commentview")
]
models.py
from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse ('blog:detail', kwargs={"slug":self.slug})
class Meta:
ordering = ("-created",)
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = models.TextField()
date = models.DateField(auto_now_add=True)
slug = Post.slug
def __str__(self):
return reverse (self.post.title, self.name, self.post.slug)
views.py
from xml.etree.ElementTree import Comment
from django.views.generic import DetailView, ListView, CreateView
from .models import Post, Comment
class PostListView(ListView):
model = Post
class PostDetailView(DetailView):
model = Post
class CommentView(CreateView):
model = Comment
template_name = 'add_comment.html'
fields = '__all__'
you're passing pk which is a type integer as parameter instead of a slug at path name commentview. May be you should check on that.
I wanted to have a comment section in the same page as in my DetailView so I decided to use the FormMixin as a way to be able to add comments. It's not raising any errors but the submitted comments seem to be going nowhere and it's not showing up in the admin site also.
models.py
from django.db import models
from django.utils import timezone
from django.urls import reverse
from embed_video.fields import EmbedVideoField
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length = 100)
content = models.TextField()
video = EmbedVideoField()
date_posted = models.DateTimeField(default = timezone.now)
author = models.ForeignKey(User, on_delete = models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
class PostComment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete = models.CASCADE)
author = models.ForeignKey(User, on_delete = models.CASCADE)
date_posted = models.DateTimeField(default = timezone.now)
body = models.TextField()
def __str__(self):
return f'{self.post} - {self.author}'
forms.py
from django import forms
from django.forms import ModelForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Fieldset, Button, Layout, Div, ButtonHolder, Field, Reset
from .models import Post, PostComment
from crispy_forms.bootstrap import FormActions
from django.urls import reverse
class CommentForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'POST'
self.fields['body'].required = True
self.fields['body'].label = False
self.helper.layout = Layout(
Field('body', style='max-height: 90px', placeholder='Write a comment...'),
ButtonHolder(
Submit('submit', 'Post', css_class='m-2 ms-3'),
Reset('Reset This Form', 'Cancel', css_class='btn btn-secondary')
),
)
class Meta:
model = PostComment
fields = ['body','post',]
views.py
I mostly got this from the django documentations.
from django.shortcuts import render
from django.views.generic import (
ListView,
DetailView,
CreateView,
UpdateView,
DeleteView
)
from .models import Post, PostComment
from .forms import PostCreateForm, CommentForm
from django.views.generic.edit import FormMixin
class PostDetailView(FormMixin, DetailView):
model = Post
context_object_name = 'posts'
form_class = CommentForm
def get_success_url(self):
return reverse('post-detail', kwargs={'pk': self.object.id})
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
context['form'] = CommentForm(initial={'post': self.object})
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.save()
return super(PostDetailView, self).form_valid(form)
urls.py
urlpatterns = [
path('', PostListView.as_view(), name = 'home-home'),
path('post/new/', PostCreateView.as_view(), name = 'post-create'),
path('post/<int:pk>/', PostDetailView.as_view(), name = 'post-detail'),
]
post_detail.html
{% extends 'videosite/base.html' %}
{% load crispy_forms_tags %}
{% load embed_video_tags %}
{% block body %}
<div class="container mt-5">
<div class="card border-dark">
<div class="row row-cols-2 gutter-0">
<div class="col-7 pe-0" style="height: 526px;">
<div class="card-body border-end border-dark">
<div class="embed-responsive embed-responsive-16by9">
{% video posts.video '720x490' %}
</div>
</div>
</div>
<div class="col-5 ps-0" style="height: 526px;">
<div class="card-body bg-light" style="height: 526px;">
{% for comment in posts.comments.all %}
<a class="h5 text-dark text-decoration-none me-5" href="#"> {{ comment.author }}</a>
<span class="ms-3" >{{ comment.date_posted|date:"F d, Y" }}</span>
<p class="card-text">{{ comment.body }} </p>
{% endfor %}
</div>
{% csrf_token %}
{% crispy form %}
</div>
<div class="col-7 pe-0" >
<div class="card-body border-top border-end border-dark">
<h5><a class="fw-bold text-dark text-decoration-none" href="#">{{ posts.title }}</a></h5>
<p class="card-text">{{ posts.content }}</p>
</div>
<div class="card-body border-end border-dark" >
<span class="text-muted text-decoration-none">Posted by <a class="text-muted" href="#"> {{ posts.author }}</a> </span>
<span class="ms-3 text-muted" >{{ posts.date_posted|date:"F d, Y" }}</span>
</div>
</div>
</div>
</div>
</div>
{% endblock body %}
Use FormView not DetailView as your base class. I expect it'll all then work.
An invaluable resource is Classy Class-based views. It shows that DetailView does not have a POST handler (method) and that FormMixin does not provide that either, which is probably why what you have is not working.
POST handling is defined by FormView itself.
I am trying to create a gym workout basic app in Django, and wish to have my workouts displayed as a list on my home page, using Django's built-in list view. For some reason, my home page template is rendering, with the proper headers and nav bar, but my list is not showing. Here is my code:
Models.py
from django.db import models
STATUS = (
(0,"Draft"),
(1,"Publish")
)
class Workout(models.Model):
workout_number = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
created_on = models.DateTimeField(auto_now_add=True)
content = models.TextField()
description = models.TextField(blank=True, default='')
time_cap = models.TimeField(auto_now=False, auto_now_add=False, blank=True)
rounds = models.IntegerField(blank=True, default='')
weight = models.FloatField(blank=True, default='')
status = models.IntegerField(choices=STATUS, default=0)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.name
Views.py
from django.shortcuts import render
from django.views import generic
from .models import Workout
class WorkoutList(generic.ListView):
model = Workout
template_name = 'home.html'
context_object_name = 'workout'
queryset = Workout.objects.filter(status=1).order_by('-created_on')
Template home.html (part of)
<div class="container">
<div class="row">
<div class="col-md-8 mt-3 left">
{% for workout in workout_list %}
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title">{{ workout.name }}</h2>
<p class="card-text text-muted h6">{{ workout.created_on}} </p>
<p class="card-text">{{workout.content|slice:":200" }}</p>
</div>
</div>
{% endfor %}
</div>
</div>
Workout/Urls.py
from django.urls import path
from . import views
app_name='workout'
urlpatterns = [
path('', views.WorkoutList.as_view(), name='home'),
]
Since the context_object_name is 'workout', this means that the list of objects is passed as workout, but you iterate over {% for workout in workout_list %}. You should change this to:
from django.shortcuts import render
from django.views import generic
from .models import Workout
class WorkoutList(generic.ListView):
model = Workout
template_name = 'home.html'
context_object_name = 'workout_list'
queryset = Workout.objects.filter(status=1).order_by('-created_on')
I’m making a blog site. And I have been trying to render from a model, RecentPosts, objects rpost1, etc. in my template blogs -file base.html which is my blog's details page.
But it is not rendering. Is there a solution?
File views.py
from django.http import HttpResponse
from django.shortcuts import render
from blog.models import Post
from django.contrib import messages
from django.views.generic import ListView, DetailView
from .models import Post, RecentPosts, Comment, Contact, Populars
from datetime import datetime
from .forms import CommentForm
def index(request):
return render(request, 'index.html')
def path(request):
return render(request, 'blog.html')
def cv(request):
return render(request, 'cv.html')
class HomeView(ListView):
model = Post
template_name = 'listview.html'
class ArticleDetailView(DetailView):
model = Post
template_name = 'base2.html'
def rpost(request):
template_name = "blogs-base.html"
rpost = RecentPosts.objects.all()
context = {'rpost': 'rpost'}
return render(request, template_name, context)
This is the URL portion.
File urls.py
from django.urls import path
from . import views
from .views import HomeView, ArticleDetailView, contact, Comment_page, popular
urlpatterns = [
path('', views.index, name='index'),
path('index.html', views.index, name='index'),
path('cv.html', views.cv, name='cv'),
path('blogs/', views.HomeView.as_view(), name="blogs"),
path('blogs/<slug:slug>', ArticleDetailView.as_view(), name='base2'),
path('contact/', views.contact, name='contact'),
path('contact.html/', views.contact, name='contact'),
path('comment.html/', views.Comment_page, name='comment'),
path('popular', views.popular, name='popular'),
]
This is the model portion.
File models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils.timezone import now
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=120)
author_name = models.CharField(max_length=120)
author_description = models.CharField(max_length=120)
body = models.TextField(default='')
slug = models.CharField(max_length = 130)
timestamp = models.DateTimeField(auto_now=True)
def get_abosolute_url(self):
return reverse()
class Comment(models.Model):
post = models.ForeignKey('Post', on_delete=models.CASCADE, related_name='comments')
user = models.CharField(max_length=200)
message = models.TextField()
timestamp = models.DateTimeField(auto_now=True)
def __str__(self):
return self.user.capitalize() + ' : ' + self.message[0:16] + '........'
class Contact(models.Model):
email = models.CharField(max_length=20)
message = models.CharField(max_length=200)
models.CharField(max_length=200)
class RecentPosts(models.Model):
rpost1 = models.CharField(max_length=120)
rpost2 = models.CharField(max_length=120)
rpost3 = models.CharField(max_length=120
)
File blogs-base.html
<a href="marketing-single.html" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="w-100 justify-content-between">
<img src="assets/blog-uploads/ad.png" alt="" class="img-fluid float-left">
{% for i in rpost %} <h5 class="mb1">{{i.rpost1}}</h5>
<small>12 Jan, 2016</small>{% endfor %}
</div>
</a>
This is the listview where I’m posting only my blog's thumbnails.
File listview.html
{% extends 'blogs-base.html' %}
{% load static %}
{% block title %}{{post.title}}{% endblock %}
{% block content %}
{% for post in object_list %}
<div class="col-lg-6">
<div class="blog-box">
<div class="post-media">
<a href="marketing-single.html" title="">
<img src="{% static 'website/assets/blog-uploads/ad.png' %}" alt="" class="img-fluid">
<div class="hovereffect">
<span class=""></span>
</div><!-- end hover -->
</a>
</div><!-- end media -->
<div class="blog-meta">
<h4>{{post.title}}</h4>
<small></small>
<small>20 July, 2017</small><br><br>
</div><!-- end meta -->
</div><!-- end blog-box -->
</div><!-- end col -->
{% endfor %}
{% endblock %}
You made a mistake in your context. You're are passing a string of 'rpost' instead of
context = { "rpost" : rpost }