Missing Variable Error in Django Form After Form Submission - django

I added a newsletter sign-up form to the footer area of my site and such had to use an inclusion_tag because I couldn't bind it to a view. It works well and as expected, but I have a strange thing happening that I apparently am not smart enough to figure out myself :)
After the form is submitted, I receive the email confirmation, but two things happen:
The Django Success Message doesn't appear until after I manually refresh the page.
Where my form sits, there are syntax 'Missing Variable' errors. I included a screenshot for reference and my form code is below. The form fields re-appear and errors go away after refreshing the page again.
home_tags.py
#register.inclusion_tag('pages/tags/footer_newsletter_signup.html', takes_context=True)
def footer_newsletter_signup(context):
request = context['request']
title = 'Newsletter Signup'
form = MailingListForm(request.POST or None)
if form.is_valid():
mailing_list_full_name = form.cleaned_data.get('mailing_list_full_name')
mailing_list_phone = form.cleaned_data.get('mailing_list_phone')
mailing_list_email = form.cleaned_data.get('mailing_list_email')
mailing_list_subject = 'Submission from Newsletter Signup'
mailing_list_message = 'Yes, please add me to marketing emails.'
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = [from_email, 'charles#studiorooster.com']
ctx = {
'mailing_list_subject': mailing_list_subject,
'mailing_list_full_name': mailing_list_full_name,
'mailing_list_email': mailing_list_email,
'mailing_list_phone': mailing_list_phone,
'mailing_list_message': mailing_list_message
}
message = get_template('pages/newsletter_signup_email.html').render(Context(ctx))
msg = EmailMessage(mailing_list_subject, message, to=recipient_list, from_email=from_email)
msg.content_subtype = 'html'
msg.send()
messages.success(request, "Thank you, you've been added to our list.")
return HttpResponse('/')
context = {
'form': form,
'title': title,
}
return context
footer_newsletter_signup.html
<form action='' method='POST' role='form' class="form-inline">
{% csrf_token %}
<div class="form-group">
{{ form.mailing_list_full_name }}
</div>
<div class="form-group">
{{ form.mailing_list_phone }}
</div>
<div class="form-group">
{{ form.mailing_list_email }}
</div>
<button class="button button-lg button-square button-pasific hover-ripple-out" type='submit'>Subscribe</button>
</form>
Then I just add the tag to my template like:
{% footer_newsletter_signup %}

Answering this
Ok, so here is where I am confused. I have a dozen views and this form is a Call-to-Action form that sits at the top of the footer. How do I bind this form to every view without repeating the code everywhere? Thank you for your help.
You need to create separate view to handle this form and provide action param in form tag pointing to this view.
Here is general idea, code my not work
#template
<form action='{% url "send-mail" %}' method='POST' role='form' class="form-inline">
...
#views
def send_mail(request):
form = MailingListForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
mailing_list_full_name = form.cleaned_data.get('mailing_list_full_name')
mailing_list_phone = form.cleaned_data.get('mailing_list_phone')
mailing_list_email = form.cleaned_data.get('mailing_list_email')
mailing_list_subject = 'Submission from Newsletter Signup'
mailing_list_message = 'Yes, please add me to marketing emails.'
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = [from_email, 'charles#studiorooster.com']
ctx = {
'mailing_list_subject': mailing_list_subject,
'mailing_list_full_name': mailing_list_full_name,
'mailing_list_email': mailing_list_email,
'mailing_list_phone': mailing_list_phone,
'mailing_list_message': mailing_list_message
}
message = get_template('pages/newsletter_signup_email.html').render(Context(ctx))
msg = EmailMessage(mailing_list_subject, message, to=recipient_list, from_email=from_email)
msg.content_subtype = 'html'
msg.send()
messages.success(request, "Thank you, you've been added to our list.")
return HttpResponse('/')
#tags
#register.inclusion_tag('pages/tags/footer_newsletter_signup.html', takes_context=True)
def footer_newsletter_signup(context):
title = 'Newsletter Signup'
form = MailingListForm()
context = {
'form': form,
'title': title,
}
return context
#url
url('r^send-mail/$', send_mail, name='send-email')

Related

Flask WTForm: submit button produces no error

