Using Checkbox in ListView - django

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 %}

Related

How to pass selection from Django template to Class view

I am struggling with something that should be routine for me by now but I have a mental block.
I have a model that stores Country names and a slug version of the name.
Eg. "United States", "united-states"
I want to display the country names in a template for selection, then return the selected country's slug value to the Class based View which will then obtain the data. The list of countries can be links or dropdown - whatever. But I need to obtain the selected value in the View. Here is a simplified version:
Template
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% for country in object_list %}
{{ country.pretty_name }}</br>
{% endfor %}
</form>
{% endblock content %}
View
class CountryView(TemplateView):
country_name = THE SLUG
country_obj = Country(country_name)
country_obj.build_country_dictionary()
country_obj.display()
So I think I need one of the get methods to access this but I cannot work it out. Thanks for any help.
The "structured way"
Have a look at FormView, where you define your form class (which you also need to create, depends on your situation can be a model form as well). The rest is pretty much handled by the view.
https://ccbv.co.uk/projects/Django/4.0/django.views.generic.edit/FormView/
PSEUDO code
class MyForm(ModelForm):
model = YourModel
class MyFormView(FormView):
form_class = MyForm
# depends on what you want to do, you can overwrite form_valid to do your logic
The quickest way
PSEUDO code
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
<select name="selection">
{% for country in object_list %}
<option value="{{ country.slug_name }}">{{ country.pretty_name }}</option>
{% endfor %}
</select>
<input type="submit">Submit</input>
</form>
{% endblock content %}
class CountryView(TemplateView):
def post(self, request, *args, **kwargs):
selection = request.POST.get('selection')

Add link on a web page to export tables2 data in Django

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

multiple query model in same view django

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

How to Create a UpdateForm with TemplateView?

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.

How to change dynamically "Site administration" string in Django's admin?

I want to replace dynamically "Site administration" by a custom string in my admin.
I've already overridden "base.html" for some other purpose, but now I need to pass a variable to this template to replace {{ title }} in
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
I've seen from this question that a variable can be passed to the change list template by overriding changelist_view and adding an extra_context in the model admin, but how can I pass an extra context to the "main" page of the admin"?
The index() view is inside django.contrib.admin.site.AdminSite class and supports extra_context as well, you could override it, something like:
def index(self, *args, **kwargs):
return admin.site.__class__.index(self, extra_context={'title':'customized title'}, *args, **kwargs)
admin.site.index = index.__get__(admin.site, admin.site.__class__)
Also you could override AdminSite directly and use customized_site instead of admin.site:
class CustomizedAdminSite(AdminSite):
def index(self, *args, **kwargs):
return super(CustomizedAdminSite, self).index(extra_context={...}, *args, **kwargs)
customized_site = CustomizedAdminSite()
If you want to have title in all Admin pages, better to use context processor or customize some template tag if you can.
You override the "admin/base_site.html" template:
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %} {{ title }} | {% trans 'YOUR TITLE HERE' %} {% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'STUFF HERE PERHAPS' %} </h1>
{% endblock %}
{% block nav-global %}
{% endblock %}