How can I include an image in a Django contact form? - django

I need to have users send me an email through my website with the front and back of their ID for verification. Why is my form returning "This field is required." when everything is filled in? What did I do wrong?
views.py
def market(request):
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
from_email = form.cleaned_data['from_email']
subject = form.cleaned_data['subject']
address = form.cleaned_data['address']
id_front = form.cleaned_data['id_front']
additional_details = form.cleaned_data['additional_details']
try:
send_mail(from_email, subject, id_front, address, additional_details, ['admin#example.com'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('market_success')
return render(request, "market.html", {'form': form})
def market_success(request):
return HttpResponse('Success! We have received your request and will get back to you soon.')
forms.py
class ContactForm(forms.Form):
from_email = forms.EmailField(required=True)
subject = forms.CharField(required=True)
address = forms.CharField(required=True)
id_front = forms.ImageField(required=True)
additional_details = forms.CharField(widget=forms.Textarea, required=True)
html
{% extends 'base.html' %}
{% block title %}Market{% endblock %}
{% block content %}
<p>To request to market your property with us, please fill out the form below.</p>
<form method="post" enctype="multipart/form-data" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<div class="form-actions">
<button type="submit">Send</button>
</div>
</form>
{% endblock %}

Related

django's clean method doesn`t load form errors

i have a problem with Django clean method because clean method of form doesn`t load error in template. Could someone help me ?
template.html
{% extends "index.html" %}
{% block header %}
<div id="container-register">
<div class="logo-register">Zarejestruj się</div>
<div class="register-form">
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field }} {{ field.errors }} <br>
{% endfor %}
<input type="submit" value="Zarejestruj">
</form>
</div>
</div>
{% endblock %}
view.py
class AddUserView(View):
template_name = 'add_user.html'
def get(self,request):
return render(request, self.template_name,{
'form': AddUserForm()
})
def post(self,request):
form = AddUserForm(request.POST)
if form.is_valid():
User.objects.create_user(
username=form.cleaned_data.get('username'),
email=form.cleaned_data.get('email'),
password=form.cleaned_data.get('password'),
first_name=form.cleaned_data.get('first_name'),
last_name=form.cleaned_data.get('last_name')
)
return redirect('/login')
else:
return render(request, self.template_name, context={
'form': AddUserForm()
})
forms.py
class AddUserForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Nazwa użytkownika'}), max_length=100)
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Hasło'}), max_length=100)
password_repeat = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Powtórz hasło'}),
max_length=100)
first_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Imię'}), max_length=100)
last_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Nazwisko'}), max_length=100)
email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Email'}), max_length=100)
def clean_username(self):
if User.objects.filter(username=self.cleaned_data.get('username')).exists():
raise ValidationError('Ten login jest już zajęty')
return self.cleaned_data.get('username')
def clean_password_repeat(self):
if self.cleaned_data.get('password') != self.cleaned_data.get('password_repeat'):
raise ValidationError('Podane hasła różnią się od siebie!')
return self.cleaned_data.get('password_repeat')
I checked the page source to see if the errors class was added in the html file.
In your post method, you create a new form in the template context instead of reuse the existing with data and errors :
return render(request, self.template_name, context={
'form': form
})

Django: Create an editable form for each instance within a queryset all in one page?

Sorry if this is too much code, but I believe it is all relevant to the question at hand.
Long story short, on my series_detail page, all episodes belonging to each series is shown, as well as forms to add a new episode or edit an existing one.
The edit episode form, however, requires an instance, which always returns the very first object of the episodes queryset. This is presumably because of the .first(), but I used this since you can only have one object passed as an instance.
What I am trying to achieve is:
after showing the edit modal next to each episode, show the instance of each episode instead of only the first episode.
save only that episode's instance after the form is filled
achieve this without redirecting to an edit page
models.py
class Series(models.Model):
name = models.CharField(max_length=100)
class Episode(models.Model):
series = models.ForeignKey(Series, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
episode_no = models.IntegerField(null=True)
description = models.TextField(max_length=500)
image = models.ImageField(upload_to='pics/episodes',)
forms.py
class EpisodeForm(forms.ModelForm):
name = forms.CharField()
description = forms.CharField(widget=forms.Textarea, required=False)
episode_no = forms.IntegerField()
class Meta:
model = Episode
fields = ['name', 'description', 'episode_no' ,'image']
views.py
def series_detail(request, pk):
try:
series = Series.objects.get(pk=pk)
except:
return render(request, 'error_404.html')
episodes = Episode.objects.filter(series=series).first()
if request.method == 'POST':
if 'addepisodeform' in request.POST:
e_form = EpisodeForm(request.POST, request.FILES, prefix='addepisode')
e_form.instance.series = Series.objects.get(pk=pk)
if e_form.is_valid():
e_form.save()
return redirect('series_detail', pk=pk)
messages.success(request, 'Episode was created')
else:
return redirect('series_detail', pk=pk)
messages.error(request, 'Episode was not created')
elif 'editepisodeform' in request.POST:
edit_e_form = EpisodeForm(request.POST, request.FILES, instance=episodes, prefix='editepisode')
edit_e_form.instance.series = Series.objects.get(pk=pk)
if edit_e_form.is_valid():
edit_e_form.save()
return redirect('series_detail', pk=pk)
messages.success(request, 'Episode was updated')
else:
return redirect('series_detail', pk=pk)
messages.error(request, 'Episode was not updated')
else:
e_form = EpisodeForm(prefix='addepisode')
edit_e_form = EpisodeForm(instance=episodes, prefix='editepisode')
context = {
'episodes': episodes,
'e_form': e_form,
'edit_e_form': edit_e_form
}
return render(request, 'series/series_detail.html', context)
def delete_episode(request, pk1, pk2):
try:
series = Series.objects.get(pk=pk1)
except:
return render(request, 'error_404.html')
try:
episode = Episode.objects.get(series=series, episode_no=pk2)
except:
return render(request, 'error_404.html')
episode.delete()
return redirect('series_detail', pk=pk1)
urls.py
path('series/<int:pk>/', views.series_detail, name='series_detail'),
path('series/<int:pk1>/episode/<int:pk2>/delete/', views.delete_episode, name='delete_episode'),
series_detail.html
<button type="submit" name="addepisodeform">
Add Episode
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ e_form }}
</form>
</button>
{% for episode in episodes %}
{{ episode.name }}
{{ episode.description}}
<img src="{{ episode.image.url }}" height="125px" width="300px" style="object-fit: cover;">
<button type="submit" name="editepisodeform">
Edit Episode
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ edit_e_form }}
</form>
</button>
{% endfor %}
Okay, so it turns out that formsets were the way to go after all. Thanks to Willem Van Onsem's answer, I decided to go that route after all and it worked like a charm.
A form can only edit one instance, but with formsets, I was able to not only edit each episode rather than just the first instance, but even create a new object and delete multiple objects at the same time!
views.py
#import the formset
from django.forms import modelformset_factory, inlineformset_factory
#formset
EpisodeFormset = inlineformset_factory(Series, Episode, fields=('name', 'episode_no', 'description', 'image'), can_delete=True, extra=1)
#post call
if request.method == 'POST':
if 'editepisodeform' in request.POST:
formset = EpisodeFormset(request.POST, request.FILES, instance=series, prefix='editepisode')
if formset.is_valid():
formset.save()
return redirect('series_detail', pk=pk)
else:
return redirect('series_detail', pk=pk)
else:
formset = EpisodeFormset(instance=series)
series_detail.html
<div>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
<button type="submit" class="btn btn-primary" name="editepisodeform">Edit</button>
{{ form.as_p }}
{% for episode in episodes %}
{% if episode.episode_no == form.episode_no.value %}
Episode: {{ episode.episode_no }} <br>
Name: {{ episode.name }} <br>
<img src="{{ episode.image.url }}" height="125px" width="300px" style="object-fit: cover;"> <br> {% endif %}
{% endif %}
{% endfor %}
{% endfor %}
</form>
</div>

Django templates: using include to pass a form template; how to write the form?

I simply would like to create a form, throw it in a template, and use the include keyword to include it in my home.html file.
What am I doing wrong ?
# forms.py
class OrderForm(forms.Form):
from_email = forms.EmailField(required=True)
subject = forms.CharField(required=True)
message = forms.CharField(widget=forms.Textarea, required=True)
# views.py
def orderView(request):
template = 'orders.html'
if request.method == 'GET':
form = OrderForm()
else:
form = OrderForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
from_email = form.cleaned_data['from_email']
message = form.cleaned_data['message']
try:
send_mail(subject, message, from_email, ['admin#example.com']
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('success')
return render(request, 'orders.html', {'form': form})
<!-- templates/orders.html -->
<form method="post">
{% csrf_token %}
{% form.as_p %}
<button type="submit" class="btn btn-primary">Send</button>
</form>
<!-- templates/home.html -->
...
<div class="orders">
{% include 'orders.html' %}
</div>
...
Use {{ form.as_p }} instead of {% form.as_p %}. And render home.html from view to make the include work. Don't forget to pass the form like below:
{% include 'orders.html' with form=form %}

Django "The submitted file is empty"

I have used multiple FileFields in a model and my app gives error "The submitted file is empty" on execution, folowing are the details of my code:
My models.py:
class KBCTest(models.Model):
algorithm = models.CharField(max_length=10, blank=False)
entityFile = models.FileField(blank=False,
upload_to=updateFilename,
validators=[validateTestingFileExtension])
relationFile = models.FileField(blank=False,
upload_to=updateFilename,
validators=[validateTestingFileExtension])
My forms.py
class KBCTestForm(forms.ModelForm):
class Meta:
model = KBCTest
fields = ('algorithm', 'entityFile', 'relationFile')
def clean(self):
super(KBCTestForm, self).clean()
data = self.cleaned_data
return data
My views.py:
def testing(request):
title = 'Testing'
template = 'testing.html'
form = ''
if request.method == 'GET':
form = KBCTestForm()
if request.method == 'POST':
form = KBCTestForm(request.POST, request.FILES)
if form.is_valid():
form.save()
runAlgo = Run(form.cleaned_data)
runAlgo.configAndTest()
return HttpResponseRedirect(reverse('learning', kwargs={'function': 'testing'}))
context = {'title': title, 'form': form}
return render(request, template, context)
My template:
{% if request.user.is_authenticated %}
<div class='col-sm-4 col-sm-offset-4'>
<h1>{{ title }}</h1>
<!-- If form not rendered(i.e. views context) don't show submit button -->
{% if form %}
<!-- Use 'csrf_token' to prevent cross-site forgery -->
<form enctype='multipart/form-data' method='POST', action=''>
{% csrf_token %}
{{ form|crispy }} </br>
{{ form.non_field_errors|crispy }}
<input type='submit' value='Test' class='btn btn-default'/>
</form>
{% endif %}
</div>
{% endif %}
When I run this template it fails to validate the form and gives error "The submitted file is empty" error as visible in the screenshot below:
Check out this part of the Django docs, where it mentions the parameter allow_empty_file for the forms.FileField. Maybe it gives you a few clues.
Note: I'm not exactly sure how this applies to ModelForm or Model.FileField.

django how to authenticate user until verify email

How to authenticate user until he verify the email?
In my sign up process, I create the User, which means he can login even without verify email. Is there a build-in method to set a un-verified status on a User?
models.py
email_active = models.BooleanField(default=False,
verbose_name=u'Email active ')
views.py
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(username=username, password=password)
if user is not None:
if user.email_active:
login(self.request, user)
return HttpResponse("login success")
else:
return HttpResponse("user email is not active")
else:
return HttpResponse("username or password error")
you can active email by send email with verify_code,user can use verify_code to active their email.
Demo:
models.py:
class EmailVerify(models.Model):
owner = models.ForeignKey(User,
on_delete=models.CASCADE,
verbose_name='owner')
verify_code = models.CharField(max_length=254,
null=True,
verbose_name='verify_code ')
def update(self):
self.verify_code = self.generate_key()
self.save()
return self.verify_code
def get_active_email_url(self, request):
from django.urls import reverse
url = '{}?active_code={}'.format(request.build_absolute_uri(reverse('user_login', args=())), self.verify_code)
return url
#staticmethod
def generate_key():
return binascii.hexlify(os.urandom(20)).decode()
class Meta:
verbose_name = 'EmailVerify'
utils.py :
def send_active_email(request, user):
current_site = get_current_site(request)
site_name = current_site.name
if EmailVerify.objects.filter(owner=user, category=0).exists():
verify = EmailVerify.objects.filter(owner=user).first()
else:
verify = EmailVerify.objects.create(owner=user)
verify.update()
title = u"{} active email".format(site_name)
message = "".join([
u"click this link can active your email:\n\n",
"{}\n\n".format(verify.get_active_email_url(request=request)),
])
try:
send_mail(title, message, settings.DEFAULT_FROM_EMAIL, [user.email])
message = "success"
except ConnectionRefusedError as e:
message = e.strerror
except Exception as e:
message = str(e)
return message
views.py
class LoginView(BaseContextMixin, FormView):
template_name = 'user/login.html'
form_class = LoginForm
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
if 'form' not in kwargs:
kwargs['form'] = self.get_form()
if 'active_email' in self.request.GET:
active_email = self.request.GET.get('active_email')
try:
user = User.objects.get(email=active_email, email_active=False)
kwargs['message'] = send_active_email(self.request, user)
except (ObjectDoesNotExist, MultipleObjectsReturned):
kwargs['message'] = 'email not exist or actived'
if 'active_code' in self.request.GET:
active_code = self.request.GET.get('active_code')
try:
email_verify = EmailVerify.objects.get(verify_code=active_code)
email_verify.owner.email_active = True
email_verify.owner.save()
email_verify.delete()
kwargs['message'] = 'email {} actived'.format(email_verify.owner.email)
except ObjectDoesNotExist:
kwargs['message'] = 'unless link'
except MultipleObjectsReturned:
EmailVerify.objects.filter(verify_code=active_code).delete()
kwargs['message'] = 'error!'
return super(LoginView, self).get_context_data(**kwargs)
def get_form(self, form_class=None):
if form_class is None:
form_class = self.get_form_class()
return form_class(request=self.request, **self.get_form_kwargs())
forms.py:
class LoginForm(forms.Form):
active_email = None
username = forms.CharField(label='username')
password = forms.CharField(widget=forms.PasswordInput, label='password')
def __init__(self, request, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
self.request = request
self.fields['username'].widget.attrs.update({'class': 'form-control',
"placeholder": self.fields['username'].label})
self.fields['password'].widget.attrs.update({'class': 'form-control',
"placeholder": self.fields['password'].label})
def clean(self):
username = self.cleaned_data["username"]
password = self.cleaned_data["password"]
user = authenticate(username=username, password=password)
if user is not None:
if user.email_active:
login(self.request, user)
else:
self.active_email = user.email
self.add_error("username", "not active")
else:
self.add_error("username", "username or password error")
login.html:
{% extends "user/user_base.html" %}
{% block content %}
<div class="login-box">
<div class="login-logo">
{{ website_title|default_if_none:'' }}
</div>
<div class="login-box-body">
<form method="post" action="{% url 'user_login' %}" class="form">
{% csrf_token %}
{% for field in form %}
<div class="form-group no-margin {% if field.errors %} has-error {% endif %}">
<label class="control-label" for="{{ field.id_for_label }}">
<b>{{ field.label }}</b>
{% if message %}
{% ifequal field.name 'username' %}{{ message }}{% endifequal %}
{% endif %}
{% if field.errors %}
{{ field.errors.as_text }}
{% ifequal field.errors.as_text '* not active' %}
<a href="{% url 'user_login' %}?active_email={{ form.active_email }}">
send active emial
</a>
{% endifequal %}
{% endif %}
</label>
{{ field }}
</div>
{% endfor %}
<div class="row">
<div class="col-md-6">
<button id="btn_login" type="submit" style="width: 100%"
class="btn btn-raised btn-primary">Login
</button>
</div>
</div>
</form>
</div>
</div>
{% endblock %}