Flask: miss some blocks - python

I try to write simple service with flask but I have some problem. I try to render a template after submitting, but It doesn't go to some blocks
Main code
#app.route('/', methods=['GET', 'POST'])
def main_page():
main_form = Main_Submit_Form()
text_form = Checking_Text_Form()
image_form = Checking_Image_Form()
if main_form.validate_on_submit():
if main_form.name_checking.data:
if text_form.validate_on_submit():
request_name = str(text_form.checking_name.data)
results = model.predict_name(request_name)
return render_template('text_data.html', request_name=request_name, results=results)
return render_template('login.html', title='Sign In', form=text_form)
if main_form.image_checking.data:
if image_form.validate_on_submit():
pass
return render_template('main.html', form=main_form)
Main form looks like
from flask_wtf import FlaskForm
from wtforms import SubmitField
class Main_Submit_Form(FlaskForm):
name_checking = SubmitField(label='Проверка Торгового наименования')
image_checking = SubmitField(label='Проверка Логотипа')
Text form looks like
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, FileField
from wtforms.validators import DataRequired
class Checking_Text_Form(FlaskForm):
checking_name = StringField('Введите наименование компании', validators=[DataRequired()])
submit_text = SubmitField('Проверить')
Main html
{% extends "base.html" %}
{% block content %}
<h2>Проверка товарного знака</h2>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p class="text">{{ form.name_checking() }}</p>
<p class="image">{{ form.image_checking() }}</p>
</form>
{% endblock %}
Login html
{% extends "base.html" %}
{% block content %}
<h2>Проверка товарного знака</h2>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.checking_name.label }}<br>
{{ form.checking_name(size=32) }}
</p>
<p>{{ form.submit_text() }}</p>
</form>
{% endblock %}
Textdata html
{% extends "base.html" %}
{% block content %}
<h2>Результаты по запросу: {{ request_name }}</h2>
{% for result in results %}
<div><p>{{ result.company_name }}, сходство: <b>{{ result.cosine_sim }}</b></p></div>
{% endfor %}
{% endblock %}
I can't go into block with checking if text_from.validate_on_submit() and I can't understand why.
How can I go into this block after rendering login.html template?
And is any way simplify this code? I don't think that it's a good example

Related

python django why the form is not showing in the html page?

** for some reason my form is not showing in the template can someone tell me why ,
think you.
**
views.py
i think the problem is here but i cant find it i followed the documentation and got nothing also
def contact(request):
print(request.POST)
forms=contactform(request.POST or None)
if forms.is_valid():
print(forms.cleaned_data())
context= {
"title":"contact",
"form": forms
}
return render(request,"form.html",context)
```
> forms.py
```
from django import forms
class contactform(forms.ModelForm):
full_name=forms.CharField()
email=forms.EmailField()
text=forms.CharField(widget=forms.Textarea)
```
> form.html
```
{% extends "gta.html" %}
{% block ferr%}
{% if title %}
<h1>{{title}}</h1>
{% endif %}
<form method="POST" action="."> {% csrf_token %}
{{forms.as_p}}
<button type="submit">submitt</button>
</form>
{% endblock %}
This should sort you.
{% extends "gta.html" %}
{% block ferr%}
{% if title %}
<h1>{{title}}</h1>
{% endif %}
<form method="POST" action="."> {% csrf_token %}
{{form.as_p}}
<button type="submit">submitt</button>
</form>
{% endblock %}

Flask-WTForms auto-submit onchange with macro not working

