Django 1.10 - Issue passing value from Template to View - html

I am having an issue when it cmoes to passing variables from templates to views. Even though I am able to pass variables from view to template, I canot seem to get it right. I have looked at similar questions here.
Following the Django docs I created a forms.py script as follows:
forms.py
GNU nano 2.7.4 File: forms.py
from django import forms
class TactForm(forms.Form):
tacttime = forms.CharField(label='Tact Time', max_length=100)
Updated View
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from lineoee.models import Lineoee31
from .forms import TactForm
def details(request):
if request.method == 'POST':
form = TactForm(request.POST)
print(form)
else:
form = TactForm()
context = {'form' : form}
return render(request, 'linedetails/index.html',context)
Updated Template
<form method="POST" action="{% url 'details' %}">
{% csrf_token %}
{{ form.as_p }}
<label for="tacttime">Tact Time: </label>
<input id="tacttime" type="text" name="tacttime" value ="60">
<input type="submit" value="OK">
<form>
Updated URLS
from django.conf.urls import url
from django.contrib import admin
from lineoee.views import index
from lineoee.views import details
urlpatterns = [
url(r'lineoee/$', index, name='index'),
url(r'linedetails/', details, name='details'),
]
Still, no errors and no values passed to the view.
EDIT
I am now getting some data on pressing the OK button, however it is not what I was expecting. I want to be able to retrieve the text entered into the input field. How can I do this?
"POST /linedetails/ HTTP/1.1" 200 24580
<tr><th><label for="id_tacttime">Tact Time:</label></th><td><input
type="text" name="tacttime" value="60" required id="id_tacttime"
maxlength="100" /></td></tr>

