UPDATE #4
Issue: I've been having trouble have content appear on my detailed page view, specifically, this content is in two separate html files called slider.html and sidebar.html, which are being pulled in using {% include %} in Django.
Status: Issue still unresolved, last update on Thurs. Dec 18, 11 a.m.
I've made changes to my models.py and my detailed.html
Yes, the detailed.html page does have empty divs with classes on them
I've included my views.py in the main post.
views.py
from django.views import generic
from . import models
from .models import FullArticle
# Create your views here.
class BlogIndex(generic.ListView):
queryset = models.FullArticle.objects.published()
template_name = "list.html"
randomArticle = FullArticle.objects.order_by('?').first()
class BlogDetail(generic.DetailView):
model = models.FullArticle
template_name = "detailed.html"
models.py
from django.db import models
from django.core.urlresolvers import reverse
# Create your models here.
class FullArticleQuerySet(models.QuerySet):
def published(self):
return self.filter(publish=True)
class FullArticle(models.Model):
title = models.CharField(max_length=150)
author = models.CharField(max_length=150)
slug = models.SlugField(max_length=200, unique=True)
pubDate = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
category = models.CharField(max_length=150)
heroImage = models.CharField(max_length=250, blank=True)
relatedImage = models.CharField(max_length=250, blank=True)
body = models.TextField()
publish = models.BooleanField(default=True)
gameRank = models.CharField(max_length=150, blank=True, null=True)
objects = FullArticleQuerySet.as_manager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("FullArticle_detailed", kwargs={"slug": self.slug})
def get_context_data(self, **kwargs):
context = super(BlogDetail, self).get_context_data(**kwargs)
context['object_list'] = models.FullArticle.objects.published()
return context
class Meta:
verbose_name = "Blog entry"
verbose_name_plural = "Blog Entries"
ordering = ["-pubDate"]
detailed.html
<!-- This grabs the sidebar snippet -->
{% include "sidebar.html" with object_list=object_list %}
<div class="mainContent clearfix">
<h1>{{object.title}}</h1>
<p class="date">{{object.pubDate|date:"l, F j, Y" }}</p> | <p class="author">{{object.author}}</p>
<img src="{{object.heroImage}}" alt="" class="largeImage">
<div class="contentBlock">
<img src="{{object.relatedImage}}" alt="" class="relatedImage">
<p class="content">{{object.body|linebreaks}}</p>
</div><!-- /.contentBlock -->
<!-- This grabs the slider snippet -->
{% include "slider.html" with object_list=object_list%}
</div><!-- /.mainContent -->
sidebar.html
<div class="navContent">
{% for article in object_list|slice:":5" %}
<div class="navItem">
<div class="overlay">
</div><!-- /.overlay -->
<img src="{{article.relatedImage}}" alt="" class="navPicture">
<p class="navTitle">{{ article.title|truncatewords:"4" }}</p>
</div><!-- /.navItem -->
{% endfor %}
</div><!-- /.navContent -->
urls.py (which relates to the app called blog)
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns(
'',
url(r'^$', views.BlogIndex.as_view(), name="list"),
url(r'^(?P<slug>\S+)', views.BlogDetail.as_view(), name="detailed"),
)
The get_context_data method belongs in your view. If you move it there, your sidebar should work.
However, there is a much cleaner way to achieve this by using a context processor. Context processors allow you to make certain data available to all of your templates, no matter where they are.
Create context_processors.py in your project module directory, and make it look something like this:
# myproject/myproject/context_processors.py
from myapp.models import FullArticle
def sidebar_articles(request):
"""Return the 5 most recent articles for the sidebar."""
articles = FullArticle.objects.all()[:5]
return {'SIDEBAR_ARTICLES': articles}
You will have to enable the context processor in your settings.py. Do this by adding the TEMPLATE_CONTEXT_PROCESSORS default setting, and appending your new context processor to the bottom. Like this:
# settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"myproject.context_processors.sidebar_articles",
)
Now you will be able to reference SIDEBAR_ARTICLES from any of your templates. Your sidebar.html could be rewritten as:
<!-- myapp/templates/detailed.html -->
<div>
{% for article in SIDEBAR_ARTICLES %}
<h1>{{ article.title }}</h1>
and so on ...
{% endfor %}
</div>
I believe it is necessary to pass the right arguments to your included template. See https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include for more documentation.
Template will probally look like: {% include "sidebar.html" with article=object %}
Update 1
After reading the docs about detailview, my following suggestions
(https://docs.djangoproject.com/en/1.7/ref/class-based-views/generic-display/#detailview) It appears your context only contains a object. So we have to pass this to this included slider template.
{% include "slider.html" with article=object %}
The problem within your sidebar template is that you don't have an object_list within your context. You can add this to your DetailView as shown in the example. So the method would look like.
def get_context_data(self, **kwargs):
context = super(BlogDetail, self).get_context_data(**kwargs)
context['object_list'] = models.FullArticle.objects.published()
return context
And the include of the template should look like.
{% include "sidebar.html" with object_list=object_list %}
Take care you are passing correctly your context to template. But probably you have to add spaces between '{{' bracket and variable name. Ex. {{ article.title }}
Related
I'm building an instagram-ish clone in Django. I have the basic functionality working, where a user can post an image, and this is displayed on the main page. I would like to make a 'user page' that only displays posts from a user. For example, example.com/foobar would only display posts from the user 'foobar'.
I believe i have the urls.py and template working correctly, but I can not figure out how to only iterate through items and pull out ones of a specific user. I realize this should be a queryset of some kind, but other than that I'm stumped. Should this be its own class, or could I extend the existing PostList class to pull out a single author's posts?
post_detail.html - gets all the images stored in the database, this works fine.
{% for post in object_list %}
<td><img src="{{ post.image.url }}" width="300"></td>
{% if forloop.counter|modulo:4 %}
</tr><tr>
{% endif %}
{% endfor %}
profile.html - shows all posts from a user (as in example.com/foobar)
<table>
<tr>
{% for post in object_list %}
<td><img src="{{ post.image.url }}" width="300"></td>
{% if forloop.counter|modulo:4 %}
</tr><tr>
{% endif %}
{% endfor %}
</tr>
</table>
urls.py - I believe this works correctly.
urlpatterns = [
path('admin/', admin.site.urls),
path('', PostList.as_view(), name='list'),
path('<str:username>/', Profile.as_view(), name='user_profile'),
views.py:
from posts.models import Post
class PostList(ListView):
ordering = ['-created']
paginate_by = 12
model = Post
class Profile(ListView):
template_name = 'posts/profile.html'
UserName = self.kwargs.get("username")
queryset = PostList.queryset
.filter(author = UserName)
return queryset
models.py:
class Post(models.Model):
image = models.ImageField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
You can override get_queryset():
class ProfileView(ListView):
template_name = 'posts/profile.html'
model = Post
def get_queryset(self, **kwargs):
return super().get_queryset(**kwargs).filter(
author__username=self.kwargs['username']
)
Note: In Django, class-based views (CBV) often have a …View suffix, to avoid a clash with the model names.
Therefore you might consider renaming the view class to ProfileView, instead of Profile.
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 am trying to create a cartoon streaming website in which when a user clicks on their cartoon of choice(Eg:Pokemon), they get to see the seasons as a list as well as the detail of the cartoons.
from django.db import models
class Cartoon(models.Model):
name = models.CharField(max_length=200)
cover = models.ImageField()
description = models.TextField()
start_date = models.CharField(max_length=50)
end_date = models.CharField(max_length=50)
def __str__(self):
return self.name
class CartoonSeason(models.Model):
cartoon = models.ForeignKey(Cartoon, null=True, on_delete=models.SET_NULL)
number = models.IntegerField()
season_cover = models.ImageField(blank=True, null=False)
season_name = models.CharField(max_length=200, blank=True, null=True)
season_description = models.TextField(blank=True, null=False)
Here I have linked the Cartoon model with the CartoonSeason model using a Foreign Key so when a new season is to be added, it automatically gets linked with the corresponding Cartoon
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import ListView, DetailView
from .models import CartoonSeason, Cartoon
class CartoonListView(ListView):
model = Cartoon
template_name = "index.html"
class CartoonSeasonView(DetailView):
queryset = CartoonSeason.objects.filter()
model = CartoonSeason
template_name = "season.html"
I am using a detail view to display the CartoonSeason model so that it displays the Cartoons details as well, but when I try to load the seasons, it only displays the season with the Primary Key of 1. I want it to display all of the seasons added to the cartoon.
Here's my seasons.html
{% extends 'base.html' %}
{% block title %}
test
{% endblock %}
{% block content %}
<main>
<section class="cartoon-description">
<div class="season_head">
<img src="{{object.cartoon.cover.url}}" width="260px" alt="">
<div class="cartoon-name">
<h1>{{object.cartoon.name}}</h1>
<small >{{object.cartoon.start_date}} - {{object.cartoon.end_date}}</small>
<br>
<div class="description">
<strong>Description:</strong>
<br>
<p>{{object.cartoon.description}}</p>
</div>
</div>
</div>
</section>
<hr>
</main>
{% endblock %}
Here is My urls.py
from .views import CartoonListView, CartoonSeasonView
urlpatterns = [
path('', CartoonListView.as_view(), name="home"),
path('cartoon/<int:pk>' , CartoonSeasonView.as_view(), name="cartoon"),
]
This is my main template
{% extends 'base.html'%}
{% block title %}
Home - CartoonsPalace
{% endblock %}
{% block content %}
<main>
<section class="cartoon-list">
<div class="select-text">
<h1>Pick Your Cartoon Series of Choice:-</h1>
</div>
{% for cartoon in object_list %}
<div class="list">
<a href="{% url 'cartoon' cartoon.pk %}"><div class="list-object">
<img src="{{cartoon.cover.url}}" alt="" width="184px">
<h3>{{cartoon.name}}</h3>
<span>{{cartoon.start_date}} - {{cartoon.end_date}}</span>
</div>
</a>
</div>
{% endfor %}
</section>
</main>
{% endblock %}
Any help would be appreciated.
Ok when you want to access all the objects related to another objects through foreign key , you have two simple options :
1.set query using the cartoon(reverse relation):
cartoon = Cartoon.objects.get(id = 'pk') # get the pk by passing in url
cartoon_season = cartoon.cartoonseason_set.all()
# object.(name of model CartoonSeason must be in lowercase )._set.all()
or set query using the CartoonSeason(recommended):
#before this you must have the id of the cartoon :
cartoon = Cartoon.objects.get(id = pk)
seasons = CartoonSeason.objects.filter(cartoon = cartoon)
I dont know about you cartoon template or urls, but I assume in you cartoon template where you show your cartoon detail you have a link to the season of the cartoon ,so there you should pass the id of the cartoon : {{cartoon.id}}
and in you urls you get this id to use in your season detail :
but note that when you are using DetailView and want to use that id passing by url you shoud define a get_queryset like this :
class CartoonSeasonView(DetailView):
model = CartoonSeason
template_name = "season.html"
def get_queryset(self, *args, **kwargs):
#before this you must have the id of the cartoon :
cartoon = Cartoon.objects.get(id = self.kwargs['pk'])
seasons = CartoonSeason.objects.filter(cartoon = cartoon)
and remember no need to user have query_set anymore.
by this you can show the name of the cartoon or the poster in the cartoonseason page.
and remember we user detail view in order to show detials of only one object not 7 ( like season of a cartoon ).You can use ListView or if you want to show different data in one template use TemplateView.
I have created a website with djangoCMS and make heavy use of apphooks, cms plugins, wizards, etc. We have a simple app with just one model holding the core data that should be displayed on the homepage.
models.py
from django.db import models
from django.utils.text import slugify
from django.urls import reverse
from cms.models.fields import PlaceholderField
from djangocms_text_ckeditor.fields import HTMLField
class Programme(models.Model):
name = models.CharField(max_length=60, unique=True)
slug = models.SlugField()
icon = models.CharField(max_length=50, unique=True)
introduction = HTMLField()
overview = PlaceholderField(
'programme_overview',
related_name='programmes_overview'
)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse(
'programmes:programme-detail',
kwargs={'slug': self.slug}
)
def save(self, *args, **kwargs):
if not self.pk:
self.slug = slugify(self.name)
super(Programme, self).save(*args, **kwargs)
I decided to create a custom inclusion templatetag for this purpose.
templatetags/programmes_tags
from django import template
from ..models import Programme
register = template.Library()
#register.inclusion_tag('programmes/programme_list.html')
def programme_list():
programmes = Programme.objects.all()
return {'programmes': programmes}
In the template I use render_model from the cms_tags, because the editors should be able to edit the contents in the frontend. Here is the template:
templates/programmes/programme_list.html
{% load cms_tags %}
{% for programme in programmes %}
<div class="col-lg-2 col-md-4 col-sm-6 col-xs-12 text-center flex-item">
<div class="service-box">
<i class="fa fa-4x {{ programme.icon }} text-primary" style="visibility:visible;"></i>
<h3>
<a href="{% url 'programmes:programme-detail' programme.slug %}">
{{ programme.name }}
</a>
</h3>
<p class="text-muted">
{% render_model programme 'introduction' %}
</p>
</div>
</div>
{% endfor %}
The tag is now used in the template for the homepage:
{% load programmes_tags %}
{% programme_list %}
When I open the homepage it throws an error:
KeyError: 'request'
Obviously the render_model tag needs to access the request. When I try to change the templatetag like this:
#register.inclusion_tag('programmes/programme_list.html', takes_context=True)
def programme_list(context):
programmes = Programme.objects.all()
context.update({'programmes': programmes})
return context
the request is passed as part of the RequestContext, but then this error is thrown:
ValueError: dictionary update sequence element #1 has length 1; 2 is required
How should I properly update the RequestContext?
If using a templatetag is not the right approach, please point out what would be a better solution.
Let takes_context be there and use your old code. Haven't tested this.
#register.inclusion_tag('programmes/programme_list.html', takes_context=True)
def programme_list(context):
request = context['request']
programmes = Programme.objects.all()
return {'programmes': programmes, 'request': request}
I am making a django blog and want to show a list of comments for each blog post, but I have trouble figuring out how to reference the comments in the views and the templates.
My models are defined like this:
class Issue(models.Model):
title = models.CharField(max_length=255)
text = models.TextField()
author = models.ForeignKey(User)
def __unicode__(self):
return self.title
class Comment(models.Model):
commenter = models.ForeignKey(User)
issue = models.ForeignKey(Issue)
text = models.TextField()
and my views like this
class IssueDetail(DetailView):
model = Issue
context_object_name = "issue"
template_name = "issue_detail.html"
def get_context_data(self, **kwargs):
context = super(IssueDetail, self).get_context_data(**kwargs)
context['comments'] = Comment.objects.all()
return context
class CommentDetail(DetailView):
model = Comment
context_object_name = "comment"
template_name = "comment_detail.html"
and finally the issue_detail.html template
{% block content %}
<h2>{{ issue.title }}</h2>
<br/>
<i>As written by {{ issue.author.first_name }}</i>
<br/><br/>
<blockquote> {{ issue.text }}</blockquote>
<h3>Comments</h3>
{% for comment in comments %}
<li>{{comment}}</li>
{% endfor %}
{% endblock %}
This allows me to reference the fields of the comment inside the Issue template, but basically then I want the comments to have a template of their own that will be rendered inside the for loop. What is the correct way to do this in Django?
The comments are already available in your template because of the model relationship you defined. You can delete the get_context_data in IssueDetail.
Your issue_detail.html template could look like this:
{% for comment in issue.comment_set.all %}
{% include 'comment_detail.html' %}
{% endfor %}
Your comment_detail.html template could look like this:
<ul>
<li>{{ comment.issue }}</li>
<li>{{ comment.text }}</li>
</ul>
what if this we were using a different model
product = models.ForeignKey(Customer)
how would we do the CRUD opertions from the templates an the views.py