I am doing a training project and have encountered a problem - django

I am doing a training project and have encountered a problem. I try to display the "events" block and the "news" block on the main page, but when I run the loop cycle, only one block is displayed, and the second is not displayed. Explain who knows what I'm doing wrong. I have been solving this problem for three days now. Thanks in advance who will respond to help
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if post %}
{% for post in post %}
{{ post.title }}
{% endfor %}
{% else %}
<p>У вас нет материала</p>
{% endif %}
{% if event %}
{% for event in event %}
{{ event.name }}
{% endfor %}
{% else %}
<p>У вас нет материала</p>
{% endif %}
</body>
</html>
Views:
from django.shortcuts import get_object_or_404, render
from django.views.generic.base import View
from .models import Blog, Event
# Create your views here.
class EventView(View):
def get(self, request):
event = Event.objects.all()
return render(request, "home/home_list.html", {"event": event})
class BlogView(View):
def get(self, request):
post = Blog.objects.all()
return render(request, "home/home_list.html", {"post": post})
Urls:
from django.urls import path
from . import views
urlpatterns = [
path("", views.EventView.as_view()),
path("", views.BlogView.as_view())
]
Models:
from django.db import models
from datetime import date
# Create your models here.
class Blog(models.Model):
"""Новости"""
title = models.CharField("Заголовок", max_length=100)
description = models.TextField("Описание")
descriptionTwo = models.TextField("Описание (второй абзац)", blank=True, default="")
blockquote = models.TextField("Цитата", blank=True, default="")
short = models.TextField("Краткое описание", max_length=300)
poster = models.ImageField("Изображение", upload_to="post/")
prewiew = models.ImageField("Превью", upload_to="post/")
dataPost = models.DateField("Дата публикации", default=date.today)
url = models.SlugField(max_length=160, unique=True, blank=True)
draft = models.BooleanField("Черновик", default=False)
def __str__(self):
return self.title
class Meta:
verbose_name = "Новости"
verbose_name_plural = "Новости"
class Event(models.Model):
"""События"""
name = models.CharField("Заголовок", max_length=100)
adress = models.TextField("Адрес")
description = models.TextField("Описание")
short = models.TextField("Краткое описание")
phone = models.TextField("Контактный номер телефона")
email = models.EmailField()
image = models.ImageField("Изображение", upload_to="event/")
dataStart = models.DateField("Дата старта", default=date.today)
def __str__(self):
return self.name
class Meta:
verbose_name = "Событие"
verbose_name_plural = "События"
Admin:
from django.contrib import admin
from .models import Blog, Event
# Register your models here.
admin.site.register(Event)
admin.site.register(Blog)

Views
You need one view with a context containing both the events ant the posts
class EventView(View):
def get(self, request):
events = Event.objects.all()
posts = Blog.objects.all()
return render(request, "home/home_list.html", {"events": events, "posts":posts})
Template
In the template loop over both
{% if posts %}
{% for p in posts %}
{{ p.title }}
{% endfor %}
{% else %}
<p>У вас нет материала</p>
{% endif %}
{% if events %}
{% for e in events %}
{{ e.name }}
{% endfor %}
{% else %}
<p>У вас нет материала</p>
{% endif %}

In for each loop, use different name for the "loop Object" and the list/dictionary on which you are looping. LIKE THIS:
{% if post %}
{% for p in post %}
{{ p.title }}
{% endfor %}
{% else %}
<p>У вас нет материала</p>
{% endif %}
{% if event %}
{% for e in event %}
{{ e.name }}
{% endfor %}
{% else %}
<p>У вас нет материала</p>
{% endif %}
Did it solve your problem?

Related

Django Mptt : How to nest another Model

