How can I set values for each HiddenInput fields of Django? - python

I wrote codes, but I don't know how to set 'name' and 'value' of hidden tag with Django template. I read Django's Widgets Docs, but I couldn't find the way.
(Pdb) print(errors)
<ul class="errorlist"><li>friend_id<ul class="errorlist"><li>This field is required.</li></ul></li><li>add_remove<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
First, I tried to write like
<input type="hidden" name="friend_id" value="{{ user_info.user_id }}">
and
friend_id = request.POST.friend_id
But I couldn't get how to get POST values without Django's Form. So, I used Django's Form with following codes.
views.py
from myapp.forms import HiddenUserPage
hiddenform = HiddenUserPage
if request.method == 'POST':
hidden = hiddenform(request.POST)
if hidden.is_valid():
from myapp.models import Friends
try:
friend_id = hidden.cleaned_data['friend_id']
add_remove = hidden.cleaned_data['add_remove']
if add_remove == "add":
f = Friends(user_id=request.user.user_id, friend_id=friend_id)
f.save()
elif add_remove == "remove":
f = Friends.objects.filter(user_id=request.user.user_id).get(friend_id=friend_id)
f.delete()
except:
errors = "DB error"
else:
errors = hidden.errors
else:
hidden = hiddenform()
errors = ""
view = {
'errors': errors,
'hidden': hidden,
}
template = 'myapp/user/user_page.html'
return render(request, template, view)
forms.py
class HiddenUserPage(forms.Form):
friend_id = forms.CharField(widget=forms.HiddenInput())
add_remove = forms.CharField(widget=forms.HiddenInput())
user_page.html
<form method="POST" action="" class="">
{% csrf_token %}
<p class="submit">
<button class="confirmbutton" type="submit">
{% if is_friend %}
remove friend
<!-- # I'd like to write like # -->
<!-- <input type="hidden" name="friend_id" value="remove"> # -->
<!-- <input type="hidden" name="friend_id" value="{{ user_info.user_id }}"> # -->
{{ hidden.add_remove }}
{{ hidden.friend_id }}
{% else %}
add friend
<!-- <input type="hidden" name="friend_id" value="add"> # -->
<!-- <input type="hidden" name="friend_id" value="{{ user_info.user_id }}"> # -->
{{ hidden.add_remove }}
{{ hidden.friend_id }}
{% endif %}
</button>
</p>
</form>
Sorry, my code is filthy.

Looks like the question is in providing initial data to the form, then it's is generally done in the view passing initial to the form instantiation, e.g.:
# In your view.py
def ...(...):
# Inside your view function
if request.method == 'GET':
# Provide initial data to the form here
# Get your 'user_info' from models or sessions,
# or wherever you keep it
hidden = hiddenform(initial={"friend_id":user_info.user_id})
if reuest.method == 'POST':
hidden = hiddenform(request.POST)
# Process posted form data
...
# More code general for both HTTP verbs
view = {'errors': errors, 'hidden': hidden}
template = 'myapp/user/user_page.html'
return render(request, template, view)
You might also want to bound the form to model data directly, see the docs for more info.

Related

How do I get the current item in the model from the request?