I would like my form to automatically submit once the last field detects an input. Here is what I have on the HTML side based on what I could find online...I know I'm probably missing something...
{% extends "base.html" %}
{% from 'bootstrap/form.html' import render_field %}
{% block content %}
<img src="/static/logo.jpg" alt="logo" width="50%" height="auto">
<h1>Scan ABC...</h1>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.A.label }}<br>
{{ form.A(autofocus=true, required=true, size=32) }}
</p>
<p>
{{ form.B.label }}<br>
{{ form.B(required=true, size=32) }}
</p>
<p>
{{ form.C.label }}<br>
{% macro render_field(C(**{required=true, size=32, onchange="this.form.submit()"})) %}
{% endmacro %}
</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}
Here is my form.py
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField
from wtforms.validators import DataRequired
class Form(FlaskForm):
A= StringField('A', validators=[DataRequired()])
B= StringField('B', validators=[DataRequired()])
C= IntegerField('C', validators=[DataRequired()])
submit = SubmitField('Submit')
I get this error
jinja2.exceptions.TemplateSyntaxError: expected token ',', got '('
on this line
{% macro render_field(weight(**{required=true, size=32, onchange="this.form.submit()"})) %}
Does it work in other cases without auto-submit? In my case, I only use onchange="submit()", i.e.for example:
{{ filterform.search_text(class="form-control-button", onchange="submit()") }}
which leads to:
<input checked class="form-control-button" id="search_keywords" name="search_keywords" onchange="submit()" type="checkbox" value="y">
Maybe you want to try onchange="submit()"?

Flask Form not submitting

I am learning Flask and trying build a very simple app which has few parts creating new users and searching the existing users. The create/register new users part is working just fine, but for some reason the search function is not working as expected because the form is not getting submitted. I have tried to look every possible case which I think of, searched Stackoverflow as well but could not get to fix it.
The code for registration functionality:
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, length
class Register(FlaskForm):
name = StringField('name', validators=[DataRequired(), length(min=1, max=30)])
email = StringField('email', validators=[DataRequired()])
submit = SubmitField('Register')
class UserSeacrh(FlaskForm):
name = StringField('name', validators=[DataRequired()])
email = StringField('email', validators=[DataRequired()])
submit = SubmitField('Search')
views.py
#app.route("/register", methods=['GET', 'POST'])
def register():
form = Register()
if form.validate_on_submit():
db.create_all()
user = CreateUser(form.name.data, form.email.data)
db.session.add(user)
db.session.commit()
flash('User {} created successfully'.format(form.name.data))
return redirect(url_for("register"))
return render_template("register.html", form=form, title='New User Registration')
#app.route("/search", methods=['GET', 'POST'])
def user_profile():
form = UserSeacrh()
if form.validate_on_submit():
flash('{}'.format(user))
return redirect(url_for("home"))
#return render_template("users.html", users=user)
return render_template("userSearch.html", form=form)
register.html
<!DOCTYPE html>
{% extends "base.html" %}
{% block body %}
<form action="" method="post">
{{ form.hidden_tag() }}
<div>
<h4>User Registeration</h4>
<span>Name {{form.name}}</span>
<span style="color: red">
{% for error in form.name.errors %}
{{error}}
{% endfor %}
</span
<span>Email {{form.email}}</span>
<span style="color: red">
{% for error in form.email.errors %}
{{error}}
{% endfor %}
</span>
</div>
{{form.submit()}}
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<br/>
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% endblock %}
userSearch.html
<!DOCTYPE html>
{% extends "base.html" %}
{% block body %}
<form action="" method="post">
{{ form.hidden_tag() }}
<h4>User Search Form</h4>
<div>
<span>Email {{form.email}}</span>
<span style="color: red">
{% for error in form.email.errors %}
{{error}}
{% endfor %}
</span>
</div>
{{form.submit()}}
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<br/>
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% endblock %}
I am clueless now, can someone please help me out here?
Most likely the reason is that in the userSearch.html you didn't add the name field. In the UserSeacrh class the field name has a validator DataRequired which most likely creates the problem. Just remove the name field in the UserSeacrh class if you want to do the search by email only.
By the way there is a typo in UserSeacrh - it should be UserSearch. But in your case it's not the problem.
I think you need to specify the action for your form, i.e., what URL to POST the completed form to.
<form action="" method="post">
I was able to solve this using Nurzhan' tip by removing the name field from UserSearch form from forms.py .

Flash messages with flask-bootstrap and flask-nav