I've used an identical form to submit and it has worked in the past. The submit button isn't working now, so everything within the POST and validate loop is not being executed.
This is a single page webapp. This form should take 7 fields and write to the db. The app does properly print the records (recent_data) from the db, so the connection to the db is correct.
Expected behavior: This should produce a POST-GET redirect. The submit button should trigger a POST, and it should redirect to the homepage, with the form now empty.
Current Behavior: The submit button does not do anything.
Reproduce: I think this could be reproduced with the plain boilerplate flask app code added to my code here.
Debugging: There is no error displayed.
class ApplicationForm(FlaskForm):
emp_length_cat_input = IntegerField(
u'How long have you been with your employer? (1-11)',
validators=[DataRequired()])
home_status_input = IntegerField(
u'What is your housing status? (1-Rent, 2-Other, 3-Mortgage, 4-Own)',
validators=[DataRequired()])
zip3_input = IntegerField(
u'What is the first 3 digits of your zip code?',
validators=[DataRequired()])
total_acc_input = IntegerField(
u'How many accounts have you ever had in your name?',
validators=[DataRequired()])
annual_inc_input = IntegerField(
u'What is your annual income? (no commas)',
validators=[DataRequired()])
dti_input = IntegerField(
u'What is your debt-to-income ratio? (round to 2 decimals)',
validators=[DataRequired()])
descr_input = StringField(
u'Why do you need this loan? (enter text)',
validators=[DataRequired()])
submit = SubmitField('Submit')
#app.route('/', methods=['GET', 'POST'])
def index():
# Set form
form = ApplicationForm(request.form)
# Form submission
if request.method == 'POST' and form.validate_on_submit():
session['emp_length_cat_input'] = form.emp_length_cat_input.data
session['home_status_input'] = form.home_status_input.data
session['zip3_input'] = form.zip3_input.data
session['total_acc_input'] = form.total_acc_input.data
session['annual_inc_input'] = form.annual_inc_input.data
session['dti_input'] = form.dti_input.data
session['descr_input'] = form.descr_input.data
with db.connect() as conn:
conn.execute(
"""INSERT INTO
loans_tbl (
emp_length_cat,
home_status,
zip3,
total_acc
annual_inc,
dti,
descr)
VALUES (%s,%s,%s,%s,%s,%s,%s)""",
(emp_length_cat_input, home_status_input, zip3_input,
total_acc_input, annual_inc_input, dti_input, descr_input)
)
db.session.commit()
flash('Thanks for applying')
return redirect(url_for('index'))
return render_template('index.html',
form=form,
emp_length_cat_input=session.get('emp_length_cat_input'),
home_status_input=session.get('home_status_input'),
zip3_input=session.get('zip3_input'),
total_acc_input=session.get('total_acc_input'),
annual_inc_input=session.get('annual_inc_input'),
dti_input=session.get('dti_input'),
descr_input=session.get('descr_input')
)
<form method="POST" action="" novalidate>
{{ form.hidden_tag() }}
{{ form.emp_length_cat_input.label }} {{ form.emp_length_cat_input(id='emp_length_cat') }} </br>
{{ form.home_status_input.label }} {{ form.home_status_input(id='home_status') }} </br>
{{ form.zip3_input.label }} {{ form.zip3_input(id='zip3') }} </br>
{{ form.total_acc_input.label }} {{ form.total_acc_input(id='total_acc') }} </br>
{{ form.annual_inc_input.label }} {{ form.annual_inc_input(id='annual_inc') }} </br>
{{ form.dti_input.label }} {{ form.dti_input(id='dti') }} </br>
{{ form.descr_input.label }} {{ form.descr_input(id='descr') }} </br>
{{ form.submit() }}
</form> 
If you're going to use form as a POST method you can simply put a button within your form inside your template to submit to form, and that should send a POST request which will be your form submitted.
<form method="post">
...
<button type="submit">Submit</button>
</form>
You must specify the type of the button. I solved the problem like this
class ApplicationForm(FlaskForm):
...
submit = SubmitField(
'Submit',
render_kw={'type':'submit'})
And you need to fix the action path
<form method="POST" action="/">
Update:
I believe something was wrong with my submit button. I used {{ wtf.quick_form(form) }} to render the form and it worked! Thank you.

Should I use request.GET['...'] or form.cleaned_data.get('...')

I have created a function for a search form form my database, method works fine, but I, don't know whether I should use queryBooks = request.GET['queryBooks'] or form.cleaned_data.get('queryBooks')
Here is my code.
# views.py
def SearchBook(request):
error = False
message = ''
books = Books.objects.all()
if 'queryBooks' in request.GET:
queryBooks = request.GET['queryBooks']
if not queryBooks:
error = True
message = u'enter book or author title'
else:
books = Books.objects.filter\
(
Q(book__icontains=queryBooks) | Q(Author__name__icontains=queryBooks)
)
contexto = {'Books': books, 'Error': error, 'Message': message}
return render(request, 'list_of_book.html', contexto)
# list_of_book.html
<form action="" method="get">
<input type="text" name="queryBooks">
<input type="submit" value="search">
</form>
# urls.py
url(r'^books/search/$', SearchBook, name='searchBook'),
There is no form in your view, so
form.cleaned_data.get('queryBooks')
Would give you an error.
In general, I recommend that you learn about Django forms, as they take care of rendering the html, and validating the input from the user. For your specific example, fetching the query string from request.GET is probably ok.

