Django rendering Form object rather than form fields - python

Sure I've missed something obvious, but any help appreciated.
I have a form model:
class UserForm(forms.Form):
name = forms.CharField()
A view:
def userform(req):
context = {}
context['user_form'] = UserForm()
context['message'] = 'test message'
return render(req, 'apps/userform.html', context)
And a template:
{% extends 'base.html' %}
{% block title %} | User Form {% endblock %}
{% block content %}
<h1>Form page</h1>
<form method='POST'>
{% csrf_token %}
{{ user_form }}
<button type='submit'>Send</button>
</form>
{{ message }}
{% endblock %}
I'm pretty sure everything is connected correctly and imported as required - the 'message' property on context renders fine under the form.
However, {{ user_form }} in the template renders the actual Form object instance, rather than the actual form field I'm expecting. I see:
<userform.views.UserForm object at 0x7fcab17e5c10>
Then the form submit button.
What have I missed?
Django 4, if that matters.

So the issue was I had a class based view in the views file with the same name as my Form class - I guess that was getting instantiated, rather than the Form class. Commented out the CBV and everything worked.
If I'd looked harder at the error message, I would have probably seen this sooner as the instantiated object is clearly in the views folder, rather than the forms one...

Related

Multiple form on a page validation errors WTForms

I have multiple form on a page in modals: 1-st to create a new user address and n-forms with addresses which user created (created with loop). When i enter invalid data in fields with validators (e.g. datarequired), i have error messages in each form.
Here is the field render example which i use in every form:
{{ address_form.street.label(class_="form-label", for="InputStreet") }}
{{ address_form.street(class_="form-control", id="InputStreet") }}
{% for error in address_form.street.errors %}
<span style="color: red;">{{ error }}</span>
{% endfor %}
Part of the code from view.py:
#bp.route('/profile/address', methods=['GET', 'POST'])
#login_required
def address():
address_form = AddressForm()
if address_form.submit_address.data and address_form.validate():
address_to_add = Address(
street=address_form.street.data,
house=address_form.house.data,
building=address_form.building.data,
entrance=address_form.entrance.data,
floor=address_form.floor.data,
apartment=address_form.apartment.data,
additional_info=address_form.additional_info.data,
user=current_user)
db.session.add(address_to_add)
db.session.commit()
return redirect(url_for('profile.address'))
if address_form.edit_address.data and address_form.validate():
address_to_edit = Address.query.get(address_form.address_id.data) # Here is data from hidden field
# Editing data in DB
db.session.commit()
return redirect(url_for('profile.address'))
return render_template('profile/address.html', title='Адрес доставки', address_form=address_form)
Forms work fine with adding, editing and deleting data, but work incorrect with validation errors.
I think i need one more condition in if statenent related with hidden field or change something in my html file.
I've tried add an action attr in form like:
<form action="{{ url_for('profile.address', form_id=address.id) }}" method="post" novalidate>
And smth like this in view func but it doesn't work:
form_id = request.args.get('form_id', type=int)
if address_form.edit_address.data and address_form.validate() and form_id == address_form.address_id.data:
pass
Finally i found a very bad solution:
address_form = AddressForm()
form_id = request.args.get('form_id', 0, type=int)
For main form form_id always is 0.
Form tag for main form:
<form action="{{ url_for('profile.address', form_id=form_id) }}" method="post" novalidate>
And a field render for main form:
{{ address_form.street.label(class_="form-label", for="InputStreet") }}
{{ address_form.street(class_="form-control", id="InputStreet") }}
{% if form_id == 0 %}
{% for error in address_form.street.errors %}
<span style="color: red;">{{ error }}</span>
{% endfor %}
{% endif %}
If someone has a better solution about my problem it'd good. Now it's time to learn some JS and solve this problem with AJAX.

how to extend class bassed view

