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()
Related
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.
I am trying to add and subtract 1 from the value of a Django model's IntegerField and display it on the webpage, depending on when a button is clicked.
models.py
class User(AbstractUser):
following = models.ManyToManyField("self", related_name="followers", symmetrical=False)
following_num = models.IntegerField(default=0)
In views.py:
To add: following_num = page_visitor.following_num +1
To subtract: following_num = page_visitor.following_num -1
This is displaying in the html where the number should be displaying:
<django.db.models.query_utils.DeferredAttribute object at 0x0000022868D735E0>
entire view:
def username(request, user):
#get user
user = get_object_or_404(User.objects, username=user)
posts = Post.objects.filter(user=user).order_by('-date_and_time')
page_visitor = get_object_or_404(User.objects, username=request.user)
if user == request.user:
followButton = False
else:
followButton = True
if request.method == "POST":
if "follow" in request.POST:
request.user.following.add(user)
following_num = page_visitor.following_num +1
#following_num = F('User.following_num') + 1
elif "unfollow" in request.POST:
request.user.following.remove(user)
following_num = page_visitor.following_num -1
#following_num = F('User.following_num') - 1
followers_num = page_visitor.following_num
following_num = User.following_num
return render(request, "network/username.html",{
"user": user,
"posts": posts,
"followButton": followButton,
"followers": followers_num,
"following": following_num
})
html
{% block body %}
<h1>{{ user }}</h1>
<h4>Followers:{{ followers }}</h4>
<h4>Following:{{ following }}</h4>
<!--follow/unfollow button-->
{% if followButton == True %}
<form action = "{% url 'username' user %}" method = "POST">
{% csrf_token %}
{% if user not in request.user.following.all %}
<input type="submit" value="Follow" name="follow">
{% else %}
<input type="submit" value="Unfollow" name="unfollow">
{% endif %}
</form>
{% endif %}
<!--displays all the posts-->
{% for post in posts %}
<div class = "individual_posts">
<h5 class = "post_user">{{ post.user }}</h5>
<h6 id = "post_itself">{{ post.post }}</h6>
<h6 class = "post_elements">{{ post.date_and_time }}</h6>
<h6 class = "post_elements">{{ post.likes }} Likes</h6>
</div>
{% endfor %}
{% endblock %}
you reference the class (model) and not the object (instance)
following_num = User.following_num
as you alredy passed your user object to the template you can also access the attributes directly
{{user.following_num }} {{request.user.following_num }}
but you better rename the passed user variable to avoid confusion / errors
Get user information in django templates
Without the html that is supposed to display the number I cant tell why it is displaying that, but if you intend to change the following count then you need to call page_visitor.save() after changing a property of page_visitor in order for it to save the new property's value in the database
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.
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.
I'm creating a page where my users can edit their articles but there is one problem, my form is not saving the modifications when I submit my form. Let's have a look:
views.py
def edit(request, id=None, template_name='article_edit.html'):
if id:
article = get_object_or_404(Article, id=id)
if article.user != request.user:
return HttpResponseForbidden()
else:
article = Article(user=request.user)
if request.POST:
form = ArticleForm(request.POST, instance=article)
if form.is_valid():
save_it = form.save()
save_it.save()
form.save_m2m()
return HttpResponseRedirect("/")
else:
form = ArticleForm(instance=article)
context = {'form': form}
populateContext(request, context)
return render(request, template_name, context)
line 3 to 8 : Is to check if it's your own articles there is no problem with that.
line 10 to 18 : There is a problem between these lines, my form is not saving when I submit my form
forms.py (ModelForm)
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
exclude = ['date', 'rating', 'user']
form = ArticleForm()
(Line 4 : I'm excluding the date, the ratings and the username when saved so that the user can not change these informations.)
I don't have any problem in my template.html and the urls.py don't bother with that I checked over 10 times :)
Any help would be great, I'm blocked on this problem since over 1 month...
******* EDIT *******
Models.py
class Article(models.Model):
user = models.ForeignKey(User)
titre = models.CharField(max_length=100, unique=True)
summary = RichTextField(null=True, max_length=140)
contenu = RichTextField(null=True)
date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de parution")
image = models.ImageField(upload_to='article', default='article/amazarashi.jpeg')
rating = RatingField(can_change_vote=True)
tags = TaggableManager(through=TaggedItem, blank=True)
def __str__(self):
return self.titre
Template.html
{% block body %}
{% if user.is_authenticated %}
<p>Authentificated as <strong>{{ user.username }}</strong></p>
{% else %}
<p>NOT Authentificated</p>
{% endif %}
<h1>Edit this {{ article.titre }}</h1>
<br>
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-8">
<form enctype='multipart/form-data' action="{% url "article.views.index" %}" method="post" class="form" autocomplete="off" autocorrect="off">
{% csrf_token %}
<div class="form-group">TITRE
{{ form.titre.errors }}
{{ form.titre }}
</div>
<div class="form-group">SUMMARY
{{ form.media }}
{{ form.summary.errors }}
{{ form.summary }}
</div>
<div class="form-group">CONTENU
{{ form.media }}
{{ form.contenu.errors }}
{{ form.contenu }}
</div>
<div class="form-group">
{{ form.image.errors }}
{{ form.image }}
</div>
<div class="form-group">TAGS
{{ form.tags.errors }}
{{ form.tags }}
</div>
<input type="submit" class="btn btn-default" value="Submit"/>
</div>
</form>
{% endblock %}
According to your error message.. Modify your form in templates:
<form enctype='multipart/form-data' ...>
Add request.FILES to your Form creation:
if request.POST:
form = ArticleForm(request.POST, request.FILES, instance=article)
UPDATE:
You have strange url tag parameter. Are you sure you have defined your url like: ('...', your_view, name='article.views.index')? I am in doubt cause url tag takes url name as parameter but not view path and nobody usually uses url names like this.