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 .
Related
One of functionality in my training project:
subscribe to the news by check-box and e-mail.
Send newsletter daily.
The user can unsubscribe from the mailing list in his profile by unchecking the checkbox.
It so happened that first I set up a daily newsletter for users who have booleanfield = true.
For it I marked the checkboxes in the admin panel. It works.
Now it is necessary to add the checkbox and the mail field to the news page.
I'm stuck on the simplest. Tired and confused.
Please help me place a checkbox and a mail box with a send button on the news page
models.py
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
hr = models.BooleanField(default=False)
subscribed_for_mailings = models.BooleanField(default=False)
subscription_email = models.EmailField(default="")
def __str__(self):
return str(self.user)
Forms.py
class MailingForm(forms.ModelForm):
class Meta:
model = models.Profile
fields = ('subscription_email', 'subscribed_for_mailings', )
widgets = {
'subscription_email': forms.EmailInput(attrs={"placeholder": "Your Email..."}),
'subscribed_for_mailings': forms.CheckboxInput,
}
views.py
def all_news(request):
today = date.today()
today_news = models.TopNews.objects.filter(created__gte=today)
return render(request, "news.html",
{'today_news': today_news})
def mailing_news(request):
if request.method == 'POST':
mailing_form = forms.MailingForm(request.POST)
if mailing_form.is_valid():
mailing_form.save()
return HttpResponse('You will receive news by mail')
else:
mailing_form = forms.MailingForm()
return render(request, "news.html", {'mailing_form': mailing_form})
urls.py
...
path('news/', views.all_news, name='all_news'),
...
news.html
{% extends 'base.html' %}
{% block title %}
News
{% endblock %}
{% block body %}
<h1>Last news</h1>
{% for news in today_news%}
<h3>{{ news.title }}</h3>
Read this news
<p>
{{ news.created }}
</p>
<hr>
{% endfor %}
<h4>I want to receive news by mail</h4>
<form action="." method="post">
{{ mailing_form.as_p }}
{% csrf_token %}
<label>
<input type="submit" value="Subscribe">
</label>
</form>
{% endblock %}
The page displays a list of news and only the "send" button. There is no check-box and a field for mail
enter image description here
Finally I realized this functionality in a different way:
forms.py
class MailingForm(forms.ModelForm):
class Meta:
model = models.Profile
fields = ('subscribed_for_mailings', 'subscription_email', )
views.py
#login_required
def mailing_news(request):
if request.method == "POST":
mailing_form = forms.MailingForm(request.POST,
instance=request.user.profile,
)
if mailing_form.is_valid():
mailing_news = mailing_form.save(commit=False)
mailing_news.subscribed_for_mailings = mailing_news.subscribed_for_mailings
mailing_news.subscription_email = mailing_news.subscription_email
mailing_news.save()
return render(request, "subscribe_complete.html",
{"mailing_news": mailing_news})
else:
mailing_form = forms.MailingForm()
return render(request, 'subscribe.html', {"mailing_form": mailing_form})
news.html
{% extends 'base.html' %}
{% block title %}
News
{% endblock %}
{% block body %}
<h1>Last news</h1> {{ news.created }}
{% for news in today_news%}
<h3>{{ news.title }}</h3>
Read this news
<hr>
{% endfor %}
I want to receive news by mail
{% endblock %}
urls.py
...
path('subscribe/', views.mailing_news, name='subscribe')
...
news.html
{% extends 'base.html' %}
{% block title %}
News
{% endblock %}
{% block body %}
<h1>Last news</h1> {{ news.created }}
{% for news in today_news%}
<h3>{{ news.title }}</h3>
Read this news
<hr>
{% endfor %}
I want to receive news by mail
{% endblock %}
subscribe.html
{% extends 'base.html' %}
{% block title %}
Subscribe
{% endblock %}
{% block body %}
<form action="." method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ mailing_news.as_p }}
{% if user.profile.subscribed_for_mailings is True %}
<input type="checkbox" name="subscribed_for_mailings" id="id_subscribed_for_mailings" checked="">
If you don't want to receive emails anymore, uncheck
<br>
Subscription email: <input type="email" name="subscription_email" value={{ user.profile.subscription_email }} class="vTextField" maxlength="254" id="id_subscription_email">
{% else %}
<label>
<input type="checkbox" name="subscribed_for_mailings" id="id_subscribed_for_mailings">
I want to subscribe for mailing news
</label>
<p><label>
Send news on my email:
<input type="email" name="subscription_email" class="vTextField" maxlength="254" id="id_subscription_email">
</label></p>
{% endif %}
<p><input type="submit" value="Update"></p>
</form>
{% endblock %}
subscribe_complete.html
{% extends 'base.html' %}
{% block title %}
Subscribing complete
{% endblock %}
{% block body %}
<h3>Hi {{ user.username }}</h3>
Thanks for subscribing.
You will receive daily news by email: {{ user.profile.subscription_email }}
{% endblock %}
you need to change subscribed_for_mailings in mailing news, like this
def mailing_news(request):
if request.method == 'POST':
mailing_form = forms.MailingForm(request.POST)
if mailing_form.is_valid():
profile = mailing_form.save(commit=False) ####
profile.subscribed_for_mailings = mailing_form.cleaned_data.get('subscribed_for_mailings') ####
profile.subscription_email = mailing_form.cleaned_data.get('subscription_email') ####
profile.save() #### new_line
return HttpResponse('You will receive news by mail')
else:
mailing_form = forms.MailingForm()
return render(request, "news.html", {'mailing_form': mailing_form})
you can change in cleaned_data.get('....')
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
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>
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.
I am writing a very simple navigation bar. For example I have my login view down below. When I open the login page I can see my the results of my base.html but for some reason I can not see the results of my login.html, meaning I cant see the form I wrote only the top links bar.
view.py -- login view
def login(request):
if request.method == 'POST':
form = UserLoginForm(request.POST)
if form.is_valid():
m = UserLogin.objects.get(user_name=request.POST['user_name'])
if m.password == request.POST['password']:
request.session['member_id'] = m.id
return HttpResponseRedirect('/game')
else:
c = {'form': form,
'error_message': "Your username and password didn't match."}
c.update(csrf(request))
return render_to_respons('game/login.html', c)
else:
form = UserLoginForm()
c = {'form': form}
c.update(csrf(request))
return render_to_response('game/login.html', c)
base.html
<div id="navigation">
Home
Upload
Register
Login
</div>
login.html
{% extends "base.html" %}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/game/login/" method="post">
{% csrf_token %}
<table border='0'>
<div class="fieldWrapper"><tr><td>
{{ form.user_name.errors }}</td><td></td></tr><tr><td>
<label for="id_user_name">User Name:</label></td><td>
{{ form.user_name }}</td></tr>
</div>
<div class="fieldWrapper"><tr><td>
{{ form.password.errors }}</td><td></td><tr><td>
<label for="id_password">Password:</label></td><td>
{{ form.password }}</td></tr>
</div>
</table>
<input type="submit" value="Login" />
</form>
You need to define blocks to use template inheritance. For example, change your base.html to:
<div id="navigation">
Home
Upload
Register
Login
</div>
{% block content %}{% endblock %}
then place your login code into the content block:
{% extends "base.html" %}
{% block content %}
...login.html content goes here
{% endblock %}
Check out the docs for more detail.