I have two Models, reseller and customer. I can generate Tree hierarchy for reseller, But I want to list down Customers under their immediate parent reseller.
models.py
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
# Create your models here.
class Reseller(MPTTModel):
reseller_name = models.CharField(max_length=40)
reseller_email = models.EmailField(max_length=70,blank=True)
reseller_code = models.CharField(max_length=40)
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
class MPTTMeta:
order_insertion_by = ['reseller_name']
def __str__(self):
return self.reseller_name
class Customer(models.Model):
customer_name = models.CharField(max_length=40)
customer_email = models.EmailField(max_length=70,blank=True)
customer_code = models.CharField(max_length=40)
reseller = models.ForeignKey(Reseller, on_delete=models.CASCADE, null=True, blank=True, related_name='cust_children')
def __str__(self):
return self.customer_name
This is the view :
views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import *
from .forms import *
# Create your views here.
def index(request):
return render(request, 'main/login.html')
def home(request):
resellers = Reseller.objects.all()
customers = Customer.objects.all()
context = { 'resellers' : resellers, 'customers' : customers }
return render(request, 'main/home.html', context)
This the recursetree tag that is been cited in mptt docs.
home.html
{% recursetree resellers %}
<li>
{{ node.reseller_name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
Here is the order I'm aiming to:
reseller1
-reseller2
-customer1
--reseller3
----customer2
The reason it doesn't work is because customers aren't part of the resellers tree, you need to get them manually. Something like this will give you a start:. If customers aren't always at a lead node you'll need to make some changes, though.
{% recursetree resellers %}
<li>
{{ node.reseller_name }}
{% if node.cust_children.all %}
<ul>
{% for customer in node.cust_children.all %}
<li>{{ customer.customer_name }}</li>
{% endfor %}
</ul>
{% endif %}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}

How can I grab articles with the same tag, so within a template I can display those articles?

I'm new to Django, so thanks for any help.
I have an Article model, and I would like to display related/similar articles by assigning tags to each article.
I've tried making a function/filter in my views.py that inherits from self (that particular article) and filters out the articles with the same tag, but with no success.
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200, blank=True)
thumbnail = models.ImageField(max_length=200, blank=True)
tag = models.CharField(max_length=200, blank=True)
from .models import Article
class ArticleView(DetailView):
template_name = "article/article.html"
model = Article
def related_articles(self):
tagged = Article.objects.filter(tag=self.tag)
return tagged
{% if articles.objects.all %}
{% for article in article.objects.all|related_articles %}
<div>
<img src="{{ article.thumbnail.url }}">
<span>{{ article.title }}</span>
</div>
{% endfor %}
{% endif %}
So, whenever I try to use this filter I get no results.
I think the related_articles function should be on the model class.
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200, blank=True)
thumbnail = models.ImageField(max_length=200, blank=True)
tag = models.CharField(max_length=200, blank=True)
def related_articles(self):
tagged = Article.objects.filter(tag=self.tag).exclude(pk=self.pk)
Suppose your have a view like this:
def articles(request):
articles = Articles.objects.all()
context = dict(articles=articles)
return render(request, 'app/articles.html', context)
You could have a template like this:
{% if articles|length %}
{% for article in articles %}
<div>
<img src="{{ article.thumbnail.url }}">
<span>{{ article.title }}</span>
{% for related_article in article.related_articles %}
<span>{{ related_article.title }}</span>
{% endfor %}
</div>
{% endfor %}
{% endif %}

Data from Views.Py not showing up on the template

I am trying to find out why some data from my views.py are not showing up. Here's my code
views.py
def user(request, user_id):
profile = get_object_or_404(User, pk=user_id)
rnk = Ranks.objects.all()
context = {
'profile' : profile,
'rnk' : rnk,
}
return render(request, 'user/user.html', context)
I am trying to show, for example the rank_name from my model and I use {{rnk.rank_name}} in the HTML template but it's not showing up.
On the other hand, data from profile like {{profile.user_name}} are showing up.
Note that rnk and profile are from this model:
class Ranks(models.Model):
rank_name = models.CharField(max_length=300)
description = models.TextField(blank=True)
def __str__(self):
return self.br_rank_name
class User(models.Model):
b_rank = models.ForeignKey(Ranks, on_delete=models.DO_NOTHING)
name = models.CharField(max_length=20)
link = models.URLField(max_length=100)
weekly = models.BooleanField(default=False)
biweekly = models.BooleanField(default=False)
def __str__(self):
return self.name
Here's my template
{% extends 'base.html' %}
{% load static %}
{% block content %}
<h5>{{profile.user_name}}</h5><!--This shows up-->
<p>{{rnk.rank_name}}</p>
<p>{{profile.weekly}}</p>
<span class="icon-desc">{{rnk.rank_points}} points</span>
{% endblock %}
That's because the rnk passed to the template is a queryset and includes multiple objects. So you need to iterate rnk using for and try to show the details for each one in your template.
{% extends 'base.html' %}
{% load static %}
{% block content %}
<h5>{{ profile.user_name }}</h5>
<p>{{ profile.weekly }}</p>
{% for rank in rnk %}
<p>{{ rank.rank_name }}</p>
<span class="icon-desc">{{ rank.rank_points }} points</span>
<img src="{{ rank.br_photo.url }}" height="150" alt="">
{% endfor %}
{% endblock %}

