ModelForm is not displaying entry fields - django

I am struggling with displaying fields in my view in Django.
The submit button appears but the fields related to models do
not.
Here is my code:
Forms.py
from django.forms import ModelForm
from blog.models import Comments
class CommentForm(ModelForm):
class Meta:
model = Comments
Models.py
class Comments(models.Model):
entries_id = models.ForeignKey(Entries)
author = models.CharField(max_length=40)
text = models.TextField()
created_on = models.DateTimeField("date published")
Views.py
class CommentListView(ListView):
model = Comments
def get_context_data(self, **kwargs):
context = super(CommentListView, self).get_context_data(**kwargs)
context['now'] = timezone.now()
return context
def add_comment(self, request, *args, **kwargs):
if request.method == 'GET':
form = CommentForm()
elif request.method == 'POST':
form = CommentForm(request.POST.copy())
if form.is_valid():
context = dict(form = form)
return render_to_response('comments_list.html', context) # etc.
The template:
{% extends 'flatpages/default.html' %}
{% block body %}
{% for Comments in object_list reversed %}
<p>{{ Comments.text }} </p></br>
<p>{{ Comments.author }}</p>
{% empty %}
<p>No comments yet</li></p>
{% endfor %}
<div="form" >
<form method = 'post' action =''> {% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</div>
{% endblock %}
I would appreciate any help. Thank you.

You are not always giving a value to form. At least, I would do that:
def get_context_data(self, **kwargs):
context = super(CommentListView, self).get_context_data(**kwargs)
context['now'] = timezone.now()
context['form'] = CommentForm()
return context

Related

Django how to add a form to a DetailView with FormMixin

I am attempting to add a form for comments to a DetailView. The DetailView displays notes for specific projects. So the comments have a foreign key that is the specific note and the note has a foreign key for the specific project.
I am attempting to use FormMixin with DetailView. So far I have not bee successful. Currently I can get the form to display but it does not save and in the terminal I see the following error Method Not Allowed (POST): /projects/project/1/note/1/
I can get these to work separately but not with the form in the DetailView.
Here are my models:
class ProjectNotes(models.Model):
title = models.CharField(max_length=200)
body = tinymce_models.HTMLField()
date = models.DateField(auto_now_add=True)
project = models.ForeignKey(Project, default=0, blank=True, on_delete=models.CASCADE, related_name='notes')
def __str__(self):
return self.title
class ProjectNoteComments(models.Model):
body = tinymce_models.HTMLField()
date = models.DateField(auto_now_add=True)
projectnote = models.ForeignKey(ProjectNotes, default=0, blank=True, on_delete=models.CASCADE, related_name='comments')
The View:
class ProjectNotesDetailView(DetailView, FormMixin):
model = ProjectNotes
id = ProjectNotes.objects.only('id')
template_name = 'company_accounts/project_note_detail.html'
comments = ProjectNotes.comments
form_class = NoteCommentForm
def form_valid(self, form):
projectnote = get_object_or_404(ProjectNotes, id=self.kwargs.get('pk'))
comment = form.save(commit=False)
comment.projectnote = projectnote
comment.save()
return super().form_valid(form)
def get_success_url(self):
return reverse('project_detail', args=[self.kwargs.get('pk')])
The form:
class NoteCommentForm(forms.ModelForm):
class Meta:
model = ProjectNoteComments
fields =['body',]
widgets = {
'body': forms.TextInput(attrs={'class': 'form-control'})
}
The template:
% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="section-container container">
<div class="project-entry">
<h2>{{ projectnotes.title }}</h2>
<p>{{ projectnotes.body | safe }}</p>
</div>
<div><b>Comments on {{projectnotes.title}}</b></div>
{% if projectnotes.comments.all %}
{% for comment in projectnotes.comments.all %}
<div class="notecomments" style="padding: 10px;">
{{ comment.body | safe }}
</div>
{% endfor %}
{% else %}
<p>No comments have been have been added yet.</p>
{% endif %}
<h2>add note</h2>
<h1>Add Comment</h1>
<form action="" method="post">
{% csrf_token %}
{{ form.media }}
{{ form|crispy }}
<input type="submit" value="save">
</form>
{% endblock content %}
Try to change the order between DetailView and FormMixin in ProjectNotesDetailView then implement the post method (enabled by the `FormMixin):
class ProjectNotesDetailView(FormMixin, DetailView):
model = ProjectNotes
id = ProjectNotes.objects.only('id')
template_name = 'company_accounts/project_note_detail.html'
comments = ProjectNotes.comments
form_class = NoteCommentForm
def form_valid(self, form):
projectnote = get_object_or_404(ProjectNotes, id=self.kwargs.get('pk'))
comment = form.save(commit=False)
comment.projectnote = projectnote
comment.save()
return super().form_valid(form)
def get_success_url(self):
return reverse('project_detail', args=[self.kwargs.get('pk')])
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
Check how to use formmixin with detailview (documentation).

Why doesn't {% if %} {% else %} construction work in Django template?

So I am trying to display "YOU" above the comment if it's in fact a comment that has been posted by current user, otherwise I am trying to display just a username of one who left a comment.
But somehow if/else doesn't work. Can anyone please tell me what am I doing wrong?
Thank you beforehand!
my models.py
class Comments(models.Model):
commented_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
comment = models.TextField(max_length=300)
def __str__(self):
return self.comment
my forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comments
fields = ['comment']
widgets = {'comment': forms.Textarea(attrs={'class': 'form-control', 'rows': 5})}
my views.py
class Comment(CreateView, LoginRequiredMixin):
form_class = CommentForm
template_name = 'app/comments.html'
login_url = 'login'
success_url = reverse_lazy('comments')
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.commented_by = self.request.user
self.object.save()
if self.object:
messages.success(self.request, 'Success')
else:
messages.error(self.request, 'Error')
return redirect(self.get_success_url())
def get_initial(self):
initial = super().get_initial()
initial['comment'] = 'Please leave your comment here'
return initial
def get_context_data(self, **kwargs):
context = super().get_context_data()
context['comments'] = Comments.objects.all()
return context
and my condition from the template:
<div>
<div class="commentsContainer">
{% for comment in comments %}
{% if comment.commented_by == user.username %}
<h4>YOU</h4>
{% else %}
{{comment.commented_by}}
{% endif %}
<div class="comment">
{{comment}}
</div>
{% endfor %}
</div>
comment.commented_by will give you an instance of the user model, whereas user.username will give you a string which is the user's username. Hence comment.commented_by == user.username will always give you False. Instead you should write:
{% if comment.commented_by == user %}
<h4>YOU</h4>
{% else %}
{{ comment.commented_by }}
{% endif %}

Overriding get_context_data is blocking some context data going to the template

I am inheriting from CreateView to create a form for a model.
class NewBlogView(CreateView):
form_class = BlogForm
template_name = 'blog_settings.html'
def form_valid(self, form):
blog_obj = form.save(commit=False)
blog_obj.owner = self.request.user
blog_obj.slug = slugify(blog_obj.title)
blog_obj.save()
return HttpResponseRedirect(reverse('home'))
Here is my template code:
{% extends 'base.html' %}
{% block content %}
<h1>Create New User</h1>
<form action='' method='post'>{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Create Account' />
</form>
{% endblock %}
At this moment everything is working as expected, but when I am overriding get_context_data() my title field is disappearing.
class NewBlogView(CreateView):
form_class = BlogForm
template_name = 'blog_settings.html'
def form_valid(self, form):
blog_obj = form.save(commit=False)
blog_obj.owner = self.request.user
blog_obj.slug = slugify(blog_obj.title)
blog_obj.save()
return HttpResponseRedirect(reverse('home'))
def get_context_data(self, **kwargs):
ctx = super(NewBlogView, self).get_context_data(**kwargs)
print(ctx)
return ctx
I am thinking although I am running the original get_context_data() form the function that I am inheriting, there is something that is going wrong when it comes to taking the field name from the form_class. Can someone help with that confusion that I have?

inlineformset_factory create new objects and edit objects after created

In the django docs, there's an example of using inlineformset_factory to edit already created objects
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-an-inline-formset-in-a-view
I changed the example to be this way:
def manage_books(request):
author = Author()
BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',))
if request.method == "POST":
formset = BookInlineFormSet(request.POST, request.FILES, instance=author)
if formset.is_valid():
formset.save()
return HttpResponseRedirect(author.get_absolute_url())
else:
formset = BookInlineFormSet(instance=author)
return render_to_response("manage_books.html", {
"formset": formset,
})
With the above, it renders only the inline model without the parent model.
To create a new object, say Author, with multiple Books associated to, using inlineformset_factory, what's the approach?
An example using the above Author Book model from django docs will be helpful. The django docs only provided example of how to edit already created object using inlineformset_factory but not to create new one
I've done that using Django Class-Based Views.
Here's my approach:
models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
forms.py
from django.forms import ModelForm
from django.forms.models import inlineformset_factory
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset
from .models import Author, Book
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = ('name', )
#property
def helper(self):
helper = FormHelper()
helper.form_tag = False # This is crucial.
helper.layout = Layout(
Fieldset('Create new author', 'name'),
)
return helper
class BookFormHelper(FormHelper):
def __init__(self, *args, **kwargs):
super(BookFormHelper, self).__init__(*args, **kwargs)
self.form_tag = False
self.layout = Layout(
Fieldset("Add author's book", 'title'),
)
BookFormset = inlineformset_factory(
Author,
Book,
fields=('title', ),
extra=2,
can_delete=False,
)
views.py
from django.views.generic import CreateView
from django.http import HttpResponseRedirect
from .forms import AuthorForm, BookFormset, BookFormHelper
from .models import Book, Author
class AuthorCreateView(CreateView):
form_class = AuthorForm
template_name = 'library/manage_books.html'
model = Author
success_url = '/'
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
book_form = BookFormset()
book_formhelper = BookFormHelper()
return self.render_to_response(
self.get_context_data(form=form, book_form=book_form)
)
def post(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
book_form = BookFormset(self.request.POST)
if (form.is_valid() and book_form.is_valid()):
return self.form_valid(form, book_form)
return self.form_invalid(form, book_form)
def form_valid(self, form, book_form):
"""
Called if all forms are valid. Creates a Author instance along
with associated books and then redirects to a success page.
"""
self.object = form.save()
book_form.instance = self.object
book_form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, book_form):
"""
Called if whether a form is invalid. Re-renders the context
data with the data-filled forms and errors.
"""
return self.render_to_response(
self.get_context_data(form=form, book_form=book_form)
)
def get_context_data(self, **kwargs):
""" Add formset and formhelper to the context_data. """
ctx = super(AuthorCreateView, self).get_context_data(**kwargs)
book_formhelper = BookFormHelper()
if self.request.POST:
ctx['form'] = AuthorForm(self.request.POST)
ctx['book_form'] = BookFormset(self.request.POST)
ctx['book_formhelper'] = book_formhelper
else:
ctx['form'] = AuthorForm()
ctx['book_form'] = BookFormset()
ctx['book_formhelper'] = book_formhelper
return ctx
urls.py
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
from library.views import AuthorCreateView
urlpatterns = patterns('',
url(r'^author/manage$', AuthorCreateView.as_view(), name='handle-books'),
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
)
manage_books.html
{% load crispy_forms_tags %}
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<div class='container'>
<form method='post'>
{% crispy form %}
{{ book_form.management_form }}
{{ book_form.non_form_errors }}
{% crispy book_form book_formhelper %}
<input class='btn btn-primary' type='submit' value='Save'>
</form>
<div>
Notice:
This is a simple runable example that use the inlineformset_factory
feature and Django generic Class-Based Views
I'm assumming django-crispy-forms is installed, and it's properly
configured.
Code repository is hosted at: https://bitbucket.org/slackmart/library_example
I know it's more code that the showed solutions, but start to using Django Class-Based Views is great.
I didn't read your question properly at first. You need to also render the the form for the parent model. I haven't tested this, I'm going off what I've done before and the previously linked answer, but it should work.
UPDATE
If you're using the view to both and edit, you should check for an Author ID first. If there's no ID, it'll render both forms as a new instance, whereas with an ID it'll, fill them with the existing data. Then you can check if there was a POST request.
def manage_books(request, id):
if id:
author = Author.objects.get(pk=author_id) # if this is an edit form, replace the author instance with the existing one
else:
author = Author()
author_form = AuthorModelForm(instance=author) # setup a form for the parent
BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',))
formset = BookInlineFormSet(instance=author)
if request.method == "POST":
author_form = AuthorModelForm(request.POST)
if id:
author_form = AuthorModelForm(request.POST, instance=author)
formset = BookInlineFormSet(request.POST, request.FILES)
if author_form.is_valid():
created_author = author_form.save(commit=False)
formset = BookInlineFormSet(request.POST, request.FILES, instance=created_author)
if formset.is_valid():
created_author.save()
formset.save()
return HttpResponseRedirect(created_author.get_absolute_url())
return render_to_response("manage_books.html", {
"author_form": author_form,
"formset": formset,
})
I am posting my final solutions, as per extensive assistant given by Onyeka.
Below I post the Add and Edit solutions of using inlineformset_factory of Django using the Author and Book example found in the Django Docs.
First, the Adding of Author object, with 3 extras of Book object to be appended.
Obviously, this goes into your views.py
def add_author(request):
'''This function creates a brand new Author object with related Book objects using inlineformset_factory'''
author = Author()
author_form = AuthorModelForm(instance=author) # setup a form for the parent
BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',))
if request.method == "POST":
author_form = AuthorModelForm(request.POST)
formset = BookInlineFormSet(request.POST, request.FILES)
if author_form.is_valid():
created_author = author_form.save(commit=False)
formset = BookInlineFormSet(request.POST, request.FILES, instance=created_author)
if formset.is_valid():
created_author.save()
formset.save()
return HttpResponseRedirect(created_author.get_absolute_url())
else:
author_form = AuthorModelForm(instance=author)
formset = BookInlineFormSet()
return render(request, "add_author.html", {
"author_form": author_form,
"formset": formset,
})
def edit_author(request, author_id):
'''This function edits an Author object and its related Book objects using inlineformset_factory'''
if id:
author = Author.objects.get(pk=author_id) # if this is an edit form, replace the author instance with the existing one
else:
author = Author()
author_form = AuthorModelForm(instance=author) # setup a form for the parent
BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',))
formset = BookInlineFormSet(instance=author)
if request.method == "POST":
author_form = AuthorModelForm(request.POST)
if id:
author_form = AuthorModelForm(request.POST, instance=author)
formset = BookInlineFormSet(request.POST, request.FILES)
if author_form.is_valid():
created_author = author_form.save(commit=False)
formset = BookInlineFormSet(request.POST, request.FILES, instance=created_author)
if formset.is_valid():
created_author.save()
formset.save()
return HttpResponseRedirect(created_author.get_absolute_url())
return render(request, "edit_author.html", {
"author_id": author_id, # This author_id is referenced
# in template for constructing the posting url via {% url %} tag
"author_form": author_form,
"formset": formset,
})
This part goes into your urls.py, assuming views have been imported, and urlpatterns constructed already.
...
url(r'^add/book/$', views.add_author, name='add_author'),
url(r'^edit/(?P<author_id>[\d]+)$', views.edit_author, name='edit_author'),
...
Now to the templates part. The edit Author object template (edit_author.html) looks like this (no styling applied)
<form action="{% url 'edit_book' author_id %}" method="POST" >
<!-- See above: We're using the author_id that was passed to template via views render of the edit_author(...) function -->
{% csrf_token %} <!-- You're dealing with forms. csrf_token must come -->
{{ author_form.as_p }}
{{ formset.as_p }}
<input type="submit" value="submit">
</form>
To add a brand new Author object via template (add_author.html):
<form action="." method="POST" >{% csrf_token %}
{{ author_form.as_p }}
{{ formset.as_p }}
<input type="submit" value="submit">
</form>
NOTE:
Using the action='.' might appear to be a cheap way of constructing the url, whereby the form posts the form data to the same page. With this example, using the action='.' for the edit_author.html template always got the form posted to /edit/ instead of /edit/1 or /edit/2
Constructing the url using the {% url 'edit_author' author_id %} ensures the form always posts to the right url. Failing to do use the {% url %} cost me lots of hours and trouble.
Big thanks to Onyeka.
i did exactly what you are trying :
https://github.com/yakoub/django_training/tree/master/article
you need to create a separate form using the prefix attribute .
then when you save you need to iterate over all books and associate them with the author you just created .
This is my first django inline_formset view for create a invoice with list of invoice_item_set.
In models.py there are three models
Customer
it has customer data like name, mobile_no, his_address etc..
Invoice
it has invoice data like customer_primary_key(required), delivery_address, billed_date etc..
total of the invoice item can be achieved by getting all "invoiceitem_set.all()" as items and from that sum of all add(items.item_subtotal)
InvoiceItem
it has invoiceitem data like invoice_primary_key(required), item_name, quantity, price etc..
the total is calculated before the model is saves
models.py
class Customer(models.Model):
pass
class Invoice(models.Model):
customer_id = models.ForeignKey(Customer, on_delete=models.PROTECT) # many - to - on relationship
invoice_id = models.CharField(....)
bill_note = models.TextField(....)
cash_pay = models.DecimalField(....)
upi_pay = models.DecimalField(....)
#property
def total_amount(self):
bill_total = 0
items = self.invoiceitem_set.all()
for item in items:
bill_total += item.item_subtotal
return bill_total
class InvoiceItem(models.Model):
invoice = models.ForeignKey(Invoice) # many - to - one relationship
item_name = models.CharField(....)
item_quantity = models.DecimalField(....)
item_price = models.DecimalField(....)
item_subtotal = models.DecimalField(....)
def save(self, *args, **kwargs):
self.item_subtotal = self.item_quantity * self.item_price
super(InvoiceItem, self).save(*args, **kwargs)
views.py (CreateView)
from django.db import transaction
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse, HttpResponseRedirect
class InvoiceCreateView(LoginRequiredMixin, CreateView):
model = Invoice
form_class = InvoiceForm
template_name = 'billingsite/create_invoice.html'
def get_context_data(self, **kwargs):
context = super(InvoiceCreateView, self).get_context_data(**kwargs)
context['custom_title'] = "New Invoice"
temp = dict()
temp['customer_id'] = 0
if self.request.POST:
customer_id = int(self.request.POST.get('customer_id')) or False # custom clean method.
if customer_id:
customer_object = Customer.objects.get(pk=customer_id)
invoice_object = Invoice.objects.filter(customer_id=customer_object).order_by('-created_time').first()
temp = {
"customer_id": customer_id, "mobile_no": customer_object.mobile_no,
"searched_mobile_no": customer_object.raw_mobile_no,
"customer_name": customer_object.name, "gst_no": customer_object.goods_tax_id,
"pre_bal": customer_object.pending_balance, "purchased_date": "No Bills",
"created_date": customer_object.created_date.strftime(CUSTOM_DATE_FORMAT)
}
context['formset'] = InvoiceFormSet(self.request.POST)
else:
context['formset'] = InvoiceFormSet()
context['temp'] = temp
return context
def post(self, request, *args, **kwargs):
self.object = None
context = self.get_context_data()
customer_id = int(self.request.POST.get('customer_id')) # custom clean method.
if customer_id and customer_id != 0:
customer_object = Customer.objects.get(pk=customer_id)
form_class = self.get_form_class()
form = self.get_form(form_class)
formsets = context['formset']
with transaction.atomic():
form.instance.customer_id = customer_object
form.save(commit=False)
if form.is_valid() and formsets.is_valid():
self.object = form.save()
messages.success(self.request, f'Invoice is Submitted.')
return self.form_valid(form, formsets)
else:
return self.form_invalid(form, formsets)
return reverse_lazy('InvoiceList')
return self.render_to_response(context)
def form_valid(self, form, formsets):
formsets = formsets.save(commit=False)
for formset in formsets:
formset.invoice = self.object
formset.save()
return HttpResponseRedirect(self.get_success_url(self.object.pk))
def form_invalid(self, form, formsets):
return self.render_to_response(
self.get_context_data(form=form, formset=formsets))
def get_success_url(self, pk):
return reverse_lazy('ViewInvoice', kwargs={'pk': pk})
urls.py
urlpatterns = [
path('invoice/create/', views.InvoiceCreateView.as_view(), name='AddInvoice'),
]
forms.py
class InvoiceItemForm(forms.ModelForm):
item_name = forms.CharField(label=_('Product Name'))
item_subtotal = forms.IntegerField(required=False, label=_('Sub Total'))
class Meta:
model = InvoiceItem
fields = ['item_name', 'item_quantity', 'item_price', 'item_subtotal']
exclude = ()
widgets = {
"item_quantity": widgets.NumberInput(attrs={'step': '0.25'}),
"item_price": widgets.NumberInput(attrs={'step': '0.25'})
}
def __init__(self, *args, **kwargs):
super(InvoiceItemForm, self).__init__(*args, **kwargs)
self.fields['item_name'].widget.attrs['placeholder'] = 'Enter the food name'
self.fields['item_quantity'].widget.attrs['placeholder'] = 'Pieces'
self.fields['item_price'].widget.attrs['placeholder'] = 'in ₹'
self.fields['item_subtotal'].widget.attrs['readonly'] = True
self.fields['item_subtotal'].widget.attrs['tabindex'] = -1
for field in self.fields.values():
field.widget.attrs['class'] = 'form-control'
class InlineFormSet(forms.BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super(InlineFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
InvoiceFormSet = forms.inlineformset_factory(
Invoice, InvoiceItem, fields=('__all__'),
form=InvoiceItemForm, formset = InlineFormSet,
extra=0, min_num=1, can_delete=True
)
create_invoice.html
<fieldset>
<div class="text-dark py-4 table-responsive">
<div class="inline-formset inline-group" id="{{ formset.prefix }}-group" data-inline-type="tabular"
data-inline-formset="{
"name": "#{{ formset.prefix }}",
"options": {
"prefix": "{{ formset.prefix }}",
"addText": "Add+",
"deleteText": "<i class='bi bi-x'></i>",
"formCssClass": "dynamic-{{ formset.prefix }}",
}
}">
{% csrf_token %}
<div class="tabular inline-related">
{{ formset.management_form }}
<table id="invoice-table" class="as-table table table-xl table-hover caption">
<div class="d-block invalid-feedback">{{ formset.non_form_errors }}</div>
<caption>Add list of items.</caption>
{% for form in formset.forms %}
{% if forloop.first %}
<thead class="text-light">
<tr class="text-center">
<th scope="col">#</th>
{% for field in form.visible_fields %}
<th scope="col">{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% endif %}
<tr scope="row" class="form-row" id="{{ formset.prefix }}-{{ forloop.counter0 }}">
<th class="original">
<div class="index">{{ forloop.counter1 }}</div>
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
</th>
{% for field in form.visible_fields %}
<td class="field-{{ field.name }}">
{% if field.name != "DELETE" %}
{% if field.errors %}
{{ field|addCls:"is-invalid" }}
{% else %}
{{ field }}
{% endif %}
{% if field.errors %}
<div class="invalid-feedback">{{ field.errors }}</div>
{% endif %}
{% else %}
{{ field }}
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
<tr scope="row" class="form-row empty-row" id="{{ formset.prefix }}-empty">
<th class="original">
<div class="index">__prefix__</div>
{% for field in formset.empty_form.hidden_fields %}
{{ field }}
{% endfor %}
</th>
{% for field in formset.empty_form.visible_fields %}
<td class="field-{{ field.name }}">
{% if field.name != "DELETE" %}
{{ field }}
{% endif %}
</td>
{% endfor %}
</tr>
</tbody>
</table>
</div>
</div>
</div>
</fieldset>

What is the best way to implement Query Form in Django ListView?

Last week I faced a task related to listView and Forms in Django, I was wondering what is the best way (+Pythonic) to implement a search form inside a ListView, after I read 1 and 2 I got a main idea so I implemented a first solution and I would like to receive your Feedback. The goal here is to perform query by code field and keep the queryset in order to synchronize it with the pagination.
forms.py
class InscriptionQueryForm(forms.Form):
query_inscription = forms.CharField(label=_('Code'), required=False)
models.py
class Inscription(models.Model):
code = models.CharField(max_length=10, unique=True)
start_on = models.DateField()
finish_on = models.DateField()
active = models.BooleanField(default=False)
views.py
class InscriptionListView(ListView, FormMixin):
model = Inscription
paginate_by = 4
context_object_name = 'inscriptions'
form_class = InscriptionQueryForm
form = None
object_list = None
search = False
def get_queryset(self):
form = self.form_class(self.request.POST)
if form.is_valid() and self.request.method == 'POST':
self.request.session['query_inscription'] = \
form.cleaned_data['query_inscription']
return self.model.objects.filter(
code__icontains=form.cleaned_data['query_inscription']).\
order_by('-active')
if self.request.method == 'GET' and \
'query_inscription' in self.request.session:
return self.model.objects.filter(
code__icontains=self.request.session.get(
'query_inscription', '')).order_by('-active')
return self.model.objects.all().order_by('-active')
def get(self, request, *args, **kwargs):
# From ProcessFormMixin
self.form = self.get_form(self.form_class)
# From BaseListView
if self.request.GET.get('page', False) or self.search:
self.object_list = self.get_queryset()
else:
self.search = False
self.object_list = self.model.objects.all().order_by('-active')
if 'query_inscription' in self.request.session:
del self.request.session['query_inscription']
context = self.get_context_data(
object_list=self.object_list, form=self.form)
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
self.search = True
return self.get(request, *args, **kwargs)
What do you think guys?, I'm sure there are many others better ways.
Last week I faced the similar problem. My model was a common django User. However the same approach can be used here.
I guess you want to search through your Inscriptions using search field and paginating your results. And when you go through the pages you expect to see the results of your search query.
Your models.py stays as it is. We are going to modify forms.py as it will be able to initialize the search request
class InscriptionQueryForm(forms.Form):
query_inscription = forms.CharField(label='Code', required=False)
def __init__(self, query_inscription):
super(InscriptionQueryForm, self).__init__()
self.fields['query_inscription'].initial = query_inscription
Now let's look at the views.py. You don't need to store your request value in the session as schillingt mentioned in his comment. All you need is to init your form with the search request.
class InscriptionListView(ListView):
model = Inscription
paginate_by = 4
context_object_name = 'inscriptions'
query_inscription = ''
def get_context_data(self, **kwargs):
context = super(InscriptionListView, self).get_context_data(**kwargs)
context['form'] = InscriptionQueryForm(self.query_inscription)
# this parameter goes for the right pagination
context['search_request'] = ('query_inscription=' +
unicode(self.query_inscription))
return context
def get(self, request, *args, **kwargs):
self.query_inscription = request.GET.get('query_inscription', '')
return super(InscriptionListView, self).get(request, *args, **kwargs)
def get_queryset(self):
if not self.query_inscription:
inscription_list = Inscription.objects.all()
else:
inscription_list = Inscription.objects.filter(
code__icontains=self.query_inscription)
return inscription_list.order_by('-active')
And the last thing to mention is the pagination
inscription_list.html
<form action="">
{{ form.as_p }}
<input type="submit" value="search"/>
</form>
<hr/>
{% if inscriptions %}
{% for inscription in inscriptions %}
{{ inscription.code }} {{ inscription.start_on }} {{ inscription.finish_on }}
<hr/>
{% endfor %}
{% if is_paginated %}
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
previous
{% endif %}
<span class="page-current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
</span>
{% if page_obj.has_next %}
next
{% endif %}
</span>
</div>
{% endif %}
{% endif %}
That's it!