I have list of posts on my homepage, which are links to post DetailView.
Links works correctly, but when i go to DetailView, list is no more visible - i have to go back to home page to choose other post. Element which is showing posts list is located on base.html, correctly passes objects list to home.html, and in post_detail.html list is empty.
Homepage looks like this
Post DetailView looks like this - posts list is no more visible
Inspect view on homepage
Inepect view on DetailView - ul element is empty
base.html
<body>
<div>
<ul>
<li>
home
</li>
<li>
<a> items</a>
<ul>
{% for post in posts %}
<li>
{{ post.title }}
</li>
{% endfor %}
</ul>
</li>
</ul>
</div>
<di>
{% block content %}
{% endblock content %}
</div>
</body>
home.html
{% extends "blog/base.html" %}
{% block content %}
{% for post in posts %}
<div>
{{post.title}}
<p>{{post.content}}</p>
<p>{{post.date_posted}}</p>
</div>
{% endfor %}
{% endblock content %}
post_detail.html
{% extends "blog/base.html" %}
{% block content %}
<div>
{{object.title}}
<p>{{object.content}}</p>
</div>
{% endblock content %}
urls.py
from django.urls import path
from .views import PostListView, PostDetailView
from . import views
urlpatterns = [
path('', PostListView.as_view(), name='blog-home'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
]
views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
class PostDetailView(DetailView):
model = Post
models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
def __str__(self):
return self.title
You have to override the context of your DetailView which display only the data of current instance.
class PostDetailView(DetailView):
model = Post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['other_posts'] = Post.objects.all()
return context
Related
`I'm working on the code in chapter 1 of the django by example. Followed all instructions and reviewed many feedbacks on this site and other areas but not to my luck. I had earlier attempted get_absolute_url and return reverse method unsuccessfully. I was trying an instructed approach and land exactly the same issues in listview and detailview. I'm a beginner so I guess I'm missing something may be fundamental. Is there any version dependency. I've installed latest django and python. Need advice and thanks for any help!
This is the error message
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')
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_date')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
body = models.TextField()
publish_date = models.DateTimeField(default=timezone.now)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager() # default Manager
published = PublishedManager() # our custom manager
class Meta:
ordering = ['-publish_date',]
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse(
'blog:post_detail',
args=[
self.publish_date.year,
self.publish_date.month,
self.publish_date.day,
self.slug,
]
)
views.py
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.published.all()
return render(request, 'blog/post/post_list.html', {'posts':posts})
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish_date__year=year,
publish_date__month=month,
publish_date__day=day)
return render(request, 'blog/post/post_detail.html',{'post':post})
urls.py ( For app)
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
#post views
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int=day>/<slug=post>/',
views.post_detail,
name='post_detail'),
]
admin.py
from django.contrib import admin
from .models import Post
#admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish_date', 'status')
list_filter = ('status', 'created_date','publish_date', 'author')
search_fields = ('title', 'body')
prepopulated_fields = {'slug':('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish_date'
ordering = ('status', 'publish_date')
post_detail.html
{% extends "blog/base.html" %}
{% block title %}{{post.title}}{% endblock %}
{% block content %}
<h> {{post.title}} </h>
<p class="date">
Published {{ post.publish_date }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{%endblock%}
post_list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{% url 'blog:post_list' post.title %}"
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish_date }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
Modified post_list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
{% url 'blog:post_detail' post.publish_date.year post.publish_date.month post.publish_date.day post.publish_date.slug as post.title %}
<a href="{{ post.title }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish_date }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
in your post_list.html
change this
<a href="{% url 'blog:post_list' post.title %}"
to
<a href="{% url 'blog:post_list'%}"
your url doesn't accept an argument but you are passing an argument from your template.
I am learning django, I added a new app,
the link can be displayed in friendly_link.html,
the code is as follows:
admin.py
from django.contrib import admin
from .models import FriendlyLink
admin.site.register(FriendlyLink)
models.py
from django.db import models
class FriendlyLink(models.Model):
title = models.CharField(max_length=100, default='', verbose_name='title')
url = models.CharField(max_length=100, default='', verbose_name='url')
class Meta:
verbose_name_plural = 'links'
def __str__(self):
return self.title
views.py
from django.shortcuts import render
from .models import FriendlyLink
def friendly_link(request):
friendly_link = FriendlyLink.objects.order_by('title')
context = {'friendly_link': friendly_link}
return render(request, 'friendly_link.html', context)
urls.py
from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^links$', views.friendly_link, name='friendly_link'),
]
friendly_link.html
{% extends "base.html" %}
{% block title %}Links{% endblock %}
{% block content %}
<ul>
{% for link in friendly_link %}
<li>{{ link.title }}</li>
{% empty %}
{% endfor %}
</ul>
{% endblock %}
base.html
{% load i18n static %}<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
...
{% block content %}{% endblock %}
<footer>
<ul>
<!--This code doesn't work-->
{% for link in friendly_link %}
<li>{{ link.title }}</li>
{% empty %}
{% endfor %}
</ul>
</footer>
...
</body>
I want to put the link inside the <footer> element in base.html. How do I change the code? Thank you.
You should really read this documentation page:
https://django-adminlte2.readthedocs.io/en/latest/templates_and_blocks.html
Each block is clickable there and there's a detailed description on how to overwrite each one.
For your problem, you will probably nav_footer
{% block nav_footer %}
{{ block.super }}
...
{% endblock %}
{{ block.super }} is optional and will append your content to the block instead of overwriting it.
I am following "django by example"
and i met the problem but i don't know what causes it.
The following is the error page:
NoReverseMatch at /blog/
Reverse for 'post_detail' with arguments '('', '', '')' not found. 1 pattern(s) tried: ['blog/(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/$']
Request Method:
GET
Request URL:
http://127.0.0.1:8000/blog/
Django Version:
1.11
Exception Type:
NoReverseMatch
Exception Value:
Reverse for 'post_detail' with arguments '('', '', '')' not found. 1 pattern(s) tried: ['blog/(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/$']
Exception Location:
E:\workspace\pycharm\djangobyexample\mysite\env\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable:
E:\workspace\pycharm\djangobyexample\mysite\env\Scripts\python.exe
Python Version:
3.5.2
Python Path:
['E:\workspace\pycharm\djangobyexample\mysite',
'E:\workspace\pycharm\djangobyexample\mysite\env\Scripts\python35.zip',
'E:\workspace\pycharm\djangobyexample\mysite\env\DLLs',
'E:\workspace\pycharm\djangobyexample\mysite\env\lib',
'E:\workspace\pycharm\djangobyexample\mysite\env\Scripts',
'c:\users\richard\appdata\local\programs\python\python35\Lib',
'c:\users\richard\appdata\local\programs\python\python35\DLLs',
'E:\workspace\pycharm\djangobyexample\mysite\env',
'E:\workspace\pycharm\djangobyexample\mysite\env\lib\site-packages']
Main URLConfiguration
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')),
]
blog/url.py
from django.conf.urls import url
from . import views
urlpatterns = [
# post views
# url(r'^$', views.post_list, name='post_list'),
url(r'^$', views.PostListView.as_view(), name='post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$',
views.post_detail,
name='post_detail'),
#url(r'^(?P<post_id>\d+)/share/$', views.post_share, name='post_share'),
]
views.py
from django.shortcuts import render, get_object_or_404
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
from .forms import EmailPostForm
from django.core.mail import send_mail
# Create your views here.
class PostListView(ListView):
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request, 'blog/post/detail.html', {'post': post})
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
class PublishedManager(models.Manager):
def get_query(self):
return super(PublishedManager, self).get_query().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = {
('draft', 'Draft'),
('published', 'Published'),
}
title = models.CharField(max_length=250, primary_key=True)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User, related_name='blog_post')
body = models.TextField()
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:
# Telling django to sort results by the publish field in descending order by default when we query the database
ordering = ('-publish',)
def __str__(self):
return self.title
objects = models.Manager()
published = PublishedManager()
def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
detail.html
{% extends "blog/base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% include "pagination.html " with page=page_obj %}
{% endblock %}
base.html
{% load staticfiles %}
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
<div id="sidebar">
<h2>My blog</h2>
<p>This is my blog.</p>
</div>
</body>
</html>
This line is giving you the error, because the arguments are invalid.
<!-- <a href="{% url 'blog:post_detail' post.year post.month post.day %}">-->
The post does not have year, month and day attributes -- they are attributes of post.publish.
You are already using {{ post.get_absolute_url }} on the next line of the template to get the url. Since the url tag line is inside an html comment <!-- -->, the easiest fix is to simply delete the line.
I made a slug field and I made migrations. My slug field is called slug and it is visible in in the site administration, but it doesn't work properly.
My browser tells me:
TypeError at /None/
detail() got an unexpected keyword argument 'slug'
models.py
from django.db import models
class Question(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(null=True, blank=True)
description = models.CharField(max_length=200)
data_published = models.DateTimeField('date published')
large_description = models.TextField(max_length=20000)
def __str__(self):
return self.title
def save(self):
if not self.id:
self.s = slugify(self.q)
super(test, self).save()
def was_published_recently(self):
return self.datum_objave >= timezone.now() - datetime.timedelta(days=1)
urls.py
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'books.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<slug>[\w_-]+)/$', 'books.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
index.html
{% extends "master.html" %}
{% block h1 %}
<div class="box first">
<div class="row">
<div class="container">
{% for question in latest_question_list %}
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="center">
<h4>{{ question.title }} </h4>
<p>{{ question.description }}</p>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
{% block title %} Index {% endblock %}
admin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
question_fields = {"slug": ("title",)}
admin.site.register(Question, QuestionAdmin)
views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all()
context = {'latest_question_list': latest_question_list}
return render(request, 'papers/index.html', context)
def detail(request, book_title):
question = Question.objects.get(title=book_title)
return render(request, 'books/detail.html', {'question': question})`
detail.html
{% extends "master2.html" %}
{% block h1 %}
<div class="center">
<h4>{{question.title}} </h4>
<p>{{question.large_description}}</p>
</div>
{% endblock %}
{% block title %} Detail {% endblock %}
This part of your urls.py, (?P<slug>[\w_-]+), means that there is a view that accepts a parameter named slug and if Django is unable to find that view, it will complain. So your view should probably be something like this:
def detail(request, slug):
question = Question.objects.get(slug=slug)
return render(request, 'papers/detail.html', {'question': question})
UPDATE
It's also a good idea not to create your urls manually in your templates:
{{ question.naslov }}
I am making a simple blog app in Django as a learning exercise. I am able to add posts, see them all on the front page, so it is working so far. Where I am having an issue is creating a view that shows the whole post on a separate page. I want to click the title, and go to a page at the url /post/primary key that has the title and body. When I click on the link, I get a page with just the base.html. Not sure where I am missing anything, here are urls.py, views.py, and post.html:
urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('blog.views',
url(r'^$', 'frontpage'),
url(r'^post/(\d+)/$', 'post'),
)
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
)
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render_to_response
from blog.models import *
views.py
def frontpage(request):
posts = Post.objects.all().order_by("-created")
paginator = Paginator(posts, 5)
page = request.GET.get('page', '1')
try:
posts = paginator.page(page)
except (InvalidPage, EmptyPage):
posts = paginator.page(paginator.num_pages)
return render_to_response("list.html", dict(posts=posts, user=request.user))
def post(request, pk):
"""Single Post"""
post = Post.objects.get(pk = pk)
d = dict(post=post, user=request.user)
return render_to_response("post.html", d)
post.html
{% extends "base.html" %}
{% block content %}
<div class ="main">
<ul>
{% for post in posts.object_list %}
<div class = "title">{{ post.title }}</div>
<ul>
<div class="time"> {{ post.created }}</div>
<div class ="body"> {{ post.body|linebreaks }}</div>
</ul>
{% endfor %}
</ul>
</div>
{% endblock %}
Thanks in advance for your help.
I am assuming the page.html is actually the post.html you have in yoru codes sample???You no longer have a collection of posts but instead just have 1 post
This needs to change from: (which is looping through your posts)
{% extends "base.html" %}
{% block content %}
<div class ="main">
<ul>
{% for post in posts.object_list %}
<div class = "title">{{ post.title }}</div>
<ul>
<div class="time"> {{ post.created }}</div>
<div class ="body"> {{ post.body|linebreaks }}</div>
</ul>
{% endfor %}
</ul>
</div>
{% endblock %}
To something like (which just displays your single post):
{% extends "base.html" %}
{% block content %}
<div class ="main">
{{ post.title }}
{{ post.created }}
{{ post.body }}
</div>
{% endblock %}
You have to change urls.py to go to a page at the url /post/primary key.
urlpatterns = patterns('blog.views',
url(r'^$', 'frontpage'),
url(r'^post/(?P<pk>\d+)/$', 'post'),
)