Display a Django form on a page

I am trying to display a comment form on a page. So far I have created a link and I want that each time that link is clicked it displays the form on the same page as where the link is but my problem here is that the link redirects me to another page, which I don't want.
urls.py
url(r'^all/$', 'posts.views.articles'),
url(r'^get/(?P<post_id>\d+)/$', 'posts.views.article'),
url(r'^articles/$', 'posts.views.create'),
url(r'^like/(?P<post_id>\d+)/$', 'posts.views.like_article'),
url(r'^article/(?P<post_id>\d+)/$', 'posts.views.add_comment'),
views.py
def articles(request):
args = {}
args.update(csrf(request))
args ['posts'] = post.objects.filter(user = request.user)
args ['full_name'] = User.objects.get(username = request.user.username)
args ['form'] = PostForm()
return render_to_response('articles.html', args)
def article(request, post_id=1):
return render(request, 'article.html',
{'post': post.objects.get(id=post_id) })
def add_comment(request, post_id):
a = post.objects.get(id=post_id)
if request.method == "POST":
f = CommentForm(request.POST)
if f.is_valid():
c = f.save(commit=False)
c.pub_date = timezone.now()
c.article = a
c.save()
messages.success(request, "You Comment was added")
return HttpResponseRedirect('/posts/get/%s' % post_id)
else:
f = CommentForm()
args = {}
args.update(csrf(request))
args['post'] = a
args['form'] = f
return render_to_response('article.html', args)
#return HttpResponseRedirect('/posts/all')
article.html
<h2>Comments</h2>
{% for c in post.comment_set.all %}
<p>{{c.name}} : {{c.body}}</p>
{% endfor %}
<form action="/posts/article/{{post.id}}/" method="post">{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type="submit" name="submit" value="Post Comment">
</form>
{% endblock %}
As from your question you want submit a comment in your article and when you submit the comment you want to redirect it to the same article page... If you are willing to do this then here is example:
First create a comment submit form either using model form or just form:
class CommentCreationForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('comment_body',) # Set your field for comment
Now pass this form as context in ur article view. Like you did above.
def articles(request):
args = {}
args.update(csrf(request))
args ['posts'] = post.objects.filter(user = request.user)
args ['full_name'] = User.objects.get(username = request.user.username)
args ['comment_form'] = CommentCreationForm
return render_to_response('articles.html', args)
Your article.html
<h2>Comments</h2>
{% for c in post.comment_set.all %}
<p>{{c.name}} : {{c.body}}</p>
{% endfor %}
<form action=""{% url "comment_submit" post.id %}"" method="get">{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type="submit" name="submit" value="Post Comment">
</form>
{% endblock %}
Catch the url with url(r'^comment/(?P<pk>\d+)/',CommentSubmitView, name="comment_submit"), and write a view.
def AnswerSubmitView(request, pk):
post = Post.objects.get(id=pk) # Get what you have set for your article
comment_text = request.GET.get('comment_body',False)
com = Comment()
post = post # or anything that you have named for your article..
com.comment_body = comment_text
com.save()
return HttpResponseRedirect('/post/%s' % pk) # Your url for your article I guess...
Enjoy...
Use an ajax call to fetch the form from the server without refreshing the page. This requires jQuery. Replace the placeholder selectors I've used with whatever you need for your app. I'd recommend wrapping all of article.html in a div and give that an id tag (and refer to this tag where I use '#form-id' selector below), so you know when the form is already displayed and you can access the entire chunk.
Also note that I'm not entirely sure how to get the html from render_to_response. Just figure out what kind of object is sent back to the ajax caller and how to get the html from that object. Shouldn't be hard.
Adapt and add the following to the bottom of the template containing the link to add the form
<script>
var showForm = function(url) {
$.ajax({
type: 'GET',
dataType: 'json',
url: url,
success: function(data, status, xhr) {
// Not positive if this is how things work with render_to_response
// I usually use render_to_string for this and just return pure HTML
$('#div-to-display-form-in').append(data);
},
error: function(error) {
// Handle error
}
});
}
$(document).ready(function() {
$('#link-to-show-form').click(function(event) {
event.preventDefault();
// The conditionals check if form is already showing
// If form already showing and link clicked again, form is removed
if ($('#form-id').length === 0) {
showForm($(this).attr('href'));
} else {
$('#form-id').remove();
}
});
});
</script>

Django: keep data visible in multiple individual forms

I have three forms in three tabs respectively in a template.
I fill in the first tab, submit, fill in the second form, submit, fill in the third form, submit.
Is there a way to keep the data in each form after saving? I need to show them all together.
No, when I save the second form I lose my data on the first form.
input.html: template
<form class="tab-pane fade in active" id="demographics" method="post" >
<input type="hidden" name="form_id" value="demographics">
{%crispy frm%}
</form>
<form class="tab-pane fade" id="diagnosis" method="post">
<input type="hidden" name="form_id" value="diagnosis">
{%crispy frm_d%}
</form>
<form class="tab-pane fade" id="a_b_sickle" method="post">
<input type="hidden" name="form_id" value="a_b_sickle">
{%crispy frm_a_b_s%}
</form>
views.py
def input(request):
context = RequestContext(request)
if request.method == 'POST':
submitted = request.POST.get('form_id', '')
print submitted
if submitted == 'demographics':
# Get the Form1 instance
my_demographics = DemographicForm(request.POST)
#my_diagnosis = DiagnosisForm()
if my_demographics.is_valid():
my_demographics_object= my_demographics.save()
my_diagnosis=DiagnosisForm({'patient': my_demographics_object.patient_id, 'diagnosis_option': 'b-thalassaemia syndromes'})
my_a_b_sickle=A_b_sickle_thalForm({'patient': my_demographics_object.patient_id})
else:
my_diagnosis=DiagnosisForm()
my_a_b_sickle = A_b_sickle_thalForm()
elif submitted == 'diagnosis':
my_diagnosis = DiagnosisForm(request.POST)
my_demographics = DemographicForm()
my_a_b_sickle = A_b_sickle_thalForm()
if my_diagnosis.is_valid():
my_diagnosis_object=my_diagnosis.save()
my_a_b_sickle =A_b_sickle_thalForm({'patient': my_diagnosis_object.patient})
else:
my_demographics = DemographicForm()
my_a_b_sickle = A_b_sickle_thalForm()
elif submitted == 'a_b_sickle':
my_a_b_sickle = A_b_sickle_thalForm(request.POST)
my_demographics = DemographicForm()
my_diagnosis = DiagnosisForm()
if my_a_b_sickle.is_valid():
my_a_b_sickle.save()
else:
raise ValueError('No form specified !')
else:
my_demographics = DemographicForm()
my_diagnosis = DiagnosisForm()
my_a_b_sickle= A_b_sickle_thalForm()
return render(request,'input.html', {'frm':my_demographics, 'frm_d': my_diagnosis, 'frm_a_b_s': my_a_b_sickle})
Any ideas please?
You can either submit them using ajax or send them back filled in your context. If you can get the saved objects between request by any association they have you can try:
For the second way you can try something like this:
my_demographics = DemographicForm(my_demographic_object)
my_diagnosis = DiagnosisForm(my_diagnosis_object)
my_a_b_sickle= A_b_sickle_thalForm(my_a_b_sickle)
return render(request,'input.html', {'frm':my_demographics, 'frm_d': my_diagnosis, 'frm_a_b_s': my_a_b_sickle})
In fact, with that you can even make that work as an update form with a little extra using the 'instance' attribute of ModelForm.

Django. Crispy forms. showing error messages with crispy filter and customizing them

I am new to django forms and Crispy Forms. I have some simple forms in a little forum Im developing. I think I don't need to use the %crispy% tag. I only need the form|crispy filter. However, I don't know why they don't render the error messages.
Also, if I want to customize the error messages (they must be in spanish), do I need to use the %crispy% tag or is it possible to do this with the |crispy filter?
Anyway, here is one of my forms:
from django import forms
from django.forms import Textarea
class FormNuevoVideo(forms.Form):
url = forms.URLField(initial='http://', max_length=250)
titulo = forms.CharField(max_length=150)
descripcion = forms.CharField(
help_text="...",
widget=Textarea(attrs={'rows': 3, 'data-maxlength': 500}))
Here is the view:
#login_required
def nuevo_video(request, slug):
template = 'videos/nuevo.html'
tema = Temas.objects.get(slug=slug)
if request.method == 'POST':
form = FormNuevoVideo(request.POST)
if form.is_valid():
...
nuevo_video.save()
return redirect('videos:videos_tema', slug=tema.slug, queryset='recientes')
else:
return redirect('videos:nuevo_video', slug=tema.slug) #this same view.
else:
form_nuevo_video = FormNuevoVideo()
context = {'form_nuevo_video': form_nuevo_video, 'tema': tema}
return render(request, template, context)
And in the HTML:
{% block form %}
<form action = "{% url 'videos:nuevo_video' tema.slug %}" method = "post">
{% csrf_token %}
{{form_nuevo_video|crispy}}
<input class = "btn pull-right" type = "submit" value ="enviar"/>
</form>
{% endblock form %}
So, lets say, when someone tries to submit a video with a title of more than 150 characters, it doesn't display the error. I am sure I am missing something simple. Also, I'd like to customize the error messages so that they are in spanish. Thanks in advance.