I've crated class-based-view which shows content of the post.I also added the ability to make comments there, however the comment form dosn't work.
views.py
class PostDetailView(DetailView):
model=Post
template_name='blogdetail.html'
pk_url_kwarg='id'
def comments(request):
comments=post.comments.all()
new_comment=None
if request.method=="POST":
comment_form=CommentForm(data=request.POST)
if comment_form.is_valid():
new_comment=comment_form.save(committ=False)
new_comment.post=post
new_comment.save()
else:
comment_form=CommentForm()
return render(request, 'allblogs.html', {'comments':comments, 'comment_form':comment_form,
'new_comment':new_comment})
forms.py
class CommentForm(forms.ModelForm):
class Meta:
model=Comment
fields=['post','body']
blogdetail.html
{% extends 'base.html' %}
{% block content %}
<h1>{{post.title}}</h1>
<p>{{post.created_on}}</p>
<p>:{{post.author}}</p>
<p>{{post.text}}</p>
<form>
{{comment_form.as_p}}
<p><input type="submit" value="add"></p>
{% csrf_token %}
</form>
{% endblock content %}
class base views are not working this way.
If you look at your class, you didn't call your comment function.
I suggest you look at this page.
Besides that, for creating a URL for your author, I suggest you use get_absolute_url in your model and use it in your template like this:
{{ author.get_absolute_url }}
Hope that this answer is helped you.

"This field is required" when all fields are filled in Django

