Filter - How to implement - django

I need to implement multi-choice filter in my Django software.
I have my model (database):
models.py
class Autor(models.Model):
naziv = models.CharField(max_length=30, null=False, blank=True)
email = models.EmailField(max_length=75, null=True, blank=True)
def __str__(self):
return str(self.naziv)
class Clanak(models.Model):
naslov = models.CharField(null=False, blank=True, max_length=120)
datumObjave = models.DateField(null=False, blank=False)
autor = models.ForeignKey(Autor, on_delete=models.CASCADE, null=True)
def __str__(self):
return str(self.naslov) + ', ' + str(self.datumObjave) + ', ' + str(self.autor)
My urls.py:
urlpatterns = [
path('filtar/',views.filtar, name='filtar'),
]
Views.py:
def filtar(request):
form = ChoiceForm(request.GET or None)
data = Clanak.objects.all()
if form.is_valid():
if 'Choice 1' in form.cleaned_data['filter']:
data = data.filter(naslov='name')
if 'Choice 2' in form.cleaned_data['year']:
data = data.filter(datumObjave__year='year')
return render(request, 'filtar.html', {'data': data, 'form': form})
Filtar.html:
<!DOCTYPE html>
<html>
<head>
{% extends 'base.html' %}
{% block main_content %}
<title></title>
</head>
<body>
<table border="1">
<tr>
<th>Naslov</th>
<th>Datum</th>
<th>Autor</th>
</tr>
{% for x in data %}
<tr>
<td>{{x.naslov}}</td>
<td>{{x.datumObjave}}</td>
<td>{{x.autor}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
{% endblock %}
footer.html
<br>
<div>Filter: </div>
<form>
{% csrf_token %}
<fieldset>
<legend>Filtar - Thing that is being chosen</legend>
{{ form.as_p }}
<input type="submit" value="Submit">
</fieldset>
</form>
<br>
<div>Copyright by </div>
Forms.py
class ChoiceForm(forms.Form):
filter = forms.MultipleChoiceField(choices=(('Choice 1', 'Choice 1'), ('Choice 2', 'Choice 2')), widget=forms.CheckboxSelectMultiple)
name = forms.CharField(label='name')
year = forms.CharField(label='year')
Screenshot:
Now my question is:
My problem is that whatever I write in textfields it do nothing, just refresh page with all same data.
Name of dynamically created textboxes should be "nameinput" and "yearinput"

Your form is going to send GET request to the same page. So when view start processing, it fetch both choice values (if they exists), get all Clanak objects and filter them based on selected choices.
This way if no filters selected, you will get all objects. If both filters selected, it will filter both on names and year.
def filtar(request):
choice1 = request.GET.get('Autor', None)
choice2 = request.GET.get('Datum', None)
data = Clanak.objects.all()
if choice1:
data = data.filter(naslov='NAME')
if choice2:
data = data.filter(datumObjave__year=2019)
return render(request, 'filtar.html', {'data': data})
Note that it will not actually execute query before you try to access it (i.e. not until you render html).
Additionally, I would suggest you switch this form to django form, like this:
forms.py:
class ChoiceForm(forms.Form):
filter = forms.MultipleChoiceField(choices=(('Choice 1', 'Choice 1'), ('Choice 2', 'Choice 2')), widget=forms.CheckboxSelectMultiple)
views.py:
def filtar(request):
form = ChoiceForm(request.GET or None)
data = Clanak.objects.all()
if form.is_valid():
if 'Choice 1' in form.cleaned_data['filter']:
data = data.filter(naslov='NAME')
if 'Choice 2' in form.cleaned_data['filter']:
data = data.filter(datumObjave__year=2019)
return render(request, 'filtar.html', {'data': data, 'form': form})
footer.html:
<br>
<div>Filter: </div>
<form>
{% csrf_token %}
<fieldset>
<legend>Filtar - Thing that is being chosen</legend>
{{ form.as_p }}
<input type="submit" value="Submit">
</fieldset>
</form>
<br>
<div>Copyright by </div>

Related

Django form scrubs blank spaces

I have a model with a text field:
models.py
class Techtip(models.Model):
title = models.CharField(max_length=150)
year = models.PositiveIntegerField()
year2 = models.PositiveIntegerField()
make = models.CharField(max_length=30)
model = models.CharField(max_length=30)
description = models.TextField(max_length=10000)
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
date_created = models.DateTimeField(auto_now_add=True)
date_revised = models.DateTimeField(null=True)
additional_field = models.TextField(max_length=5000, null=True, blank=True)
additional_field2 = models.TextField(max_length=5000, null=True, blank=True)
image1 = models.ImageField(upload_to=user_directory_path, null=True, blank=True)
image2 = models.ImageField(upload_to=user_directory_path, null=True, blank=True)
image3 = models.ImageField(upload_to=user_directory_path, null=True, blank=True)
def __str__(self):
return self.title
If a create a Techtip and give it a description of:
"Hello, This is line one of the disctription.
This is line two.
and this is line 3."
When using {{techtip.deescription}} in the template I receive this:
"Hello, This is line one of the disctription.
This is line two.
and this is line 3."
However, if you bring up a form to edit the description, the spaces are there. It is also displayed correctly in the admin panel.
Here is the form:
forms.py
class TechtipFormModel(forms.ModelForm):
"""This form creates and edits techtips."""
class Meta:
model = Techtip
fields = '__all__'
exclude = ('user', 'date_revised', 'additional_field', 'additional_field2', 'image1', 'image2', 'image3')
def __init__(self, *args, **kwargs):
super(TechtipFormModel, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-TechtipForm'
self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Submit'))
self.fields['description'].strip = False
Here are the views for editing a Techtip or displaying one. The spacing works correctly when using the edit form but not while displaying a Techtip.
views.py
#login_required
def techtip_detail(request, pk):
# display techtip details. In template: if user is the creator of Techtip they can update/delete
tech_id = pk
user = request.user
techtip = Techtip.objects.get(pk=tech_id)
context = {'techtip': techtip}
return render(request, 'techtips/view_techtip.html', context)
class TechtipEdit(LoginRequiredMixin,
UpdateView,):
model = Techtip
form_class = TechtipFormModel
template_name = 'techtips/edit_techtip.html'
def get_success_url(self):
return reverse('manage_techtips')
def dispatch(self, request, *args, **kwargs):
# check for user logged in
# check for user permission:
# Take pk from kwargs
pk = kwargs.get('pk') # example
# Take user from request
user = request.user
# check permission
try:
Techtip.objects.get(pk=pk, user=user)
return super(TechtipEdit, self).dispatch(request, *args, **kwargs)
except Techtip.DoesNotExist:
return HttpResponse(status=403)
and here are the templates!
view_techtip.html
{% extends "techtips/base.html" %}
{% block body %}
<div class="row">
<div class="col-md-12">
<div class="container-fluid" align="left">
<div id="punchlist">
<h3 style="margin-bottom: 0px;">Title:</h3>{{ techtip.title }}<br>
<h3 style="margin-bottom: 0px; margin-top: 20px;">Make:</h3> {{ techtip.make }}<br>
<h3 style="margin-bottom: 0px; margin-top: 20px;">Model:</h3> {{ techtip.model }}<br>
<h3 style="margin-bottom: 0px; margin-top: 20px;">Beginning Year:</h3> {{ techtip.year }}<br>
<h3 style="margin-bottom: 0px; margin-top: 20px;">Ending Year:</h3> {{ techtip.year2 }}<br>
<h3 style="margin-bottom: 0px; margin-top: 20px;">Description:</h3> {{ techtip.description }}<br>
</div>
</div>
</div>
</div>
{% if techtip.user.id == user.pk or request.user.is_superuser == TRUE %}
<br>
<form method="GET" action="{% url 'edit_techtip' techtip.pk %}">
<span class="techtip-button-1">
<button type="submit" value="EDIT">Edit</button>
</span>
</form>
{% endif %}
{% if request.user.is_superuser == TRUE %}
<form method="POST" action="{% url 'delete_techtip' techtip.pk %}">
{% csrf_token %}
<span class="techtip-button-1">
<button type="submit" value="DELETE">Delete</button>
</span>
</form>
{% endif %}
<br><br>
<br>
{% if techtip.user.id != user.pk %}
Author: {{techtip.user}}
{% endif %}
{% if techtip.user.id == user.pk %}
Author: You
{% endif %}
{% endblock %}
and
edit_techtip.html
{% extends 'techtips/base.html' %}
{% block body %}
{% load crispy_forms_tags %}
{% crispy form form.helper %}
{% endblock %}
Thanks in advance!
I did some research and added
self.fields['description'].strip = False
to the form class but then later realized that wouldn't help because this form class has no part of passing an object into the template and using Django template language.

django upload multiple images use form

I'm making a bulletin board. I want to post several images in one post
However, no matter how hard I try to find a way to create multiple using the form.I want to upload several files at once.
I created an image upload function, but it doesn't show on the web
I don't know where it went wrong
models.py
class Writing(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
subject = models.CharField(max_length=200)
content = models.TextField()
create_date = models.DateTimeField()
modify_date = models.DateTimeField(null=True, blank=True)
view_count = models.IntegerField(default=0)
def __str__(self):
return self.subject
class WritingImage(models.Model):
writing = models.ForeignKey(Writing, on_delete=models.CASCADE)
image = models.ImageField(upload_to='images/%Y/%m', blank=True, null=True)
forms.py
class WritingForm(forms.ModelForm):
captcha = ReCaptchaField(label=('로봇확인'))
class Meta:
model = Writing
fields = ['subject', 'content']
labels = {
'subject': '제목',
'content': '내용',
}
class WritingFullForm(WritingForm):
images = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
class Meta(WritingForm.Meta):
fields = WritingForm.Meta.fields + ['images', ]
views.py
#login_required(login_url='account_login')
def writing_create(request):
"""
글작성
"""
if request.method == 'POST':
form = WritingFullForm(request.POST, request.FILES)
files = request.FILES.getlist('images')
if form.is_valid():
writing = form.save(commit=False)
writing.author = request.user
writing.create_date = timezone.now()
writing.save()
if files:
for f in files:
WritingImage.objects.create(writing=writing, image=f)
return redirect('ourtube:index')
else:
form = WritingFullForm()
context = {'form': form}
return render(request, 'ourtube/writing_form.html', context)
form.html
<div class="form-group">
<label for="note-image">Images</label>
<input type="file" name="images" class="form-control-file" id="note-image" multiple>
</div>
detail.html
<h1>{{ writing.view_count }} 회</h1>
<h1>{{ writing.id }}</h1>
<h1>{{ writing.subject }}</h1>
<div>
{{ writing.content }}
</div>
{% for photo in writing.image_set.all %}
<li>
{{ photo.image }}
</li>
{% endfor%}
<div>
{% for photo in writing.photo_set.all %}
{{ photo.image.url }}
<img src="{{ photo.image.url }}" style="width: 100%; float: left; margin-right: 10px;" />
{% endfor %}
</div>
{% for photo in writing.image_set.all %} << Is the image not saved in the writing?
please help me
If you are using save(commit = False) you must call save_m2m(). Link to documentation.

My form keep saying "This(image) field is required!" Django 3.0

I made a project with net-ninja on youtube now I am adding "uploading media" feature in my own project but it is not working although every thing is set just tell me where i m going wrong.
My form is keep asking me to upload image file even though i had already done, every time i send a post request it renders the page again with no value in image field. what is wrong here, why it is not taking image input?Don't comment set the required to false that is not the solution i don't know why some people said this to others on stackoverflow when they asked the same question as me.
My model class looks like this
class Products(models.Model):
name = models.CharField(max_length=500, unique=True, )
price = models.IntegerField()
stock = models.IntegerField()
date_added = models.DateTimeField(verbose_name="date added", auto_now_add=True, )
thumb = models.ImageField(default='default.png', blank=True)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, default=None,)
def __str__(self):
return self.name
class Meta():
verbose_name_plural = "Products"
My form looks like this
class AddProduct(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Product Name', 'required':'True', 'class': 'form-control'}))
price = forms.IntegerField(widget=forms.NumberInput(attrs={'placeholder':'Set Price', 'required':'True', 'class': 'form-control'}))
stock = forms.IntegerField(widget=forms.NumberInput(attrs={'placeholder':'Number of items', 'required':'True', 'class': 'form-control'}))
thumb = forms.ImageField(required=False, widget=forms.ClearableFileInput(attrs={'placeholder':'Upload Picture', 'enctype' : 'multipart/form-data'}))
class Meta():
model = Products
fields = ('name', 'price', 'stock','thumb', )
HTML looks like this
<form class="row contact_form" action="/shop/add_products" method="post">
{% csrf_token %}
{% for field in form %}
<div class="col-md-12 form-group p_star">
{{ field }}
{{ field.errors }}
</div>
{% endfor %}
<!-- {% for field in form %}
{% for error in field.errors %}
<div class="col-md-12 form-group p_star">
{{ error }}
</div>
{% endfor %}
{% endfor %} -->
{% if form.non_field_errors %}
<div class="col-md-12 form-group p_star">
{{ form.non_field_errors }}
</div>
{% endif %}
<div class="col-md-12 form-group">
<button type="submit" value="submit" class="btn_3">
Add Product
</button>
</div>
</form>
My views file looks like this
#login_required(login_url='/shop/login')
def shop_add_products(request):
if request.method == 'POST':
form = AddProduct(request.POST, request.FILES)
if form.is_valid():
instance = form.save(commit=False)
instance.profile = request.user
instance.save()
return redirect("/shop/products")
else:
form = AddProduct()
return render(request, "pillow_site_html/add_product.html", { 'form':form })
Oh sorry i didn't understood the question here, you are getting that because in fact you are not sending the picture inside your form as you have to add this to your form so it can accept files handling
<form class="row contact_form" action="/shop/add_products" method="post" enctype="multipart/form-data">

Uploading multiple images in Django for a single post

I am beginner in django . I would like to make an application that allows a user to record examinations and related images.
So i try to uploading multiple images in Django for a single post according this topic http://qasimalbaqali.com/uploading-multiple-images-in-django-for-a-single-post/.
But nothing happens when I press the button
I explain my models. A document is a generic class. An exam is a document, a document contains files.
class Document(models.Model):
class Meta:
db_table = 'Document'
slug = models.SlugField(max_length=100)
user = models.ForeignKey(User)
level = models.ForeignKey(ClassLevel, null=False, default=1)
school = models.ForeignKey(School, null=False, default=1)
nb_views = models.IntegerField(default=0)
name = models.CharField(max_length=100)
matter = models.ForeignKey(ClassTopic, null=False, default=1)
status = models.IntegerField(choices=DOCUMENT_STATUS, default=1)
creation_date = models.DateTimeField(auto_now_add=True)
deletion_date = models.DateTimeField(auto_now_add=False, default=None, null=True)
def __unicode__(self):
return self.name + " (" + str(self.status) + ") " + self.school.name
class DocumentFile(models.Model):
class Meta:
db_table = 'DocumentFile'
file = models.FileField(upload_to="photo/", null=True)
document = models.ForeignKey(Document)
def __unicode__(self):
return self.file
class Exam(Document):
class Meta:
db_table = 'Exam'
year_exam = models.IntegerField(choices=EXAM_YEAR_CHOICES, default=1)
mock_exam = models.IntegerField(choices=EXAM_TYPE, default=1)
def __unicode__(self):
return self.name + " " + self.matter
I create two forms. For exam and for file.
class UploadFileForm(ModelForm):
#description = forms.CharField(max_length=30)
file = forms.FileInput()
helper = FormHelper()
helper.form_id = 'file-input'
helper.form_show_labels = False
helper.layout = Layout(PrependedText('file', "", placeholder=""))
#helper.layout.insert(1, HTML("<input type='file' class='file' multiple data-show-upload='false' data-show-caption='true'>"))
class Meta:
model = DocumentFile
fields = ('file',)
#exclude = ("file_type", "file_path", "document")
class CreateExamForm(forms.ModelForm):
helper = FormHelper()
helper.form_id = 'CreateExam'
helper.form_show_labels = False
helper.layout = Layout(
PrependedText("matter", "", ""),
PrependedText("level", "<small class='text-warning'>Selectionner la classe. </small>", ""),
PrependedText("school", "<pre><small>Selectionner l\'établissement. </small></pre>", css_class="selectpicker"),
PrependedText("year_exam", ""),
PrependedText("mock_exam", ""))
class Meta:
model = Exam
exclude = ("slug", "user", "nb_views", "name", "status", "creation_date", "deletion_date")
My view
def createexam(request):
# Creation du formulaire + upload des images
doc_form = CreateExamForm(auto_id=True)
# Création du formset avec n itération : extra=2
file_form_set = modelformset_factory(DocumentFile, form=UploadFileForm, extra=1)
# Récupération du formulaire géré par le mécanisme formset
#formset = sortedfilesform()
if request.method == "POST":
doc_form = CreateExamForm(request.POST)
files_form = file_form_set(request.POST, request.FILES, queryset=DocumentFile.objects.none())
if doc_form.is_valid() and files_form.is_valid():
doc_save = doc_form.save(commit=False)
doc_save.user = request.user
for fileform in files_form.cleaned_data:
image = fileform['file']
one_image = DocumentFile(document=doc_save, file_value=image)
one_image.save(commit=False)
transaction.commit()
msg = FORM_PROPERTIES.FORM_EXAM_STORED.replace("user", request.user.nickname)
messages.add_message(request, messages.SUCCESS, msg)
form = LoginForm()
context = {'form': form}
return render_to_response(template_name='login.html', context=context, context_instance=RequestContext(request))
else:
messages.add_message(request, messages.SUCCESS, FORM_PROPERTIES.FORM_EXAM_ERROR)
context = {'doc_form': doc_form, 'file_form_set': file_form_set, }
return render(request, 'createexam.html', context)
else:
context = {'doc_form': doc_form, 'file_form_set': file_form_set, }
return render(request, 'createexam.html', context)
my url.py
urlpatterns = [
# Examples:
url(r'^$', home, name='home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', home),
url(r'^home', home),
url(r'^login', login),
url(r'^logout', logout),
url(r'^register$', register),
url(r'^createexam$', createexam),
url(r'^account/reset_password', reset_password, name="reset_password"),
]
My template is simple
<div id="login-overlay" class="modal-dialog">
<div class="row">
<div class="panel panel-info" >
<div class="panel-heading">
<div class="panel-title">Créer un examen</div>
</div>
<div class="panel-body" >
<div class="col-sm-12">
<form id="post_form" method="POST" action='.'
enctype="multipart/form-data">
{% csrf_token %}
{% for hidden in doc_form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in doc_form %}
{{ field }} <br />
{% endfor %}
{{ file_form_set.management_form }}
{% for form in file_form_set %}
{% crispy form %}
{% endfor %}
<input type="submit" value="Add recipe" class="submit" />
</form>
</div>
</div>
</div>
</div>
Everything seems good form me. But my submit does no think.
I 'm on it for two days.
I read and tried many things. Does anybody can help me please (Sorry for my english)
There are a couple of things wrong here.
Firstly you are not showing the form errors in the template, so the user has no way of knowing why their submission is invalid. Make sure you either do {{ field.errors }} after every field, or do {{ form.errors }} for the whole form at once; and remember to do this both form the main form and the image formset.
Secondly, you're saving the forms with commit=False, so they are not persisted to the database. It's fine to do that if you want to add extra data not included in the form, but you then need to remember to actually persist the object by calling eg doc_save.save().
I solved my problem when i put my main form body in <table> ... </table>
<form id="CreateExamForm" method="POST" enctypr="multipart/form-data">
{% csrf_token %}
<table>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Classe - Matière - Date</h3>
<span class="pull-right"><i class="glyphicon glyphicon-chevron-up"></i></span>
</div>
<div class="panel-body">
{% crispy doc_form %}
{{ file_form_set.management_form }}
{% for f_form in file_form_set %}
<div class="form-inline">
{% crispy f_form %}
</div>
{% endfor %}
</div>
</div>
</table>
<input type="submit" value="Add recipe" class="submit" />
</form>

Django 'unicode' object has no attribute 'field'

I recently added crispy_forms to my django project and it caused me to get the 'unicode' object has no attribute 'field' error. Can't figure out why.
models.py
class Trip(models.Model):
location_name = models.CharField(max_length=60)
trip_date = models.DateField()
trip_rating = models.IntegerField(validators=[MinValueValidator(1),MaxValueValidator(5)])
fishing_vehicle = models.ForeignKey(FishingVehicle)
water_body = models.ForeignKey(WaterBody)
user = models.ForeignKey(User)
def __unicode__(self):
return self.location_name
views.py
#login_required
def logtrip(request):
if request.method == 'POST':
form = forms.TripForm(request.POST)
if form.is_valid():
trip = form.save(commit=False)
trip.user = request.user
trip.save()
return redirect('home')
else:
print form.errors
else:
form = forms.TripForm()
return render_to_response('logtrip.html', {'form': form}, context_instance=RequestContext(request))
forms.py
class TripForm(ModelForm):
CHOICES = (('1', 'None',), ('2', 'Below Average Amount',), ('3', 'Average Amount',), ('4', 'Above Average Amount',), ('5', 'A Lot/Limited Out',))
trip_rating = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES, label='Fish Caught')
class Meta:
model = Trip
exclude = ['user']
widgets = {'trip_date': forms.DateInput(attrs={'class':'datepicker'})}
logtrip.html
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<!-- Example row of columns -->
<div class="row">
<div class="col-md-4">
<form action="/logtrip/" method="post">
{% csrf_token %}
{{ form.as_p|crispy }}
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
{% endblock %}
Everything works correctly when I remove the |crispy filter.
For anyone else getting this error, my problem was solved by changing {{ form.as_p|crispy }} to {{ form|crispy }}.