When a submit button is clicked, I want to be able to know what item the user was on. The button would be on an item's page that the user gets taken to when they click on an item. This button is is part of a django form (PlaceBid) that allows the user to bid for the item. So I want to be able to update what the item's highest bid is if the bid is higher than the current highest. This means that I need to know what item the user was viewing.
I also want to save the bid in a model called Bid and in that model I also need to know what listing the bid is for.
So how do I get the correct item from the model?
The models:
Listing() is the model for the item
Bid() is the model for the bid
views.py:
def bid(request):
if request.method == 'POST':
form = PlaceBid(request.POST)
if form.is_valid():
current = Listing.objects.get(pk=request.id) # my attempt to get the current item
highest = current.highest_bid
if form.cleaned_data['bid'] < highest:
obj = Bid()
obj.price = form.cleaned_data['bid']
obj.item = current
obj.user = User.objects.get(pk=request.user.id)
obj.save()
current.highest_bid = form.cleaned_data['bid']
current.save()
return HttpResponseRedirect(request.path_info)
forms.py:
class PlaceBid(forms.Form):
bid = forms.FloatField(required=False, widget=forms.NumberInput(attrs={
'class': 'bid',
}))
html:
<form action=" {% url 'bid' %} " method="post">
{% csrf_token %}
{{ bidForm }}
<input type="submit" value="Place Bid" class="place-bid">
</form>
With the limited code shown, I can only assume at this point that you have a bidding form for each item in a forloop. Example:
{% for item in items %}
...
<form action=" {% url 'bid' %} " method="post">
{% csrf_token %}
{{ bidForm }}
<input type="submit" value="Place Bid" class="place-bid">
</form>
...
{% endfor %}
But here are two methods that can be done...
Use a hidden input field to hold the item object id then retrieve that field name on the server to get the item's id value.
# html
<form action=" {% url 'bid' %} " method="post">
{% csrf_token %}
{{ bidForm }}
<input type="hidden" value="{{ item.id }}" name="item_id">
<input type="submit" value="Place Bid" class="place-bid">
</form>
# views.py
def bid(request):
if request.method == 'POST':
form = PlaceBid(request.POST)
if form.is_valid():
item_id = request.POST.get('item_id', None)
current = Listing.objects.get(id=item_id)
# rest of code follows...
Pass the item's id via the url. (My recommendation)
# html
<form action=" {% url 'bid' item.id %} " method="post">
{% csrf_token %}
{{ bidForm }}
<input type="submit" value="Place Bid" class="place-bid">
</form>
# urls.py
# update the necessary url to accept an id
path('bid/<int:id>/', views.bid, name='bid')
# views.py
def bid(request, id):
if request.method == 'POST':
form = PlaceBid(request.POST)
if form.is_valid():
current = Listing.objects.get(id=id) # id passed to this method used here...
# rest of code follows...
Also, instead of using Listing.objects.get(id=id), I'd suggest using get_object_or_404(Listing, id=id) as this will handle any potential error that Listing.objects.get(id=id) will throw.
You won't get it from the request. Hide it in the form.
class PlaceBid(forms.Form):
bid = forms.FloatField(required=False, widget=forms.NumberInput(attrs={
'class': 'bid',
}))
listing_id = forms.IntegerField(widget=forms.HiddenInput())
Then in your method that displays the form (I'm using function-based views)
def view_listing(request, listing_id)
listing = Listing.objects.get(id=listing_id)
myForm = PlaceBid(initial={"listing_id" : listing_id})
return render("whatever.html", {"listing" : listing, "form" : form })
I suppose another option would be to stash the listing_id in the session, but I try to avoid that.

Django HTML Dropdown

I am trying to make a html dropdown and pass the values into Postgrase SQL database. My dropdown values are being retrieved from another database table. It gives me a MultiValueKeyDictError every time I submit the form. I know I can use forms.py to do the same thing but I want to explore the HTML way of doing this.
My HTML file
<form action = "" method = "post">
{% csrf_token %}
<label for = "LogType"></label>
<input id ="LogType" type = "text" value = "{{ user.department }}">
<label for ="DelayCategory">Delay Category</label>
<select id = "delaycategory" class = "form-control">
{%if user.department == 'TechAssembly'%}
{%for techdelay in techdelay%}
<option value = "{{ techdelay.DelayCode }}">{{ techdelay.DelayCategory}}</option>
{%endfor%}
{%endif%}
{%if user.department == 'Testing'%}
{%for testdelay in testdelay%}
<option value = "{{ testdelay.DelayCode }}">{{ testdelay.DelayCategory}}</option>
{%endfor%}
{%endif%}
</select>
<label for = "iterations">Iterations</label>
<input type = "number" id = "iterations">
<center><input type="submit" value=Submit id = "button"></center>
</form>
My Views.py file
def rulesView(request, user_name):
testdelay = TestingDelayCategory.objects.all()
techdelay = TechDelayCategory.objects.all()
if request.method == "POST":
rulesnew = rules()
rulesnew.DelayCategory = request.GET['DelayCategory']
rulesnew.LogType = request.POST('LogType')
rulesnew.iterations = request.POST('iterations')
rulesnew.save()
context = {
'techdelay':techdelay,
'testdelay':testdelay,
}
return render(request, 'rules/rules.html', context)
rulesnew.DelayCategory = request.GET['DelayCategory']
rulesnew.LogType = request.POST('LogType')
rulesnew.iterations = request.POST('iterations')
Have a second look at this: request.GET should be request.POST and request.POST('LogType') should be request.POST['LogType'] same with iterations.
The error message should include the exact line where the errors was raised. So it would have been way easier to debug if you have told us that the error was raised e.g. in this line rulesnew.LogType = request.POST('LogType')

Prepopulate WTForms radio field with info from view

Using flask and WTForms. I am trying to combine my edit and create forms for posts. When a user clicks edit, the view checks if there is a post id passed, if there is it grabs the current info for that post and populates the form in the template. I can get this to work for every field but the 'main_post' field which is radio buttons.
View:
#app.route('/posts-update' , methods=['POST','GET'])
#login_required
def posts_update():
form = forms.Post()
if request.method == 'POST' and request.form['id']:
post = Post.query.filter_by(id=request.form['id']).first()
form.title.data = post.title
form.body.data = post.body
# Works for all but main_post
form.main_post.data = post.main_post
# Also tried this and it didn't work
# form = forms.Post(obj=post)
else:
post = False
# form = forms.Post()
return render_template('post_update.html', post=post, form=form)
Form:
#post create and update
class Post(Form):
title = StringField('Title', validators=[DataRequired()])
body = TextAreaField('Body', validators=[DataRequired()])
main_post = RadioField('Make Main Post', choices=[('1', 'yes'), ('0', 'no')], validators=[DataRequired()])
Template:
<input type="hidden" name="id" {% if post.id %}value="{{ post.id }}"{% endif %}>
{{ form.title.label }} : {{ form.title }} <br/>
{{ form.body.label }} : {{ form.body }} <br/>
{{ form.main_post.label }} : {{ form.main_post }} <br/>
<button class="button postfix" type="submit">{% if post == False %}Add{% else %}Update{% endif %}</button>
A similar question was asked and answered here How to dynamically set default value in WTForms RadioField?
You should be able to do this by setting the default value and then running form.process().
form.main_post.default = post.main_post
form.process()

only raise validation error after submitting in Django forms

After creating a form and setting required=True the form shows the validation errors immediately when loading the page.
Of course this should only happen after submitting.
How would I be able to make sure the proper errors only show after submitting?
forms.py
class CurrencyConverterForm(forms.Form):
base_currency = forms.ModelChoiceField(queryset=Currency.objects.all(), required=True)
counter_currency = forms.ModelChoiceField(queryset=Currency.objects.all(), required=True)
base_amount = forms.FloatField(required=True)
index.html
<form action="" method="get">
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.base_currency.errors }}
<label for="{{ form.base_currency.id_for_label }}">From Currency</label>
{{ form.base_currency }}
</div>
<div class="fieldWrapper">
{{ form.counter_currency.errors }}
<label for="{{ form.counter_currency.id_for_label }}">To Currency</label>
{{ form.counter_currency }}
</div>
<div class="fieldWrapper">
{{ form.base_amount.errors }}
<label for="{{ form.base_amount.id_for_label }}">Amount</label>
{{ form.base_amount }}
</div>
</form>
views.py
def index(request):
counter_amount = ""
if request.method == 'GET':
form = CurrencyConverterForm(request.GET)
if form.is_valid():
# Get the input data from the form
base_currency = form.cleaned_data['base_currency']
counter_currency = form.cleaned_data['counter_currency']
base_amount = form.cleaned_data['base_amount']
# Calculate the counter_amount
counter_amount = get_conversion_amount(base_currency, counter_currency, datetime.now(), base_amount)
# Retrieve the counter amount from the dict
counter_amount = counter_amount['GetConversionAmountResult']
# Maximize the number of decimals to 4
if counter_amount.as_tuple().exponent < -4:
counter_amount = "%.4f" % counter_amount
else:
form = CurrencyConverterForm()
context = {
'form': form,
'counter_amount': counter_amount
}
return render(request, '../templates/client/index.html', context)
The problem is that both requests are GETs: both the initial request to get the form, and the request to submit the form. So there is no point in checking if request.method == 'GET', because it is always true.
Instead, check that there is actually information in the GET dictionary:
if request.GET:
Note that this will not work if you need to show an error on a completely empty submission, though.

