I'm having trouble getting an image field to upload correctly with django crispy forms. Everything else on the form and page works fine, but the image field does not error or save, it just passes right over it. I am able to add the image through admin just fine, it only fails on my crispy form.
#views.py
#login_required
def dashboard(request, **kwargs):
'''
perform managerial and administrative tasks for a blog. Only
available to the blog owner
'''
alert_message = ""
status = ""
blog_slug = kwargs['blog']
blog = get_object_or_404(PersonalBlog, slug=blog_slug)
# handle the form submit to update blog data
form = BlogEditForm(instance=blog)
if request.method == "POST":
if blog.owner == request.user:
form = BlogEditForm(request.POST, instance=blog)
if form.is_valid():
form.save()
alert_message = "Your blog data has been updated successfully"
status = "saved"
else:
alert_message = "There was a problem saving the data you entered. Please correct the errors above."
status = "error"
else:
alert_message = "You do not have access to update this blog's information."
status = "error"
return render(request, "blogcontent/dashboard.html", {'alert_message':alert_message,
'status':status, 'form':form})
#forms.py
class BlogEditForm(ModelForm):
description = forms.CharField(widget = forms.Textarea())
twitter = forms.CharField(required=False, help_text="show twitter feed, and allow people to interact with you on twitter")
twitter_widget_id = forms.CharField(required=False, help_text="required to show a timeline widget for your twitter account. " + \
"<span class='glyphicon glyphicon-question-sign'></span>")
instagram = forms.CharField(required=False, help_text="show instagram feed on your blog page")
disqus = forms.CharField(required=False, help_text="allow comments at the bottom of your blog posts")
class Meta:
model = PersonalBlog
def __init__(self, *args, **kwargs):
super(BlogEditForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
'<h2 class="text-center">Edit data about your blog</h2>',
Field('owner', type="hidden"),
Field('title', type="hidden"),
Div('description', css_class="col-md-12"),
Div('twitter', css_class="col-md-6"),
Div('twitter_widget_id', css_class="col-md-6"),
Div('instagram', css_class="col-md-6"),
Div('disqus', css_class="col-md-6"),
Div('logo', css_class="col-md-12"),
),
ButtonHolder(
Submit('submit', 'Update Info', css_class='btn-lg'),
css_class="text-center"
),
)
#dashboard.html
<form method="post" enctype="multipart/form-data">
{% crispy form %}
</form>
Ah, I knew it was going to be something dumb. I was not attaching the request.FILES to the form.save() object.
form = BlogEditForm(request.POST, request.FILES, instance=blog)
Have you tried changing:
<form method="post" enctype="multipart/form-data">
{% crispy form %}
</form>
To just:
{% load crispy_forms_tags %}
{% crispy form %}
Also in your code, your are calling:
if form.is_valid():
form.save()
You don't want to save() the form. You want to save the form data to a model instance. So create() a model using the form data. This may be why as well. Forms are just used to hold data until you clean it and save it to the database using a model.
Related
I'm doing a website in django but this is the first time i use this framework so i'm not so used to it. I need to save some information on a DB, and i need to take these information from some radio buttons. I tryied so many ways to get the data but nothing worked. So i'd like to ask how to get these data in models.py from a template.html.
This is the code in views.py:
def question1(request):
form = CHOICES(request.POST)
if request.method == 'POST':
form = CHOICES(request.POST)
if form.is_valid():
selected = form.cleaned_data.get("NUMS")
return render(request, 'q1.html', {'form': form})
This is the template question1.html:
<form class="form-inline" method='POST' action="" enctype='multipart/form-data'>{% csrf_token %}
{% csrf_token %}
{{form.path}}
</form>
Then there is the form in forms.py:
NUMS = [
('one', 'one'),
('two', 'two'),
('three', 'three'),
('four', 'four'),
('five', 'fives'),
]
class CHOICES(forms.Form):
NUMS = forms.ChoiceField(choices=NUMS, widget=forms.RadioSelect)
I checked and I think that the problem could be the request.method that is GET insted of POST.
So how can I make a POST request?
Thank you
Good day everyone.
I am trying to build a form which queries the database based on user data inputs and then returns the results in a new page. but I don't know exactly how to do it and I am getting errors. I've looked for a solution but couldn't find any. Please help me if you know any solutions.
Thanks in advance.
Here are my codes:
forms.py
class AttendanceForm(forms.Form):
course = forms.CharField(max_length=50)
department = forms.CharField(max_length=10)
semester = forms.IntegerField()
views.py
class AttendanceForm(generic.FormView):
form_class = CrsAttForm
template_name = 'office/crsatt_form.html'
success_url = reverse_lazy('office:members_list')
class MembersList(generic.ListView):
template_name = "office/crs_att.html"
context_object_name = 'members'
def get_queryset(self):
return Members.objects.all()
# I know I should use .filter method but how could I set the parameters to data received from the form
urls.py
url(r'^CourseAttendanceForm/$', views.AttendanceForm.as_view(), name='courseattendance'),
url(r'^CourseAttendanceForm/Results/$',views.MembersList.as_view(), name='memebrs_list'),
I think that it will be easier for you to use function based views for this one.
You can do it like this:
views.py
def form_page(request):
form = AttendanceForm()
# you get to this "if" if the form has been filled by the user
if request.method == "POST":
form = AttendanceForm(request.POST)
if form.is_valid():
course = request.POST['course']
department = request.POST['department']
semester = request.POST['semester']
members = Member.objects.filter(#here you do your filters as you already have the course, department and semester variables)
context = {'members': members}
return render(request, 'second_page.html', context)
# if the form hasn't been filled by the user you display the form
context = {'form': form}
return render(request, 'form_page.html', context)
form_page.html
<form method="post" action="{% url 'form_page' %}">
{% csrf_token %}
{{ form }}
<button type="submit">Search!</button>
</form>
urls.py
path('form_page/', views.form_page, name='form_page')
second_page.html
{% for member in members %}
# here you display whatever you want to
{% endfor %}
I have a form that when i submit it need to show the details I submitted in the form.
I am really struggling to understand how to get it to display checkbox data.
I went thropugh the django documentation on DetailForms but this didnt really help me with how to display ManyToManyFields.
My template is as follows:
<li>{{theBurger.burger}}</li>
<li>{{theBurger.bun}}</li>
{% for toppings in theBurger.toppings.all %}
<li>{{toppings}}</li>
{% empty %}
<p>No toppings!</p>
{% endfor %}
<li>{{theBurger.sauces}}</li>
{% for extras in theBurger.extras.all %}
<li>{{theBurger.extras}}</li>
{% empty%}
<p>No extras!</p>
{% endfor %}
My view is as followes:
class OrderDetailView(DetailView):
context_object_name = 'theBurger'
slug_field = 'id'
model = models.Burger
def get_context_data(self, **kwargs):
context = super(OrderDetailView, self).get_context_data(**kwargs)
context['now'] = timezone.now()
return context
I can get the page to display all the other information except information that has been submitted via checkboxes. the response that is being sent is:
<QueryDict: {'csrfmiddlewaretoken':
['l6Qq7tg89cueHV2Fl6Qq7tg89cueHV2F2WrzrbJ'],
'burger': ["Aurion's Famous Beef Burger"], 'bun': ['White Bread'],
'toppings': ['15', '1
6'], 'sauces': ['Our Zesty Barbaque Sauce'], 'Submit': ['Git my food!']}>
Lastly here is the form:
class BurgerForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BurgerForm, self).__init__(*args, **kwargs)
self.fields['toppings'].widget = forms.CheckboxSelectMultiple()
for field_name in self.fields:
field = self.fields.get(field_name)
if field and isinstance(field , forms.TypedChoiceField):
field.choices = field.choices[1:]
self.fields['extras'].widget = forms.CheckboxSelectMultiple()
class Meta:
model = Burger
fields = ['burger', 'bun', 'toppings', 'sauces', 'extras']
Can someone point out what ive done wrong?
Darn after ploughing though a gazillion google linked I came across this:
http://www.joshuakehn.com/2013/6/23/django-m2m-modelform.html
I tried to remove the:
Commit=False
from:
post = form.save()
in the forms.py file and it works now. I wasted a lot of time on this so I hope it helps someone else.
I have a Django 1.8 form that contains a paragraph tag that renders either some feedback or a question submitted by a user. It also contains a textarea input 'response_text' and a pair of radio buttons 'close_issue'. This response input can be used to send an optional response to the user. If the user submitted some feedback, the admin should be able to click the 'close issue' radio button and submit the form with no response. However, if the textarea input contains a question, then the form should render an error telling the admin that he/she can't submit the form without typing an answer into the response input. The problem I'm having is that I can't get the form to cause the template to render an error message if the user submitted a question but the admin didn't type in a response. My view, model, form, and template are shown below. forms.py shows all the ways (all commented out) I have tried to make the response input field required if the user submitted a question so that the template will display an error. I also tried overriding the default 'clean' method with one that would raise a ValidationError if the user submitted a question and the response input is blank but that didn't work either. Can anyone tell me what I'm doing wrong?
Thanks.
# view.py
def review_feedback_or_question(request, template, *args, **kwargs):
fqid = kwargs['fqid']## Heading ##
submission = FeedbackQuestion.objects.get(pk=fqid)
if request.method == 'POST':
form = FeedbackQuestionResponseForm(request.POST, submission=submission)
if form.is_valid():
# process the form
return redirect('review-feedback-or-question-queue')
else:
pass
form = FeedbackQuestionResponseForm(submission=submission)
context = {'form': form, 'submission': submission,}
return render(request, template, context)
# models.py
class FeedbackQuestion(models.Model):
SELECT = ''
FEEDBACK = 'feedback'
QUESTION = 'question'
SUBMISSION_TYPE_CHOICES = (
(SELECT , '-- Select --'),
(FEEDBACK, 'Feedback'),
(QUESTION, 'Question'),
)
user = models.ForeignKey(User, related_name="user")
submission_type = models.CharField(max_length=8,
choices=SUBMISSION_TYPE_CHOICES,
default=SELECT)
submission_text = models.TextField()
date_submitted = models.DateTimeField(auto_now_add=True)
response_text = models.TextField()
respondent = models.ForeignKey(User, related_name='respondent')
date_responded = models.DateTimeField(auto_now=True)
issue_closed = models.BooleanField(default=False)
class Meta:
db_table = 'feedback_question'
# forms.py
class FeedbackQuestionResponseForm(forms.Form):
TRUE = 1
FALSE = 0
BLANK = ''
CHOICES = ( (TRUE, 'Yes'), (FALSE, 'No') )
response_text = forms.CharField(
required=False,
label='',
widget=forms.Textarea(attrs={'placeholder': 'Enter response...'}))
close_issue = forms.TypedChoiceField(
choices=CHOICES,
label='Close this issue?',
widget=forms.RadioSelect(renderer=HorizontalRadioRenderer),
coerce=int)
def __init__(self, *args, **kwargs):
if 'submission' in kwargs:
submission = kwargs.pop('submission')
if submission.submission_type == 'question':
# NONE OF THESE WORKED!
#self.fields.get('response_text').required = True
#self.declared_fields['response_text'].required = self.TRUE
#self.declared_fields['response_text'].required = self.TRUE
#self.declared_fields['response_text'].required = True
#self._errors['response_text'] = "You must enter a response"
pass
super(FeedbackQuestionResponseForm, self).__init__(*args, **kwargs)
# template.html
<p>{{ submission.submission_text }}</p>
<form action="" method="post">{% csrf_token %}
{{ form.non_field_errors }}
{% if form.errors %}
{% if form.errors.items|length == 1 %}
Please correct the error below.
{% else %}
Please correct the errors below.
{% endif %}
</p>
{% endif %}
{{ form.response_text.errors }}
{{ form.response_text.label_tag }} {{ form.response_text }}
{{ form.close_issue.errors }}
{{ form.close_issue }} {{ form.close_issue.label_tag }}
<input type="submit" value="Submit" class="" />
</form>
You're not passing submission into the form when you instantiate it on POST, so the required attribute is never being set.
Daniel Roseman was correct in that I need to pass 'submission' into the form when I instantiate the form on POST. But there were still two other problems. First, I need to instantiate the form inside the else block. If this isn't done and the form doesn't validate, then you're passing an unbound form back to the viewer and any errors won't be displayed. Also, it isn't necessary to pass 'submission' to the form when you instantiate it here:
...
else:
form = FeedbackQuestionResponseForm()
context = {...}
...
The next problem was that the order of my statements inside the init method was incorrect. It appears that I needed to execute 'super()' before trying to reference the 'response_text' field. I'll need to locate and study this method in the Django source code to understand exactly why. In any case, this works:
def __init__(self, *args, **kwargs):
if 'submission' in kwargs:
submission = kwargs.pop('submission')
else:
submission = False
super(FeedbackQuestionResponseForm, self).__init__(*args, **kwargs)
if submission:
if submission.submission_type == 'question':
self.fields['response_text'].required = True
else:
self.fields['response_text'].required = False
When the above changes are implemented, the form will make the response_text field required if the user submits a question and an error will be displayed if the admin doesn't enter a response before submitting the form. Many thanks again to Daniel for getting me back on track towards finding a solution.
Merry Christmas!!!!I work with flask sqlalchemy and wtf. I could create new and would display in info.html But when I try to edit the form there is no changes in database So it is not working. So I wonder where is issues?
app.py
#With this route I can add new form in database
#app.route('/contact', methods=['GET', 'POST'])
def contact():
form = LoginForm(request.form)
if request.method == 'POST':
if form.validate()== True:
contact = Contacts()
# populate the model
form.populate_obj(contact)
db.session.add(contact)
db.session.commit()
# Contact commited to database
# RETURN TO INFO.HTML
return redirect(url_for('info'))
else:
#If the form does not have all fields that are required
return render_template('contact.html', form=form)
# This is the part for edit which is not working
# so I query and populate it but no change none in
# database none in info.html
#app.route('/edit/<int:id>', methods=['POST'])
def edit(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
# check the validate and then populate the obj
if form.validate_on_submit()== True:
#populate it
form.populate_obj(user)
db.session.commit()
return redirect(url_for('info'))
else:
#If the form does not have all fields that are required
return render_template('edit.html', id=id )
#app.route('/edit/<int:id>', methods=['GET'])
def profile(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form, obj=user)
return render_template('edit.html',form=form, id =id)
# this route to html that should show all info
#app.route('/info', methods=['GET', 'POST'])
def info():
#query all
info = Contacts.query.all()
return render_template('info.html', contact=info)
model.py
# model with table name Contacts
class Contacts(db.Model):
__tablename__ = "Contacts"
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
age = db.Column(db.Integer)
form.py
# this is the form wtf
Class LoginForm(Form):
name = StringField("Name", [validators.Required("Please enter your name.")])
email = StringField("Email", [validators.Required("Please enter your email address."), validators.Email("Please enter valid email address.")])
age = IntegerField("age", [validators.Required("Please enter your age.")])
submit = SubmitField("Submit")
info.html
# it should display all updated form But it wont??
{% extends "layout.html" %}
{% block content %}
<h2>show the info</h2>
{% for contact in contact %} # Maybe this is also Problem?
<strong>name:</strong> {{ contact.name}} <br>
<strong>email:</strong> {{ contact.email }} <br>
<strong>age:</strong> {{ contact.age}} <br>
<br>
{% endfor %}
{% endblock %}
You don't have to readd a Object, when you get it per query. The Queryobject is bound to the session. So you only need to commit the changes and not to add them again.
so this should be the corrected code snippet
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
# check the validate and then populate the obj
if form.validate()== True:
# populate it
form.populate_obj(user)
db.session.commit()
I tried your code on my machine. With little modification in code I was able to update database.
Please find updated chunk below:
#app.route('/edit/<int:id>', methods=['POST'])
def submit(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
#return render_template('edit.html', form=form, id=1 )
if form.validate() == True:
form.populate_obj(user)
db.session.commit()
return redirect(url_for('info'))
else:
return redirect(url_for(edit, id=id))
#app.route('/edit/<int:id>', methods=['GET'])
def edit(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
return render_template('edit.html', form=form, id=id )
I know this is an older question, but I'm referencing from Why doesn't my WTForms-JSON form update correctly?.
Essentially, you need to initialize the Form first then check to see if it was submitted or not:
form = FormBuilder()
if not form.is_submitted():
form = FormBuilder.populate_obj(obj)
else:
form = FormBuilder() # will populate from submitted data
if form.validate_on_submit():
It helped me when having the same issues.