Answer the question
In order to leave comments, you need to log in
Why is the variable not displayed in the template?
index.html
{% extends 'main/main.html' %}
{% block admin-panel %}
<a href="" class="btn btn-success">Создать</a>
<a href="{{ admin_obj.get_update_url }}" class="btn btn-primary">Изменить</a>
<a href="{{ admin_obj.get_delete_url }}" class="btn btn-danger">Удалить</a>
{% endblock %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="{% static 'css/admin_panel.css' %}">
<title>
{% block title %}
Блог
{% endblock %}
</title>
</head>
<body style="background: #9d9d9d">
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="{% url 'main_url' %}">
<img src="{% static 'home.svg' %}" width="30" height="30" class="d-inline-block align-top mr-2" alt="">Блог
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{% url 'posts_url' %}">Посты<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="{% url 'tags_url' %}">Теги<span class="sr-only">(current)</span></a>
</li>
</ul>
<a href="{% url 'post_create_url' %}"><button type="button" class="mr-2 btn btn-outline-success">Создать +</button></a>
<a href="{% url 'tag_create_url' %}"><button type="button" class="mr-2 btn btn-outline-success">Создать #</button></a>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-info my-2 my-sm-0" type="submit">Искать</button>
</form>
</div>
</nav>
<!-- navbar -->
{% block content %}
<h1 class="m-5">There content</h1>
{% endblock %}
<div class="admin-panel align items">
<a href="/admin" class="btn btn-outline-info">Admin</a>
{% block admin-panel %}{% endblock %}
</div>
</body>
</html>
from django.shortcuts import render, get_object_or_404, redirect
from . models import *
class ObjectDetailMixin:
model = None
template = None
def get(self, request, slug):
obj = get_object_or_404(self.model, slug__iexact=slug)
return render(request, self.template, context={self.model.__name__.lower(): obj, 'admin_obj': obj})
class ObjectCreateMixin:
model_form = None
template = None
def get(self,request):
form = self.model_form()
return render(request, self.template, context={'form': form})
def post(self, request):
bound_form = self.model_form(request.POST)
if bound_form.is_valid():
new_obj = bound_form.save()
return redirect(new_obj)
return render(request, self.template, context={'form': bound_form})
class ObjectUpdateMixin:
def get(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
bound_form = self.model_form(instance=obj)
return render(request, self.template, context={'form': bound_form, self.model.__name__.lower(): obj})
def post(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
bound_form = self.model_form(request.POST, instance=obj)
if bound_form.is_valid():
new_obj = bound_form.save()
return redirect(new_obj)
return render(request, self.template, context={'form': bound_form, self.model.__name__.lower(): obj})
class ObjectDeleteMixin:
def get(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
return render(request, self.template, context={self.model.__name__.lower(): obj})
def post(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
obj.delete()
return redirect(reverse(self.redirect_url))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question