I've been doing some django these past weeks and I'm having a problem.
I have a page where Articles are shown. No problem while reovering all articles from db. But now I'd like to get all categories (an Article has a category) that I have in my database.
So I can display like this in my page:
List of categories
-cat1
-cat2
-cat3
List of articles
-art1
-art2
-art3
But I don't know how to do with both queries.
Here's what I've tried.
class IndexView(generic.ListView):
template_name = 'eduardoApp/index.html'
context_object_name = 'article_list'
def get_queryset(self):
return Article.objects.order_by('article_name')
def get_categories(request):
category_list=Category.objects.all()
context = {'category_list':category_list}
return render(request,'eduardoApp/index.html',context)
And in my view:
<h2>List of categories</h2>
{% if category_list %}
{% for category in category_list %}
<p>{{ category.name }}</p>
{% endfor %}
{% else %}
<p>no categorys</p>
{% endif %}
<h2>List of articles</h2>
{% if article_list %}
<div class="flex-container">
{% for article in article_list %}
<div>{{ article.article_name }}</div>
{% endfor %}
</div>
{% else %}
<p>No articles...</p>
{% endif %}
{% endblock %}
In my view I keep seeing no categorys displayed (since category_list does not exist but don't know why and how to fix)
ListView is creating context with 'objects' as queryset get_queryset returns.
I suppose your custom method get_categories hasn't been used anywhere?
Best practice here is to override get_context_data method like...
class IndexView(generic.ListView):
...
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['category_list'] = ...
return context
Related
I'm trying to include a link on a webpage to download a tables2 table to csv. I got the commented out piece below from the documentation, and I think it might just need a simple tweak to get it to work with my code. Any thoughts?
views.py
class PlatListView(SingleTableMixin, FilterView):
model = Plat
template_name = 'blog/filtertable.html'
filter_class = PlatFilter
def get_context_data(self, **kwargs):
context = super(PlatListView, self).get_context_data(**kwargs)
query = Phase.objects.all().select_related('plat','plat__community')
f = PlatFilter(self.request.GET, queryset=query)
t = PlatTable(data = f.qs)
RequestConfig(self.request, paginate=False).configure(t)
context['filter'] = f
context['table'] = t
'''
export_format = self.request.GET.get("_export", None)
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, query)
return exporter.response("query.{}".format(export_format))
'''
return context
filtertable.html
{% extends "blog/base.html" %}
{% block content %}
{% load querystring from django_tables2 %}
<div style = 'padding-top: 24px'>
Download CSV
</div>
{% load render_table from django_tables2 %}
{% load bootstrap4 %}
{% if filter %}
<form action="" method="get" class="form form-inline">
{% bootstrap_form filter.form layout='inline' %}
{% bootstrap_button 'filter' %}
</form>
{% endif %}
{% render_table table 'django_tables2/bootstrap4.html' %}
{% endblock content %}
The commented-out snippet in your code is for function-based views as mentioned in the docs. In your case you should just add the ExportMixin :
from django_tables2.export.views import ExportMixin
class PlatListView(SingleTableMixin, ExportMixin, FilterView):
# other stuff your view is doing
I'm trying to make Todo app. I want to make checkbox next to task, so when you select it, the task is set to done. The problem is, I can't really know how to change value of my BooleanField in Task Model. There are plenty of posts like this, but they are usually using functions inside views.py or use forms, but I can't relate do my form in ListView.
views.py
class TodolistView(LoginRequiredMixin, ListView):
model = Todolist
template_name = 'todolist.html'
def get_queryset(self):
return Todolist.objects.all().filter(user=self.request.user)
def get(self, request, *args, **kwargs):
todolist_objects = Todolist.objects.all()
return render(request, 'todolist.html', {'todolist_objects': todolist_objects})
todolist.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<p>Add new task</p>
{% for todo in todolist_objects %}
<div>
<form action="" method="post">
<li> {{ todo }} see details</l>
</form>
</div>
{% endfor %}
{% endblock %}
I'm trying to increment a variable with "if" in the Django template, I've already tried all the suggestions on this site, but none have solved my problem.
there is views.py
class IndexView(generic.ListView):
template_name = 'home/index.html'
context_object_name = 'problemes'
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data['problemes'] = Probleme.objects.all()
context_data['commentaires'] = Commentaire.objects.all()
return context_data
def get_queryset(self):
result_list = self.get_context_data
return result_list
and index.html
....
{% for problem in problemes %}
{% with number_comment= 0%}
{% for comment in commentaires %}
{% if comment.probleme.id == problem.id %}
{% number_comment ++ %}
{% endif %}
{% endfor %}
<tr data-status="pagado">
<td>
<div class="votes">
<div class="mini-counts"><span title="74 votes">74</span></div>
<div>votes</div>
</div>
</td>
<td>
<div class="status answered-accepted" title="one of the answers was accepted as the correct answer">
<div class="mini-counts"><span title="2 answers">{{ number_comment }}</span></div>
<div>answers</div>
</div>
{% endwith %}
....
If you can reference your number_comment directly as you have it in your index.html, you can use an add template tag to increment it. Of course this increments number only in template and doesn't save to the database.
{{ number_comment|add:"1" }}
I need to create a UpdateForm with a TemplateView. Why with TemplateView? Because, I has a attribute what is geo_location, and I'm using LeafLet maps, and LeafLet maps doesn't work with generic.UpdateView or others the same type.
Here my views from Update:
class UpdateStore(LoginRequiredMixin, TemplateView):
template_name = 'store_form'
success_url = reverse_lazy('register:store_list')
def post(self, request, *args, **kwargs):
store_id = kwargs['store']
store = get_object_or_404(Store, pk=store_id)
form = StoreForm(request.POST, on_edit=True)
if form.is_valid():
form.save()
return redirect(reverse('register:store_list'))
else:
context = self.get_context_data()
context['data_form'] = form
return render(request, self.template_name, context)
return self.get(request)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
store_id = self.kwargs['store']
store = get_object_or_404(Store, pk=store_id)
data = {
'name': store.name,
'description': store.description,
'address': store.address,
'geo_loc': store.geo_loc,
'opened': store.opened
}
context['editing'] = True
context['data_form'] = StoreForm(initial=data, on_edit=True)
context['store'] = store
return context
Here is my template code:
{% extends 'base.html' %}
{% load bootstrap3 %}
{% load leaflet_tags %}
{% block extra_css %}
{% leaflet_css plugins="forms" %}
{% endblock %}
{% block body %}
<h1> Update Store </h1>
<form method="POST">
{% csrf_token %}
{{ form }}
{% buttons %}
<button type="submit">
{% bootstrap_icon "star" %} Save
</button>
{% endbuttons %}
</form>
{% endblock %}
{% block extra_js %}
{% leaflet_js plugins="forms" %}
{% endblock %}
I trying this, but in my template, the Forms doesn't load, and my template are blanked :(. Someone knows why? I need another method for get anything else?
Thanks.
The problem with your code is that you place the form in the data_form key of the context:
context['data_form'] = StoreForm(initial=data, on_edit=True)
and then on the template you try to use it with {{form}} instead of {{data_form}}. After that the form should be rendered.
article = get_object_or_404(Article,slug=slug)
categories = article.category.all()
Using render_to_response() , how can I use the categories in the view ?
Assuming you have the article in the template, you can do the following:
# In your view
return render_to_response('page.html', {'article': article})
# In your template
{% for category in article.category.all %}
{{ category.attribute }}
{% endfor %}
# Or, if you already have the categories
return render_to_response('page.html', {'categories': categories})
{% for category in categories %}
{{ category.attribute }}
{% endfor %}