When filling out a form I get "This field is required." even though all fields are filled in.
It doesn't have to do with setting required to False or anything like that, because all fields are required.
views.py
def upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
title = form.cleaned_data['title']
username = request.user.get_username()
category = form.cleaned_data['category']
handle_uploaded_file(request.FILES['file'],title,username,category)
return HttpResponseRedirect('')
else:
form = UploadFileForm()
return render(request, 'main/upload.html', {'form': form})
function
def handle_uploaded_file(f,title,username,category):
with open('/uploads/' + category + '/' + title, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
forms.py
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
category = forms.CharField(max_length=50)
file = forms.FileField()
upload.html
{% extends 'base.html' %}
{% block title %}Upload{% endblock %}
{% block content %}
{% if user.is_authenticated %}
Uploading as: {{ user.username }}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit"/>
</form>
{% else %}
<p>You are not logged in</p>
login
{% endif %}
{% endblock %}
The error I get when filling out a form is: "This field is required"
Screenshot:
When I select a file and it throws the error it unselects whatever file I've selected, similar to how the password field is cleared when hitting sign up without completing every field.
The file isn't being submitted with the request because you didn't sent the correct enctype on the form element. Here are Django's docs concerning that.
<form method="post" enctype="multipart/form-data">
One way to verify this/debug it would be to print the form's data form.data, request.POST and/or request.FILES before the call to is_valid. Or verifying the request in a browser's dev tools.

'ManagementForm data is missing or has been tampered with' Issue

I am stuck with this error and I don't know what to do...
I followed the Django documentation here
I would like also to create a new Player object when I fill the empty extra form, but one requires field of the model must be based on request.user.team not seated directly by the user
Hope someone is familiar with ManagementForm
My files:
views.py
from django.shortcuts import render, redirect
from skeleton.models import Player
from django.contrib.auth.decorators import login_required
from .forms import PlayerForm
from django.forms import modelformset_factory
# Create your views here.
#login_required(login_url="/accounts/login/")
def team_area(request):
PlayerFormSet = modelformset_factory(Player, fields=('first_name', 'last_name'), extra=1)
if request.method == "POST":
player_formset = PlayerFormSet(
request.POST,
request.FILES,
queryset=Player.objects.all().filter(team=request.user.team),)
if player_formset.is_valid():
player_formset.save()
return redirect('team_area:home')
else:
player_formset = PlayerFormSet(queryset=Player.objects.all().filter(team=request.user.team))
return render(request, 'team_area/team_area.html', {'player_formset': player_formset})
team_area.html
{% extends 'base_layout.html' %}
{% block content %}
<h1>Area Squadra</h1>
<form method="post" action="">
{% csrf_token %}
{{ formset.management_form }}
{% for player_form in player_formset %}
{% for field in player_form %}
{{ field.label_tag }} {{ field }}
{% endfor %}
<br>
{% endfor %}
<input type="submit" value="Aggiorna">
</form>
{% endblock %}
A formset may contain more then a single form, so for saving the data from a formset in the view file, you have to loop through all the forms and save each form's data. Like in your views.py file you can write:
if player_formset.is_valid():
for form in player_formset:
form.save()
This will create as many new objects of Player as there are new forms in the formset (assuming you have not deleted any form or edited the pre-existing form).
One must also update the Management Form data in the .html file as soon as the user clicks on Add Player button
This is the solution I found and used using
.save(commit=False)
if request.method == "POST":
player_formset = PlayerFormSet(request.POST, request.FILES, queryset=Player.objects.all().filter(team=request.user.team),)
for player_form in player_formset:
if player_form.is_valid():
player = player_form.save(commit=False)
player.team = request.user.team
if player_formset.is_valid():
player_formset.save()

Trying to save my Django Model Formset, keep getting ManagementForm error?

So, a total Django Model Formset Newb question. I'm trying to save my form and keep getting this error:
['ManagementForm data is missing or has been tampered with']
Here is what I have for my TemplateView:
class AttendanceTemplate(TemplateView):
template_name = 'attendance/index.html'
def get_context_data(self, **kwargs):
context = super(AttendanceTemplate, self).get_context_data(**kwargs)
instruction = Instruction(self.request.user.username)
sections_list = self.request.GET.getlist('sections_list')
term = self.request.GET.get('term', instruction.term)
enrollments = Enrollment.objects.using('wisp').prefetch_related('profile').filter(section_id__in=['111111'], term=term)
attendanceQuery = Enrollment.objects.using('wisp').prefetch_related('student').filter(section_id__in=['111111'], term=term)
for enrollment in attendanceQuery:
attendance, created = Attendance.objects.update_or_create(
section_id=enrollment.section_id,
term=enrollment.term,
first_name=enrollment.student.first_name,
last_name=enrollment.student.last_name,
email_address=enrollment.student.email_address,
)
something = Attendance.objects.filter(section_id__in=['111111'], term=term)
formset = AttendanceFormSet(queryset=something)
combined = zip(enrollments, formset)
context['combined'] = combined
return context
Here is how I'm trying to save the form:
def post(self, request):
formset = AttendanceFormSet(request.POST)
if formset.is_valid():
for thing in formset:
formset = thing.save()
return render_to_response("template/index.html",{'formset': formset}, RequestContext(request))
else:
return HttpResponse(error.msg)
Here is what I have in my template:
<form method="POST" action="">
{% csrf_token %}
{% for enrollment, form in combined %}
<div class="wrapper-formset">
<div>
{{ form.first_name.value }}
{{ form.last_name.value }}
{{ form.email_address.value }}
</div>
<div class="clear-all"></div>
</div>
{% endfor %}
<button type="submit" class="save btn btn-default">Save</button>
</form>
Am I saving my form wrong? Maybe my loop is wrong? Also, I'd prefer to print each field out individually, so using the "myform.management_Form" may not work for me? (e.g., myform.management_form.field_name)
If you render the forms separately, then you must include the management form in your template. The fact that you are zipping your forms makes no difference.
Including the management form is easy, just add {% formset.management_form %} to your template.
<form method="POST" action="">
{% csrf_token %}
{{ formset.management_form }}
{% for enrollment, form in combined %}
...
For that to work, you'll need to make sure that formset is in the template context, for example:
context['formset'] = formset
You might find the docs on using model formsets in the template useful. It would be a good idea to start with the simplest option, {{ formset }}, test it, then gradually customize the template. That makes it easier to debug when stuff goes wrong. At the moment it looks like you have missed out {{ form.id }}.

Categories

Resources