Reputation: 123
title_posts.html
{% extends "main/base.html" %}
{% load static %}
{% load humanize %}
{% block styles %}
<link rel="stylesheet" type="text/css" href="{% static 'main/css/title_posts.css' %}">
{% endblock styles %}
{% block content %}
<style>
body {
background-image: url("{{ game.cover_display.url }}");
background-repeat: no-repeat;
background-size: 100% auto;
background-color: #171717;
background-attachment: fixed;
background-position: 0 3.5rem;
}
</style>
<div class="container black2 container-nav gamenav">
<ul class="quantico">
<li><a class="text-light nav-link" href="{% url 'title-posts' game.title %}">Updates</a></li>
<li><a class="text-light nav-link" href="{% url 'title-bugs' game.title %}">Bugs</a></li>
<li><a class="text-light nav-link" href="https://twitter.com/{{ game.twitter }}">Twitter</a></li>
<li><a class="text-light nav-link" href="https://www.reddit.com/r/{{ game.reddit }}">Reddit</a></li>
</ul>
</div>
<div class="container black container-position">
<div class="row justify-content-center">
<div class="col-md-3 text-center">
<img class="cover-image-height" src="{{ game.cover.url }}">
</div>
<div class="col-md-9">
<p>{{ game.description| safe }}</p>
<div class="btn-group">
<button type="button" id="platform" class="btn btn-primary dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
Select Platform
</button>
<div class="dropdown-menu" id="plat-form-options">
{% for platform in game.platform.all %}
<option class="dropdown-item" value="{{ platform.id }}"
onclick="platFormSelect('{% url 'title-posts-ajax' title=game.title platform_id=platform.id %}', '{{ platform }}')">{{ platform }}</option>
{% endfor %}
</div>
</div>
</div>
</div>
<hr>
<div id="posts_data">
</div>
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-info mb-4" href="?page=1">First</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}
</div>
<script>
$(document).ready(function () {
if ($("#plat-form-options option").length > 0) {
$("#plat-form-options option")[0].click();
}
});
function platFormSelect(url, platform) {
$('#platform').text(platform);
$.get(url, function (response) {
$('#posts_data').html(response);
})
.done(function () {
})
.fail(function () {
});
}
</script>
{% endblock content %}
post_ajax.html: This gets inserted into the title_posts.html at div id="posts_data".
{% if user.is_superuser %}
{% for post in posts %}
<div class="row">
<div class="col-10">
<article class="media content-section">
<div class="media-body">
<h2 class="article-title">{{ post.article_title }}</h2>
<div class="article-content">{{ post.content|safe }}</div>
</div>
</article>
</div>
<div class="col-2 post-menu">
<a class="btn btn-primary" href="#" aria-label="Edit">
<i class="fas fa-edit" aria-hidden="true"></i>
</a>
<a class="btn btn-danger" href="#" aria-label="Delete">
<i class="far fa-trash-alt"></i>
</a>
</div>
</div>
<hr>
{% endfor %}
{% else %}
{% for post in posts %}
<div class="row">
<article class="media content-section">
<div class="media-body">
<h2 class="article-title">{{ post.article_title }}</h2>
<div class="article-content">{{ post.content|safe }}</div>
</div>
</article>
</div>
<hr>
{% endfor %}
{% endif %}
My post_ajax.html has an if else statement that does not trigger for if user.is_superuser even though I am logged in as superuser. I've tested this on the title_posts.html and it trigger but not on the post_ajax.html
Edit:
views.py
class TitlePostAjaxListView(ListView):
model = Post
template_name = 'main/posts_ajax.html'
context_object_name = 'posts'
paginate_by = 5
def get_queryset(self):
title = get_object_or_404(Game, title=self.kwargs.get('title'))
return Post.objects.filter(game=title, platform=Platform.objects.filter(
id=self.kwargs.get('platform_id')).first()).order_by('-date_published')
def get_context_data(self, **kwargs):
context = super(TitlePostAjaxListView, self).get_context_data(**kwargs)
context['game'] = get_object_or_404(Game, title=self.kwargs.get('title'))
return context
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
context = self.get_context_data()
return HttpResponse(render_to_string(self.template_name, context))
Upvotes: 0
Views: 163
Reputation: 7049
On your last line of code, try replacing return HttpResponse(...)
with return render(request, self.template_name, context)
and let me know if that works.
This will add the request
into the rendering process, allowing you to access request.user
Upvotes: 1