Я пытаюсь ввести запрос в html с django, но дело в том, что он сохраняется в базе данных, но запрос не распознает его, я упомяну соответствующие части, чтобы вы могли видеть к тому, что я имею в виду.
from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout
from django.urls import reverse, reverse_lazy
from .forms import PostForm, PostUpdateForm
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post, Category
# Create your views here.
def index(request):
return render(request, 'app1/index.html')
class PostView(ListView):
model = Post
template_name = 'app1/post.html'
ordering = ['-post_date']
def get_context_data(self, *args, **kwargs):
cat_menu = Category.objects.all()
context = super(PostView, self).get_context_data(*args, **kwargs)
context["cat_menu"] = cat_menu
return context
def CategoryView(request, cats):
category_posts = Post.objects.filter(category=cats.replace('-', ' '))
return render(request, 'app1/categories.html', {'cats':cats.title().replace('-', ' '), 'category_posts': category_posts})
def CategoryListView(request):
cat_menu_list = Category.objects.all()
return render(request, 'app1/category_list.html', {"cat_menu_list":cat_menu_list})
class ArticleDetailView(DetailView):
model = Post
template_name = 'app1/article_details.html'
class AddPostView(CreateView):
model = Post
form_class = PostForm
template_name = 'app1/createpost.html'
#fields = '__all__'
def get_context_data(self, *args, **kwargs):
cat_menu = Category.objects.all()
context = super(AddPostView, self).get_context_data(*args, **kwargs)
context["cat_menu"] = cat_menu
return context
class UpdatePostView(UpdateView):
model = Post
form_class = PostUpdateForm
template_name = 'app1/update_post.html'
class DeletePostView(DeleteView):
model = Post
template_name = 'app1/delete_post.html'
success_url = reverse_lazy('index')
class AddCategoryView(CreateView):
model = Category
template_name = 'app1/add_category.html'
fields = '__all__'
success_url = reverse_lazy('app1:Post')
В представлениях основная проблема заключается в функции CategoryView, в частности в переменной category_posts
{% extends "app1/base.html" %}
{% block body_block %}
{{category_posts}}
{% if category_posts%}
<h1> {{cats}} Category</h1>
<ul>
{% for post in category_posts %}
<li><a href="{% url 'app1:article-detail' post.pk %}">{{post.title}}</a> -
{{post.author}} - <small>{{post.post_date}}</small> -
{% if user.is_authenticated %}
<small><a href="{% url 'app1:updatepost' post.pk %}">Edit</a></small><small>
<a href="{% url 'app1:deletepost' post.pk %}">- Delete</a>
</small></li>
{% endif %}
{{post.body|slice:":200"|safe}}
{% endfor %}
</ul>
{% else %}
<h1>Sorry this page does not exist</h1>
{% endif %}
{% endblock %}
вот проблема, она читается как false оператор if, и он должен быть истинным, потому что он находится в базе данных.
models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from datetime import datetime, date
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length= 255)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('index')
class Post(models.Model):
title = models.CharField(max_length= 255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255)
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
Я был бы очень признателен за вашу помощь, спасибо за чтение .