I am using django-mptt library for the category. I could show the list of categories in template but I wanted to indent it properly so user can know which is a main category and which one is sub-category and etc. The way i tried to structure is
{% recursetree nodes %}
<li class="node">
<a href="/category/{{ node.get_absolute_url }}"
class="{% if not node.is_leaf_node and not node.is_root_node and node.is_child_node %} child_parent {% elif node.is_leaf_node and not node.is_root_node %}leaf_node{% endif %}">
{{ node.name }}
{% if node.is_root_node %}
<span class="badge">{{ node.get_count_children }}</span>
{% endif %}
</a>
{% if not node.is_leaf_node %}
<ul class="children">
<li>{{ children }}</li>
</ul>
{% endif %}
</li>
{% endrecursetree %}
This yields the following design of category
Here Dressing Table is a child of Bedroom Items like Bed and Almirah not a child of Bed. How could i fix this? I know the problem is here
<a href="/category/{{ node.get_absolute_url }}"
class="{% if not node.is_leaf_node and not node.is_root_node and node.is_child_node %} child_parent {% elif node.is_leaf_node and not node.is_root_node %}leaf_node{% endif %}">
but could not fix this issue
Not the dressing table in the screenshot
According to you updated answer.
Dinning Set, Kitchen Rack, and Kitchen Setup(Modular Kitchen) are supposed to be cyan since they are second level.
If my understanding is correct.
Here is my hacked solution. If anybody found the better one please raise.
Add extra method to the Model instance
I have to add nodes to the context. (This would be an optional if you are using Django2.0 like mine)
Use the instance method in the template
models.py
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
class Genre(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True,
on_delete=models.CASCADE)
class MPTTMeta:
order_insertion_by = ['name']
def __str__(self):
return f'{self.name}'
def is_second_node(self):
return True if (self.get_ancestors().count() == 1) else False
views.py
from django.views.generic import ListView
from genres.models import Genre
class GenreListView(ListView):
model = Genre
template_name = 'genre_list.html'
def get_context_data(self, *, object_list=None, **kwargs):
"""Get the context for this view."""
queryset = object_list if object_list is not None else self.object_list
page_size = self.get_paginate_by(queryset)
context_object_name = self.get_context_object_name(queryset)
if page_size:
paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
context = {
'paginator': paginator,
'page_obj': page,
'is_paginated': is_paginated,
'object_list': queryset
}
else:
context = {
'paginator': None,
'page_obj': None,
'is_paginated': False,
'object_list': queryset
}
if context_object_name is not None:
context[context_object_name] = queryset
context.update(kwargs)
context['nodes'] = context.get('object_list')
return super().get_context_data(**context)
genre_list.html
<!DOCTYPE html>
{% load mptt_tags %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Genre ListView</title>
</head>
<style>
.root {color: purple}
.child{color: cyan}
.leaf {color: gray}
</style>
<body>
{% recursetree nodes %}
<div class="
{% if node.is_root_node %}
root
{% elif node.is_child_node and not node.is_leaf_node or node.is_second_node%}
child
{% elif node.is_leaf_node and not node.is_root_node%}
leaf
{%endif%}">
{{node.name}}
{{node.is_second_node}}
</div>
{% if not node.is_leaf_node%}
<ul>{{children}}</ul>
{% endif %}
{% endrecursetree %}
</ul>
</body>
</html>
Related
I have two seperated models. One with two text fields and one for multiple images. Now I want to display the entire data in one html div. What do I have to change in the projects view and in projects.html?
models.py
class Project(models.Model):
title = models.CharField(max_length=200)
describtion = models.TextField(null=True, blank=True)
class ProjectImage(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
image = models.FileField(upload_to="products/")
forms.py
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ['title', 'describtion']
class ProjectImageForm(forms.ModelForm):
class Meta:
model = ProjectImage
fields = ['image']
widgets = {
'image': ClearableFileInput(attrs={'multiple': True}),
}
views.py
def createProject(request):
form = ProjectForm()
form2 = ProjectImageForm()
if request.method == 'POST':
form = ProjectForm(request.POST)
form2 = ProjectImageForm(request.POST, request.FILES)
images = request.FILES.getlist('image')
if form.is_valid() and form2.is_valid():
title = form.cleaned_data['title']
describ = form.cleaned_data['describtion']
project_instance = Project.objects.create(
title=title, describtion=describ)
for i in images:
ProjectImage.objects.create(project=project_instance, image=i)
context = {'form': form, 'form2': form2}
return render(request, 'projects/project_form.html', context)
def projects(request):
projects = Project.objects.all()
context = {"projects":projects}
return render(request, 'projects/projects.html', context)
projects.html
{% for project in projects %}
<div class="column">
<div class="card project">
<img class="project__thumbnail" src="{{project.image.url}}" alt="project thumbnail" />
<div class="card__body">
<h3>{{project.title}}</h3>
<h2>{{project.describtion}}</h2>
</div>
</a>
</div>
</div>
{% endfor %}
You don't need to change anything.
You should be able to access the reverse with project.project_image_set attribute in the template:
<div class="card__body"
<h3>{{project.title}}</h3>
<h2>{{project.describtion}}</h2>
{% for image in project.project_image_set.all %}
{{ image.image }}
{% endfor %}
</div>
Docs: https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_one/
I don't really understand the question here but i see a problem in your template considering you are using foreign key in ProjectImage. And update the question
{% for project in projects %}
<div class="column">
<div class="card project">
{% for j in project.projectimage_set.all %}
<img class="project__thumbnail" src="{{j.image.url}}" alt="project thumbnail" />
{% endfor %}
<div class="card__body">
<h3>{{project.title}}</h3>
<h2>{{project.describtion}}</h2>
</div>
</a>
</div>
</div>
{% endfor %}
I would change FileField to ImageField and add function:
#property
def get_photo_url(self):
if self.image and hasattr(self.image, 'url'):
return self.image.url
else:
return ''
If createProject function works( I would rename it to create_project) then in projects.html:
{% for project in projects %}
<div class="column">
<div class="card project">
<div class="card__body">
<h3>{{project.title}}</h3>
<h2>{{project.describtion}}</h2>
{% for img in project.projectimage_set.all %}
<img class="project__thumbnail" src="{{img.get_photo_url}}" alt="project thumbnail" />
{% endfor %}
</div>
</a>
</div>
</div>
{% endfor %}
I'm making a search bar in Django using Class-Based Views on my Views.py. When I try to get the value submitted in the input it's not showing the query name and if I don't submit anything is NOT giving me an error. Also what is the way to get the post showed by the world searched? Thanks for all. My code is:
Models.py:
class Post(models.Model): # Post core
title = models.CharField(max_length=299)
author = models.ForeignKey(User,default=ANONYMOUS_USER_ID, on_delete=models.CASCADE)
category = models.CharField(max_length=50)
image = models.ImageField(blank=True)
desc = models.TextField()
text = RichTextField(blank = True, null = True )
date = models.DateTimeField(auto_now=False, auto_now_add=True)
slug = models.SlugField(null = True, blank = True, unique=True)
class Meta: # Order post by date
ordering = ['-date',]
def __str__(self): # Display title
return self.title
def get_absolute_url(self): # #TODO da cambiare
return reverse("listpost")
Views.py:
class SearchView(TemplateView):
model = Post
template_name = "admin/search.html"
def get(self, request, *args, **kwargs):
q = request.GET.get('q', '')
self.results = Post.objects.filter(text__icontains=q, desc__icontains = q, title__icontains = q)
return super().get(request, *args, **kwargs)
Urls.py:
path('dashboard/listpost/search/', SearchView.as_view(), name="search")
ListPost.html:
{% extends "admin/layout.html" %}
{% block title %}
<title>Post List</title>
{% endblock title %}
{% block content %}
<form action="{% url 'search' %}" method="GET">
<input type="text" placeholder="Cerca" name="search"> <button>Cerca</button>
</form>
<div class="postlist">
<div class="cards"> <!-- Card Container -->
{% for post in object_list %}
<div class="card"> <!-- Single Card -->
{% if post.image %}
<img src="{{post.image.url}}" alt="">
{% endif %}
<h3>{{ post.title }}</h3>
<p>
{{ post.category }}
{{ post.author }}
<data class="data"> {{ post.date|date:'d-m-Y' }} </data>
</p>
<p class="desc-list">{{ post.desc|truncatewords:10 }}</p>
edit
delate
</div>
{% endfor %}
</div>
</div>
{% endblock content %}
Search.html
{% extends "admin/layout.html" %}
{% block title %}
<title>Search</title>
{% endblock title %}
{% block content %}
{% if query %}
{{Query}}
{% else %}
<p>Nope</p>
{% endif %}
{% endblock content %}
Your are trying to display the variable query, Query (two different variables since the template language is case sensitive).
Your do not pass any of those two variables in the template context.
I don't see any query nor Query variable in your view.
It seems that your want to show the results variable, so I will assume this.
Your need to send the results from your queryset in the template results.
The get_context_data method ContextMixin (one of the TemplateView derived classes) is useful for this.
class SearchView(TemplateView):
model = Post
template_name = "admin/search.html"
def get(self, request, *args, **kwargs):
q = request.GET.get('q', '')
self.results = Post.objects.filter(text__icontains=q, desc__icontains = q, title__icontains = q)
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
"""Add context to the template"""
return super().get_context_data(results=results, **kwargs)
And in your template:
{% extends "admin/layout.html" %}
{% block title %}
<title>Search</title>
{% endblock title %}
{% block content %}
{% if results.exists %}
{% for result in results %}
{{ result }}
{% endfor %}
{% else %}
<p>Nope</p>
{% endif %}
{% endblock content %}
I have a quiz model and a view where I'm displaying a list of all the quizzes on the page. I want to show the score that they got alongside each quiz. Currently, I have a score model that links to each quiz and the user that took the quiz. I want to do something like Score.objects.filter(user=request.user, quiz=quiz).get_percentage() to show the percentage achieved for that quiz. However this doesn't work in template code of course.
def learn(request): #Quiz Screen
context = {
'quizzes':Quiz.objects.all(),
'scores':Score.objects.filter(user=request.user),
}
return render(request, 'quiz_app/learn.html', context)
{% extends "quiz_app/base.html" %}
{% block content %}
New Quiz
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
<p>Score: {{ quiz.score.get_percentage }}%</p>
</article>
</a>
{% endfor %}
{% endblock content %}
class Quiz(models.Model):
title = models.CharField(max_length=100) #or TextField
#slug = models.SlugField(max_length=200, default=1) #url
video_link = models.CharField(max_length=100, default="https://www.youtube.com/watch?v=p60rN9JEapg")
question_amount = models.IntegerField()
author = models.ForeignKey(User, default=1, on_delete=models.CASCADE, related_name='quiz_author',)
date_created = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name_plural = "Quizzes"
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('quiz-detail', kwargs={'pk': self.pk})
class Score(models.Model):
quiz = models.ForeignKey(Quiz, default=1, on_delete=models.CASCADE)
user = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
correct_answers = models.IntegerField()
incorrect_answers = models.IntegerField()
def __str__(self): #returns as a/b
score = str(self.correct_answers) + '/' + str(self.get_total_answers())
return score
def add_correct_answers(self, amount):
self.correct_answers = self.correct_answers + amount
self.save()
def add_incorrect_answers(self, amount):
self.incorrect_answers = self.incorrect_answers + amount
self.save()
def get_total_answers(self):
total = self.correct_answers + self.incorrect_answers
return total
def get_percentage(self):
percentage = round(self.correct_answers / self.get_total_answers() * 100)
return percentage
So as you have a foreign Key relationship between Quiz and Score, you can have multiple scores for a single quiz, therefore you should use the backward relationship from the quiz model like this:
{% extends "quiz_app/base.html" %}
{% block content %}
New Quiz
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
{% for score in quiz.score_set.all %}
<p>Score: {{ score.get_percentage }}%</p>
{% endfor %}
</article>
</a>
{% endfor %}
{% endblock content %}
Edit
This is a quick fix:
{% extends "quiz_app/base.html" %}
{% block content %}
New Quiz
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
{% for score in quiz.score_set.all %}
{% if score.user == request.user %}
<p>Score: {{ score.get_percentage }}%</p>
{% endif %}
{% endfor %}
</article>
</a>
{% endfor %}
{% endblock content %}
But it's not a good idea, because you will retrieve all the score for all the users first, and then filter them; It's better to use a template tag here.
I try to create a book database in Django. I have to do one more thing. So i have a model with CHOICES:
#model Book
class Book(models.Model):
#book types and placed
BIOGRAPHY = 1
FANTASY = 2
HISTORICAL = 3
HORROR = 4
CLASSIC = 5
YOUTH_LITHERATURE = 6
NON_FICTION = 7
MODERN_LITERATURE = 8
POETRY = 9
ADVENTURE = 10
ESSAYS = 11
ROMANCE = 12
SATIRE = 13
THRILLER = 14
DRAMA = 15
NONE = 0
B00K_CHOICES = (
(BIOGRAPHY,'Biography'),
(FANTASY, 'Fantasy/Sci-Fi'),
(HISTORICAL, 'Historical'),
(HORROR, 'Horror'),
(CLASSIC, 'Classic'),
(YOUTH_LITHERATURE, 'Youth Litherature'),
(NON_FICTION, 'Non-Fiction'),
(MODERN_LITERATURE, 'Modern Literature'),
(POETRY, 'Poetry'),
(ADVENTURE, 'Adventure'),
(ESSAYS, 'Essays'),
(ROMANCE, 'Romance'),
(SATIRE, 'Satire'),
(THRILLER, 'Thriller'),
(DRAMA, 'Drama'),
(NONE, 'No Information'),
)
book_image = models.ImageField(upload_to='book_image', blank=True, null=True)
book_name = models.CharField(max_length=255, unique=True)
book_author = models.ForeignKey(Author, on_delete=models.CASCADE)
book_types = models.IntegerField(choices=B00K_CHOICES, default= NONE)
book_description = models.TextField(null=True, blank=True)
book_pages = models.PositiveIntegerField(null=True, blank=True)
book_published = models.DateField(null=True, blank=True)
book_ratings = GenericRelation(Rating, related_query_name='book', default=NONE)
def __str__(self):
return '{}'.format(self.book_name)
And i create a simple view with display this choices:
def book_types_list(request):
book = Book.objects.all()
context = {'book': book, 'book_types': Book.B00K_CHOICES}
return render(request, 'plibrary_core/book_types_list.html', context)
I create also a html template with list of this choices:
{% extends 'base.html' %}
{% load static %}
{% block custom %}
<link rel='stylesheet' type="text/css" href="{% static 'css/background_color_styles.css' %}">
{% endblock %}
{% block title %}
<title>Book Types List | Private Library</title>
{% endblock %}
{% block content %}
<div class="container">
<div class="col">
<div id="background-color-content">
<h3>Book Types</h3>
<hr>
{% for book_type in book_types %}
<ul>
<h6>{{ book_type }}</h6>
</ul>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
And in this moment, i need to have something like this - User click on the book type (for example Horror) and then he see another template with all books with only this types. I try to do something like this but it doesn't work. I don't know how to fix it. Somebody know what i need to do?;X
Firt use ul apropiatelly, with each item in a li tag, and in the correct place:
<ul> <!-- ul is outside the loop -->
{% loop %}
<li> ...</li>
{% endloop %}
</ul>
Then in your view you don't need to call the object Book.objects.all() unless you want to show all your books. It looks like you are just displaying the book choices.
def book_types_list(request):
book = Book.objects.all() # why do you need this in your view?
context = {'book': book, 'book_types': Book.B00K_CHOICES}
return render(request, 'foo/books.html', context)
Then in your template you just need to:
<ul>
{% for book_type in book_types %}
<li>
<!-- you have to handle the logic of the url here -->
{{ book_type.1 }}
</li>
{% endfor %}
</ul>
urls.py
url(r'^my_url/(?P<book_type_id>\d+)/$', my_view, name='my_url'),
with this element you get to render a select element on screen, and it will use the choices you are providing in choices, use that instead of the for loop of book types
{{ book.booktypes }}
I made a blog with Django. The blog lets the admin create posts and categories. I used bootstrap to align the posts next to each other.
This is the homepage of my blog. When the posts reach the other end of the screen. look at the Example below:
post1 post2 post3 post4 post5 post6 post7 post8 post9 post10 post11 post12 post 13 post14 post16.
post 16 wouldnot show on the homepage, but if you go to its category it will show on the category list but not on the homepage which is index.html
index.html
{% extends 'base.html' %}
{% block content %}
{% if categories %}
<div class="tab">
{% for category in categories %}
<button class="tablinks"><a href="{{ category.get_absolute_url }}">{{
category.title }}</a></button>
{% endfor %}
{% else %}
<p>There are no posts.</p>
{% endif %}
</div>
<br><br><br>
<div class="container ">
{% if posts %}
<div class="row ">
{% for post in posts %}
<div class="poosts col-md-2">
<p class="past"><a class="link" href="{{ post.get_absolute_url }}"><span
class="tda"> {{post.title}}</span><br><br><span class="postbody">
{{post.body|truncatewords:13}}</a></span></p>
</div>
{% endfor %}
{% else %}
<p class="text-center">There are no posts.</p>
</div>
{% endif %}
</div>
{% endblock %}
categories page
{% extends 'base.html' %}
{% block head_title %}Viewing category {{ category.title }}{% endblock %}
{% block title %}{{ category.title }}{% endblock %}
{% block content %}
<br><br><br>
<div class="container">
{% if posts %}
<div class="row">
{% for post in posts %}
<div class="poosts col-md-4">
<p class="past"><a class="link" href="{{ post.get_absolute_url }}"><span
class="tda"> {{post.title}}</span><br><br>{{post.body|truncatewords:13}}</a>
</p>
</div>
{% endfor %}
{% else %}
<p class="text-center">There are no posts.</p>
</div>
{% endif %}
</div>
{% endblock %}
views.py
from django.shortcuts import render, redirect, get_list_or_404
from django.http import HttpResponse
from .models import Blog, Category
from django.shortcuts import render_to_response, get_object_or_404
def index(request):
return render_to_response('account/index.html', {
'categories': Category.objects.all(),
'posts': Blog.objects.all()[:5]
})
def view_post(request, slug):
return render_to_response('account/view_post.html', {
'post': get_object_or_404(Blog, slug=slug)
})
def view_category(request, slug):
category = get_object_or_404(Category, slug=slug)
return render_to_response('account/view_category.html', {
'category': category,
'posts': Blog.objects.filter(category=category)[:5]
})
models.py
from django.db import models
from django.db.models import permalink
class Blog(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
body = models.TextField()
posted = models.DateTimeField(db_index=True, auto_now_add=True)
category = models.ForeignKey('Category')
def __unicode__(self):
return '%s' % self.title
#permalink
def get_absolute_url(self):
return ('view_blog_post', None, { 'slug': self.slug })
def __str__(self):
return self.title
class Category(models.Model):
title = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(max_length=100, db_index=True)
def __unicode__(self):
return '%s' % self.title
#permalink
def get_absolute_url(self):
return ('view_blog_category', None, { 'slug': self.slug })
def __str__(self):
return self.title
you should try this 'posts': Blog.objects.all() in views.py