Django form showing as hidden

I'm doing some work with forms and I believe I did something wrong, my forms arent showing now, I'm doing 4 different pages with a different form on each one.
When I open the page and check the source of the page, the input of my form appears as
<form role="form" method = 'post'><input type='hidden' name='csrfmiddlewaretoken' value='4VlAtXgpblSSq5tkugStVWKWYtZ6rd8A' />
<input type='submit'>
</form>
type = 'hidden' as you can see. Theseare my views:
def add_venta(request):
ventaForm = VentaForm(request.POST or None)
if ventaForm.is_valid():
save_it = ventaForm.save(commit=False)
save_it.save()
return redirect("/venta/")
return render_to_response("venta.html",locals(),context_instace=RequestContext(request))
def add_compra(request):
rotate_token(request)
compraForm = CompraForm(request.POST or None)
if compraForm.is_valid():
save_it = compraForm.save(commit=False)
save_it.save()
return redirect("/compra/")
return render_to_response("compra.html",locals(),context_instance=RequestContext(request))
def add_accion(request):
accionForm = AccionForm(request.POST or None)
if accionForm.is_valid():
save_it = accionForm.save(commit=False)
save_it.save()
return redirect("/accion/")
return render_to_response("accion.html",locals(),context_instance=RequestContext(request))
def add_banco(request):
bancoForm = BancoForm(request.POST or None)
if bancoForm.is_valid():
save_it= bancoForm.save(commit=False)
save_it.save()
return redirect("/banco/")
return render_to_response("banco.html",locals(), context_instance=RequestContext(request))
the .html files for each view
banco.html
<form role="form" method = 'post'>{% csrf_token %}
{{ banco.as_p }}
<input type='submit'>
compra.html
<form role="form" method = 'post'>{% csrf_token %}
{{ compra.as_p }}
<input type='submit'>
venta.html
<form role="form" method = 'post'>{% csrf_token %}
{{ venta.as_p }}
<input type='submit'>
accion.html
<form role="form" method = 'post'>{% csrf_token %}
{{ accion.as_p }}
<input type='submit'>
You need to use the same variable name in the template as in the view.
For example, in the view you have
ventaForm = VentaForm(request.POST or None)
so in the template you should use
{{ ventaForm.as_p }}
The hidden input in your question is from the csrf token. This is a security feature, and is meant to be hidden.

Categories

Resources