Slug not rendering detail page in Django 2.0

What is the best way to use slug in this case and make it work properly. I can see the URL on the browser display the items requested but i am unable to render the detailed page. I cant find where the issue is coming from. When i access 'page_detail' the url is 'http://127.0.0.1:8000/posts/2019/03/23/greetings/', which is correct based on my input but django throw an error to render the page. Error is: TypeError: post_detail() got an unexpected keyword argument 'slug'
MODEL:
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')
author = models.ForeignKey(User, on_delete = models.CASCADE, related_name='blog_posts')
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')
published = PublishedManager() # Custom Model Manager
def get_absolute_url(self):
''' Canonical URL for post detail.'''
return reverse('snippets:post-detail',
args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
URL
app_name = 'snippets'
urlpatterns = [
path('posts/', views.post_list, name='post-list'),
path('posts/<int:year>/<int:month>/<int:day>/<slug:slug>/', views.post_detail, name='post-detail'),
]
VIEWS
def post_list(request):
posts = Post.published.all()
context = {'posts': posts}
return render(request, 'snippets/list.html', context)
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, 'snippets/detail.html', {'post':post})
post_list HTML
{% extends "base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>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 %}
{% endblock %}
post_detail HTML
{% extends "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 %}
I am still stuck. Any help would be much appreciated.
try to change your views.py to
def post_detail(request, year, month, day, slug):
post = get_object_or_404(Post, slug=slug,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request, 'snippets/detail.html', {'post':post})

slug url won't display url's that don't have hyphens

for some reason I can't view pages which have a slug without a hyphen. For example:
This doesn't work:
/example1
This works:
/this-way-works
I have tried changing the regular expressions but had no joy. Any help would be appreciated!
urls
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register_profile/$', views.register_profile, name='register_profile'),
url(r'^update_profile/$', views.update_profile, name='update_profile'),
url(r'^create_project/$', views.CreateProject.as_view(), name='create_project'),
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
)
project_page view
def project_page(request, slug):
context_dict = {}
username = request.user.username
user = get_object_or_404(User, username=username)
context_dict['project_user'] = user
project = UserProject.objects.get(slug=slug)
context_dict['project'] = project
context_dict['project_title'] = project.title
return render(request, 'howdidu/project.html', context_dict)
models
class UserProject(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
project_overview = models.CharField(max_length=1000)
project_picture = models.ImageField(upload_to='project_images', blank=True)
date_created = models.DateTimeField(auto_now_add=True)
project_views = models.IntegerField(default=0)
project_likes = models.IntegerField(default=0)
project_followers = models.IntegerField(default=0)
slug = models.SlugField(max_length=100, unique=True) #should this be unique or not?
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(UserProject, self).save(*args, **kwargs)
def __unicode__(self):
return self.title
template
{% extends 'howdidu/base.html' %}
{% load staticfiles %}
{% block title %}{{ profile_user.userprofile.first_name }} {{ profile_user.userprofile.second_name }}{% endblock %}
{% block body_block %}
<h1>{{ profile_user.userprofile.first_name }}'s profile page</h1>
<img src="{{ profile_user.userprofile.profile_picture.url }}" width = "150" height = "150" />
<h2>{{ profile_user.userprofile.first_name }} {{ profile_user.userprofile.second_name }}</h2>
<h2>{{ profile_user.userprofile.user_country }}</h2>
{% if projects %}
<ul>
{% for project in projects %}
<li>{{ project.title }}</li>
{% endfor %}
</ul>
{% else %}
<strong>There are no projects present.</strong>
{% endif %}
{% if user.is_authenticated %}
{% if profile_user.username == user.username %}
<p>Edit profile</p>
<p>Create new project</p>
{% endif %}
{% endif %}
{% endblock %}
The problem is in you url ordering.
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
There is no big difference for Django in these urls. Django will go from up to down and find first match. If there is a - in your slug it will map to
url(r'^(?P<slug>[-\w]+)/$', views.project_page, name='user_project')
as it is the only url that matches -. But if your slug is example1 link will match
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url as it is closer to the top. What you need to do is to add another 'level' to the urls. For example:
url(r'^users/(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^projects/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
Then everything will work fine.
P. S.
Your url doesn't deppend on user so actually any user can see all projects, I think that's not what you need.Think about this for a little bit and ask me if you need any help.