Template
<div style="text-align:center;">
<form method="POST" action="{% url 'details' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="adsfadsfas">
</form>
</div>
Views (EDITED)
Whatever the name you use in your input on your HTML template, that's the key you're gonna use to get what comes in the request.POST. That's why you'd like to use {{ form.field }} in the template so you know beforehand the name of the fields you're expecting to come in the request.POST
def details(request):
if request.method == 'POST':
print(request.POST)
print(request.POST.get('tacttime')
form = TactForm(request.POST)
print(form)
else:
form = TactForm()
return render(request, 'linedetails/index.html', context)
URLS
from django.conf.urls import url
from django.contrib import admin
from lineoee.views import index
from lineoee.views import details
urlpatterns = [
url(r'lineoee/$', index, name='index'),
url(r'linedetails/', details, name='details'),
]

def details(request):
if request.method == 'POST':
var = request.POST['textfield']
print(var)

Related

How can I create redirect button with post method in Django template

I'm trying to create a button in the Django template which will redirect to another URL. But getting error 404 since Django can't recognize URL path rescribed in the urls.py.
HTML part
<form method="post" action='sts'>
{% csrf_token %}
<button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="cts_link">cts</button>
</form>
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from rtRegRes.views import units
from rtRegRes.views import spartan
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^units/$', units),
url(r'^units/sts/?$', spartan),
]
views.py
from django.shortcuts import render, redirect, reverse, render_to_response
from .models import rt_reg_res
from django.http import HttpResponse, JsonResponse
def units(request):
"""Return main webpage"""
return render_to_response('runtime.html')
def spartan(request):
"""Link to the other unit webpages"""
table = rt_reg_res.objects.all()
if request.method == 'POST':
qatables = request.POST.get("cts_link")
if qatables:
return render(request, 'cts.html', {'table': table})
Clicking the button following error message appears:
enter image description here
Could somebody point me what is wrong in my code
Thanks
from django.conf.urls import include, url
from django.contrib import admin
from rtRegRes.views import spartan , units
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^units/$', units),
url(r'^units/sts/?$', spartan, name='sts'),
]
and
<form method="post" action='sts'>
{% csrf_token %}
<button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="cts_link">cts</button>
</form>
Swapping the order of urls should help.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^units/sts/?$', spartan),
url(r'^units/$', units),
]
What a pity, I wasted my time for nothing (( My code was correct, I just forget to rerun server after changes, every time reloaded page instead and did not see real changes. Anyway, thanks for the responses guys!!

Getting Django CSRF error with raw html form

I'm trying to set up a raw html form where a user can make a suggestion and then save it on a database with a POST method, but I keep getting a Forbidden (403) CSRF verification failed. Request aborted. even after following the steps in the Help section.
I have found that I don't get the error if I add csrf_exempt on top of my view like this:
from django.views.decorators.csrf import csrf_exempt
#csrf_exempt
def suggest_ptags(request):
context = {}
print("Form is submitted.")
return render(request, "partials/search_form.html", context)
But I was made aware that It removes completly the CSRF protection and I don't want that.
So what should I do?
Here's my search_form.html form in a partials folder in templates:
<!-- Suggestion Form in popup -->
<div class="prop-modal">
<div class="prop-content">
<a class="btn-close-prop">×</a>
<img src="{% static 'images/pyramids.svg' %}">
<form action="/suggest_ptags/" class="feedback-form" method="POST" enctype="text/plain">
{% csrf_token %}
<h5 class="title-prop">Suggestion</h5>
<input class="input-prop" name="suggest" rows="3" cols="37" placeholder="suggest something..."></input>
<input class="button-prop" type="submit" value="Envoyez"></input>
</form>
</div>
</div>
My current Views.py:
from django.views.decorators.csrf import ensure_csrf_cookie
#ensure_csrf_cookie
def suggest_ptags(request):
context = {}
print("Form is submitted.")
return render(request, "partials/search_form.html", context)
And in my Urls:
from django.conf.urls import url
from django.contrib import admin
from search.views import HomeView, ProductView, FacetedSearchView, autocomplete, suggest_ptags
from .settings import MEDIA_ROOT, MEDIA_URL
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', HomeView.as_view(), name='home'),
url(r'^admin/', admin.site.urls),
url(r'^suggest_ptags/$', suggest_ptags, name='suggest_ptags'), #Suggestions
url(r'^product/(?P<slug>[\w-]+)/$', ProductView.as_view(), name='product'),
url(r'^search/autocomplete/$', autocomplete),
url(r'^search/', FacetedSearchView.as_view(), name='haystack_search'),
] + static(MEDIA_URL, document_root=MEDIA_ROOT)
Any solutions?
You shouldn't use enctype="text/plain". You can remove it (which is the same as enctype="multipart/form-data"), or use enctype="multipart/form-data" if you are uploading files.

Jinja2 Exceptions - Cannot find attribute

Working on a problem in Flask/ Python. Had a few of these errors pop up and I've been able to squash them as they arise; however, this one I cannot seem to get to the bottom of.
I have a simple form which allows users to login.
But each time I load the page I am greeted with this error:
jinja2.exceptions.UndefinedError: 'shop.forms.LoginForm object' has no attribute 'submit'
Below is the code that I am working with, thanks in advance.
p.s. I have seen similar posts regarding the hidden_tag() attribute, but the fixes suggested are not working for this scenario.
routes.py
import os
from flask import render_template, url_for, request, redirect, flash
from shop import app, db
from shop.models import Author, Book, User
from shop.forms import RegistrationForm, LoginForm
from flask_login import login_user, current_user, logout_user, login_required
#app.route("/login", methods=['GET', 'POST'])
def login():
form = LoginForm()
if request.method == 'POST':
user = User.query.filter_by(email=form.email.data).first()
if user is not None and user.verify_password(form.password.data):
login_user(user)
return redirect(url_for('home'))
return render_template('login.html', title='Login', form=form)
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo
class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
sumbit = SubmitField('Login')
login.html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
{% extends "layout.html" %}
{% block content %}
<form method="POST" action="">
{{ form.csrf_token }}
<div class="">
{{ form.email.label }} {{ form.email}}
</div>
<div class="">
{{ form.password.label }} {{ form.password}}
</div>
<div class="">
{{ form.submit() }}
</div>
</form>
{% endblock content %}
</body>
</html>
EDIT: Removing the () from submit doesn't solve the issue. Just removes the instance of the button entirely from the template. See below:
Change form.submit() to form.submit and it will show the submit button in template.
Here is an example of using flask_wtf for a login form.
Example of using Flask wtform:
app.py:
from flask import render_template, url_for, request, redirect, flash, Flask
from forms import LoginForm
app = Flask(__name__)
app.secret_key = 'secret key'
#app.route("/login", methods=['GET', 'POST'])
def login():
form = LoginForm()
if request.method == 'POST':
user_email = form.email.data
user_password = form.password.data
if user_email and user_password:
return "{} - {}".format(user_email, user_password)
return render_template('login.html', title='Login', form=form)
if __name__ == '__main__':
app.run(debug=True)
forms.py:
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo
class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Login')
login.html:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST" action="">
{{ form.csrf_token }}
<div class="">
{{ form.email.label }} {{ form.email }}
</div>
<div class="">
{{ form.password.label }} {{ form.password }}
</div>
<div class="">
{{ form.submit }}
</div>
</form>
</body>
</html>
Output:
Get request of login route:
Post request of login route:
Updates:
requirements.txt:
Click==7.0
Flask==1.0.2
Flask-WTF==0.14.2
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.1
Werkzeug==0.15.0
WTForms==2.2.1
I successfully run this code both in my machine and in c9.io.
Get request for /login route (before submitting the form):
After submitting the form:
Issue solved!
I didn't spell submit correctly in forms.py
Simple clerical error that cost me 2 hours.

form field not showing (django 1.7)

The form field (text area) is not showing in my django template. I can figure out where the problem is.
Views.py
class Profile(View):
"""User Profile page reachable from /user/<username> URL"""
def get(self, request, username):
params = dict()
user = User.objects.get(username=username)
tweets = Tweet.objects.filter(user=user)
params["tweets"] = tweets
params["user"] = user
return render(request, 'profile.html', params)
class PostTweet(View):
"""Tweet Post form available on page /user/<username> URL"""
def post(self, request, username):
if request.method == 'GET':
form = TweettForm()
else:
form = TweetForm(self.request.POST)
if form.is_valid():
user = User.objects.get(username=username)
tweet = Tweet(text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country'])
tweet.save()
words = form.cleaned_data['text'].split(" ")
for word in words:
if word[0] == "#":
hashtag, created = HashTag.objects.get_or_create(name=word[1:])
hashtag.tweet.add(tweet)
return HttpResponseRedirect('/user/'+username)
return render(request, 'profile.html', {'form': form})
forms.py
from django import forms
class TweetForm(forms.Form):
text = forms.CharField(widget=forms.Textarea(attrs={'rows': 1, 'cols':85}), max_length=160)
country = forms.CharField(widget=forms.HiddenInput())
profile.html
{% extends "base.html" %}
{% block content %}
<div class="row clearfix">
<div class="col-md-12 column">
<form method="post" action="post/">{% csrf_token %}
<div class="col-md-8 col-md-offset-2 fieldWrapper">
{{ form.text.errors }}
{{ form.text }}
</div>
{{ form.country.as_hidden }}
<div>
<input type="submit" value="post">
</div>
</form>
</div>
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from tweets.views import Index, Profile, PostTweet
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', Index.as_view()),
url(r'^user/(\w+)/$', Profile.as_view()),
url(r'^admin/', include(admin.site.urls)),
url(r'^user/(\w+)/post/$', PostTweet.as_view())
)
Only the submit (post) button shows on the on when rendered in the browser. The text are is not there
You get nothing since you are not passing the form to the template. Write get function in PostTweet view and include form = TweetForm() in it as a param passed to the template.

Django and the POST request: unexpected behaviour with a form

urls.py
from django.conf.urls.defaults import patterns, include, url
import myproject.views
urlpatterns = patterns('', (r'^$', myproject.views.home), (r'^login$', apolla.views.login))
views.py
import django.http
import django.template
import django.shortcuts
def home(request):
return django.http.HttpResponse("Welcome home!")
def login(request):
un = request.POST.get('username')
pa = request.POST.get('password')
di = {'unam': un, 'pass': pa}
if un and pa:
di['act'] = "/"
else:
di['act'] = "/login"
return django.shortcuts.render_to_response('login.html', di,
context_instance=django.template.RequestContext(request))
# Why does this code not send me immediately to "/" with
# username and password filled in?
login.html
<html>
<head>
</head>
<body>
<form name="input" method="post" action="{{ act }}">
{% csrf_token %}
Username:
<input type="text" name="username"><br>
Password:
<input type="password" name="password"><br>
<input id="su" type="submit" value="Submit"><br>
</form>
</body>
</html>
When I run the development server and go to localhost:8000/login and fill in a username and password and push the submit button I am not sent to localhost:8000/ as I expected from my login function in views.py, I just return to localhost:8000/login. But when I fill in any field and submit for the second time I get directed to localhost:8000.
I also used print un and print pa to see if the post caught the data from the username and password fields and it did from the first time, so why am I not being directed to localhost:8000/login from the first submit with both username and password fields filled in?
You can add redirects to your view by:
from django.http import HttpResponseRedirect
def foo_view(request):
# ...
return HttpResponseRedirect('/')