I'm using flask-bootstrap to use the Bootstrap front-end for a flask web application. Unfortunately, since I've started using flask-bootstrap and flask-nav, I'm not able to display flash messages.
This is the base.html:
{% extends 'bootstrap/base.html' %}
{% block navbar %}
{{ nav.mynavbar.render() }}
{% endblock %}
<html>
<body>
<hr>
<div class='container'>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endwith %}
{% block content %}{% endblock %}
</div>
</body>
</html>
This is one of the view that should flash a message when a change is saved:
#app.route('/model/<model_name>/edit', methods=['GET', 'POST'])
def edit(model_name):
"""Edit model's configuration.
Args:
model_name [str]: The name of the model we want to change the
configuration.
"""
# return to index page if model is not found in the database
model = DSModel.query.filter_by(name=model_name).first()
if not model:
flash('Model {} not found.'.format(model_name))
return redirect(url_for('index'))
# pre-load current model's configuration
form = ConfigurationForm()
# if it's a POST request, it means the user is trying to change the model's
# configuration. Save changes in the database
if request.method == 'POST': # and form.validate_on_submit():
model.configuration.configuration = form.value.data
model.configuration.datetime = datetime.datetime.utcnow()
db.session.add(model)
db.session.commit()
flash('Your changes have been saved.', 'success')
return redirect(url_for('edit', model_name=model_name))
else:
form.value.data = model.configuration.configuration
return render_template('edit.html', form=form)
Finally, this is the __init__.py:
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import Navbar, View
from flask_sqlalchemy import SQLAlchemy
nav = Nav()
#nav.navigation()
def mynavbar():
return Navbar(
'Dashboard',
View('Home', 'index'),
View('Login', 'login')
)
app = Flask(__name__)
Bootstrap(app)
nav.init_app(app)
app.config.from_object('config')
db = SQLAlchemy(app)
from app import views, models
I think there must be something funky with my base.html file, but I'm not terribly familiar with HTML, so I'm not able to find what's wrong. I've looked into examples online (i.e. here), but the format seems to be pretty similar to what I'm doing.
EDIT: This is the edit.html file:
{% extends 'base.html' %}
{% block content %}
<h1>Update configuration</h1>
<form action='' method='post' name='configuration'>
<p>
Please update the current model configuration:<br>
{{ form.value(cols='150', rows='20') }}
<p><input type='submit' value='Save'></p>
</form>
{% endblock %}
Try to edit your base.html to this:
{% extends 'bootstrap/base.html' %}
{% block navbar %}
{{ nav.mynavbar.render() }}
{% endblock %}
<html>
<body>
<hr>
<div class='container'>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</div>
{% block content %}{% endblock %}
</div>
</body>
</html>

Django-likes repeats the same post

I'm using the app django-likes and have managed to get it working. But I'm running into a very weird problem. The model "Story" is associated with the likes. Now the Story posts are repeated the number of votes they have -- for example, if they have 4 votes, they're actually repeated 4 times. Not sure what's going on. Any help much appreciated.
views.py
def home(request):
posts = Story.objects.all().order_by('votes')
form = StoryForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Thanks for adding your mix!')
return render_to_response('home.html',
locals(),
context_instance=RequestContext(request))
models.py
from django.db import models
import secretballot, likes
class Story(models.Model):
text = models.CharField(max_length=2000)
def __unicode__(self):
return self.text
secretballot.enable_voting_on(Story)
home.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load likes_inclusion_tags %}
{% block content %}
{% if user.is_authenticated %}
<h1 align=center>Add Your Mix!</h1>
<div class='col-sm-6 col-sm-offset-3'>
<h1>{{ form_title }}</h1>
<form method="POST" action="" >{% csrf_token %}
{{ form|crispy }}
<input type='submit' class='btn' value='Add{{ button_submit }}' />
</form>
</div>
{% else %}
<h1 align=center>Login and add a story!</h1>
<p align=center><a class="btn btn-primary btn-lg" href="/accounts/login/" role="button">Login!</a></p>
{% endif %}
{% for post in posts %}
<p>{{ post.text }} {% likes post %}</p>
{% endfor %}
{% endblock %}
It is possible that the {% likes post %}returns a number and Multiplies the string {{ post.text }} five times like python would. Try separating it or converting it to string.

Categories