Hello dear professors:
I wrote several modules to design a website, each of them works well on a separate page, now I want these modules to be displayed on the main page of my website, for this I selected Include, but unfortunately I got a response I didn’t Please guide me in this matter, for example, about us, products, contact us, each works on a single page, but I want them to be shown on the main page as well.
You’ll need to post the relevent code here. Let’s start with the view for your main page, and the template that it is rendering.
Also post one of views and templates for one of the other pages, such as “about us”. (Don’t post all of them, having one example will be ok for now.)
Note: Do not post images of your code or templates.
Copy / paste the actual code (and template) into your post.
When posting code (and templates) here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.
(You can also highlight (select) the code in the editor panel and click on the </>
icon in the menu at the top to mark your code for you.)
Very good, so I will complete the code and send it to you, because after this I get another error, after completion, I will send both to you as per your request.
I will mention up front that in Django, it’s the views that are in control of what data is being rendered, not the templates.
If you’re going to render a template, then it’s the responsibility of the view rendering that template to populate the context.
What this means is that if view “X” is rendering template “A”, and template “A” includes template “B”, then view “X” needs to ensure that all data needed for template “B” is in the context that view “X” is using for the render process.
There is no mechanism by which template “B” is going to cause any view to execute to populate the context that it may be looking for.
(This is a lot easier to explain with real code, which is why I’m asking to see code from your project.)
class Post(models.Model):
class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'
REJECTED = 'RJ', 'Rejected'
# relation
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_posts', verbose_name='author')
# data fields
title = models.CharField(max_length=250, verbose_name='title')
description = models.TextField(verbose_name='description')
slug = models.SlugField(max_length=25)
# data
publish = jmodels.jDateTimeField(default=timezone.now, verbose_name='publish')
created = jmodels.jDateTimeField(auto_now_add=True)
updated = jmodels.jDateTimeField(auto_now=True)
# choices fields
status = models.CharField(max_length=2, choices=Status.choices, default=Status.DRAFT, verbose_name='pos')
reading_time = models.PositiveIntegerField(verbose_name='reading')
# objects = models.Manager()
objects = jmodels.jManager()
published = PublishedManager()
class Meta:
ordering = ['-publish']
indexes = [
models.Index(fields=['publish']),
]
verbose_name = 'post'
verbose_name_plural = 'posts'
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:posts_detail', args={self.id})
# this is my models
# ------------------------------------------------------------------
# this is my index.html
{% extends 'parent/base.html' %}
{% block title %}post list{% endblock %}
{% block content %}
# I want the main page to be shown to the user of this section and to include the single pages here, but it does not show regularly.
{% endblock %}
What is your view that is rendering “index.html”?
What are the complete contents of your “index.html”, including the directives that you are using to show that other content?
{% extends 'parent/base.html' %}
{% block title %}post list{% endblock %}
{% block content %}
{% include 'about.htm'%}
{% endblock %}
On the index page, for example, I include the page about us, but it shows untidy
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, Http404
from .models import *
from .form import *
# Create your views here.
def index(request):
return render(request, 'blog/index.html')
def about(request):
posts = About.objects.all()
context = {
'posts': posts
}
return render(request, 'blog/about.html', context)
def post_search(request):
query = None
results = []
if 'query' in request.GET:
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
results = About.objects.filter(title__icontains=query)
context = [
'query', query,
'results', results
]
return render(request, 'blog/search.html', context)
and this is my view page
Is this a template or an HTML page?
If it is a template, is it rendering data?
If it is rendering data, are you aquiring the data in the view and passing it through to the template in your render function?
Each of the pages works great on its own, my problem is putting them into an index page that can be used on the home page.
they are template pages
f it is a template, is it rendering data? YES
If it is rendering data, are you aquiring the data in the view and passing it through to the template in your render function? YES SIR
{% extends 'parent/base.html' %}
{% load static %}
{% block title %}About_us{% endblock %}
{% block content %}
<!-- About Start -->
<div class="container-fluid about py-5">
<div class="container py-5">
<div class="row g-5 align-items-center">
<div class="col-lg-5">
{% for post in posts %}
<div class="h-100" style="border: 50px solid; border-color: transparent #13357B transparent #13357B;">
<img src="{% static 'img/about-img.jpg' %}" class="img-fluid w-100 h-100" alt="">
</div>
</div>
<div class="col-lg-7" style="background: linear-gradient(rgba(255, 255, 255, .8), rgba(255, 255, 255, .8)), url(img/about-img-1.png);">
<h5 class="section-about-title pe-3">{{post.title}}</h5>
<h1 class="mb-4"> {{post.title_en}}</h1>
<p class="mb-4">{{ post.description|linebreaks }}</p>
<div class="row gy-2 gx-4 mb-4">
<div class="col-sm-6">
<p class="mb-0"><i class="fa fa-arrow-right text-primary me-2"></i>{{post.under_title_one}}</p>
</div>
<div class="col-sm-6">
<p class="mb-0"><i class="fa fa-arrow-right text-primary me-2"></i>{{post.under_title_two}}</p>
</div>
<div class="col-sm-6">
<p class="mb-0"><i class="fa fa-arrow-right text-primary me-2"></i>{{post.under_title_three}}</p>
</div>
<div class="col-sm-6">
<p class="mb-0"><i class="fa fa-arrow-right text-primary me-2"></i>{{post.under_title_four}}</p>
</div>
<div class="col-sm-6">
<p class="mb-0"><i class="fa fa-arrow-right text-primary me-2"></i>{{post.under_title_five}}</p>
</div>
<div class="col-sm-6">
<p class="mb-0"><i class="fa fa-arrow-right text-primary me-2"></i>{{post.under_title_six}}</p>
</div>
</div>
<a class="btn btn-primary rounded-pill py-3 px-5 mt-2" href="">Read More</a>
</div>
{% endfor %}
</div>
</div>
</div>
<!-- About End -->
{% endblock %}
my about.html
Not in your index view you’re not.
What is the solution?
See my response at Create a template on the main page - #4 by KenWhitesell
thank you I will try my best