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.
Related
So I am trying to basically have a check box on an image page which lets me set a boolean field true/false if I want this image to be deemed the "profile image." The problem is that the form field options are not showing up in the template. Any suggestions?
single_image.html
<form method='POST' action="{% url 'set_profile_image' plant_pk=plnt.pk image_pk=img.pk %}">
{% csrf_token %}
<hr>
{{ form.as_p }}
<hr>
<input type="submit" value="Submit">
forms.py
class SetProfileImageForm(forms.ModelForm):
"""A form to set profile image."""
image_main = forms.BooleanField(required=True)
class Meta:
model = Image
fields = ["image_main","image_url",]
views.py
class SingleImageView(DetailView):
""" A view to see a single image."""
template_name = "project/single_image.html"
queryset = Image.objects.all()
def get_context_data(self, **kwargs):
"""Return a dictionary with context data for this template to use."""
# get the default context data:
# this will include the Profile record for this page view
context = super(SingleImageView, self).get_context_data(**kwargs)
img = Image.objects.get(pk=self.kwargs['image_pk'])
plnt = Image.objects.get(pk=self.kwargs['image_pk']).plant
context['img'] = img
context['plnt'] = plnt
form = SetProfileImageForm()
context['set_profile_image'] = form
# return the context dictionary
return context
def get_object(self):
"""Returns the Note Object that should be deleted."""
# read the URL data values into variables
plant_pk = self.kwargs['plant_pk']
image_pk = self.kwargs['image_pk']
# find the StatusMessage object, and return it
return Image.objects.get(pk=image_pk)
def set_profile_image(request, plant_pk, image_pk):
"""A custom view function to set profile image."""
# find the plant for whom we are setting the image
plant = Plant.objects.get(pk=plant_pk)
if request.method == 'POST':
if "cancel" in request.POST:
return redirect('display_plant', pk=plant.pk)
form = SetProfileImageForm(request.POST or None, request.FILES or None)
if form.is_valid():
plant.set_profile_image(image_pk)
return redirect('display_plant', pk=plant.pk)
else:
print("Error: the form was not valid.")
else:
return reverse('gallery', kwargs={'pk':plant.pk})
You are sending the form instance as a set_profile_imgkey in the context.Change it in your HTML or just rename the context key.
...
context['form'] = form
<form method='POST' action="{% url 'set_profile_image' plant_pk=plnt.pk image_pk=img.pk %}">
{% csrf_token %}
<hr>
{{ form.as_p }}
<hr>
<input type="submit" value="Submit">
OR
...
context['set_profile_image'] = form
<form method='POST' action="{% url 'set_profile_image' plant_pk=plnt.pk image_pk=img.pk %}">
{% csrf_token %}
<hr>
{{ set_profile_image.as_p }}
<hr>
<input type="submit" value="Submit">
as the questions states i receive two errors in my template. Here is the code
def create(request):
full_content = forms.InputForm()
if request.method == "POST":
full_content = forms.InputForm(request.POST)
if full_content.is_valid():
title = full_content.cleaned_data["title"]
content = full_content.cleaned_data["content"]
if full_content.clean_title():#Works
full_content.create(title, content)
context= {
'title' : util.get_page_name(title),
'entry' : util.get_entry(title),
}
return render(request, "encyclopedia/entry.html",context)
#From here on its not valid:
context = {
'form':full_content
}
return render(request, "encyclopedia/create.html", context)
return render(request, "encyclopedia/create.html", {
'form':full_content
})
And the forms.clean_title():
def clean_title(self):
title_name = self.cleaned_data.get("title")
filename = f'entries/{title_name}.md'
if default_storage.exists(filename):
raise ValidationError("This title is already taken")
return title_name
Ofcourse the create.html aswell:
<h3>Create new entry</h3>
<form action="{% url 'create'%}" method="POST">
{{ form.title.errors }}
{% csrf_token %}
<table>
{{form.as_p}}
</table>
<button type="submit" value="save">Save</button>
</form>
Any ideas why i get two bullets?:
This title is already taken
This title is already taken
No need {{ form.title.errors }}. {{form.as_p}} is also show errors. So remove it.
<h3>Create new entry</h3>
<form action="{% url 'create'%}" method="POST">
{% csrf_token %}
<table>
{{form.as_p}}
</table>
<button type="submit" value="save">Save</button>
</form>
I'm trying to implement both searching posts and create a post in single view.
views.py
class HomeView(TemplateView):
template_name = 'home/home.html'
def get(self, request):
post_list = None
form = HomeForm(self.request.GET or None)
form1 = CommentForm(self.request.GET or None)
posts = Post.objects.filter(user = request.user).order_by('-created')
comments = Comment.objects.all()
users = User.objects.exclude(id=request.user.id)
query = request.GET.get('q')
if query:
post_list = Post.objects.filter(
Q(post__icontains=query)
)
context = {
'posts_list': post_list, }
print(post_list)
args = {
'form': form, 'posts': posts, 'users': users, 'form1':form1,
'comments':comments,'post_list':post_list,
}
return render(request, self.template_name, args)
def post(self, request):
form1 = CommentForm()
text = ''
if request.method=='GET' and 'btn1' in request.POST:
post_list = Post.published.all()
query = request.GET.get('q')
if query:
post_list = Post.objects.filter(
Q(post__icontains=query)
)
context = {
'posts_list': posts_list,
}
return redirect('home:home')
if request.method=='POST' and 'btn' in request.POST:
form = HomeForm(request.POST,request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
text = form.cleaned_data['post']
form = HomeForm()
form1 = CommentForm()
return redirect('home:home')
html code
<form enctype="multipart/form-data" class="form-inline my-2 my-lg-0" action=".">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name='q'>
<button class="btn btn-outline-success my-2 my-sm-0" name="btn1" type="submit">Search</button>
{% block body %}
<div class="container">
<div class="col-md-8">
<h2>Home</h2>
<form method="POST" enctype="multipart/form-data" type = 'file'>
{% csrf_token %}
{{ form.post }}
{{ form.image }}
<br>
<button name="btn" type="submit">Submit</button>
</form>
<h2>{{ text }}</h2>
{% for post in posts %}
<h1>{{post.id}}.{{ post.post }}</h1>
<br>
<img src="{{ post.image.url }}" width = 240 >
<p>Posted by {{ post.user.get_full_name }} on {{ post.created }}</p>
<!-- <form action="{% url 'home:cmnt' pk=post.id %}" method="POST" enctype="multipart/form-data" type = 'file'>
{% csrf_token %}
{{ form1.content }}
<br>
<button type="submit">Submit</button>
</form> -->
{{comments.count}} comment{{comments|pluralize}}<br>
{% for comment in post.comment_set.all %}
<small><b>{{ comment.comment }}</b></small>
<p>commented by {{ comment.user.get_full_name }} on {{ comment.created }}</p>
{% endfor %}
{% endfor %}
</div>
</div>
{% endblock %}
from this code I get the query value entered by the user.
when I implement this code I need to fill both the forms post create and search form but, I need to submit only one form at a time
when I fill both the forms and submit I get URL like this
http://127.0.0.1:8000/home/?q=django+&csrfmiddlewaretoken=OsNRlkvRjSHHuNYMxhZS9MFushCzCTZtjFvgEb5qFFybovBhWI3X0uufvd8bO8WS&post=asdsad&image=7653_1792089421024273_590924154993413545_n.jpg&btn=
I need a code which implements:
when submitted post create form create the post and return
when submitted search form load the respective results from 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.
I am trying to write a condition where if 1st part is ok then 2nd part will show.I wrote my forms.py in a following way :
class SubmissionForm(forms.Form):
first_name=forms.CharField(max_length=120, required=True)
last_name=forms.CharField(max_length=120, required=True)
class UploadForm(forms.Form):
file1 = forms.Field(label='File1', widget = forms.FileInput,required = True )
file2 = forms.Field(label='File1', widget = forms.FileInput, required = True )
In my views.py:
def submission(request):
validform=False
title="Please submit your data"
form = SubmissionForm(request.POST)
contextsumission={"title":title, "form":form}
if form.is_valid():
first_name = form.cleaned_data.get("first_name")
last_name = form.cleaned_data.get("last_name")
validform=True
if validform:
contextupload={"title":"Please upload your files","valid":True}
return render(request, 'submission.html', contextupload)
form2 = UploadForm(request.FILES)
contextsumission={"title":'Upload files', "form2":form2}
if form2.is_valid():
file1 = form2.request.FILES.get("file1")
file2 = form2.request.FILES.get("file2")
contextsumission={"title":"Thank you for your data submission"}
return render(request, 'submission.html', contextsumission)
And I have tried to write my template in a following way
{% if valid %}
<h1 class ='text-align-center', style="color: blue;"> {{ title }} </h1>
<form method ='POST' enctype="multipart/form-data" action =''> {% csrf_token %}
{{form2|crispy}}
<input class='btn btn-primary', type='submit', value='Upload'>
</form>
{% else %}
<h1 class ='text-align-center', style="color: blue;"> {{ title }} </h1>
<form method ='POST' enctype="multipart/form-data" action =''> {% csrf_token %}
{{form|crispy}}
<input class='btn btn-primary', type='submit', value='Submit'>
</form>
{% endif %}
I can see my SubmissionForm clearly in template but after pressing submit button I want to show file upload form but it is not working.
Anyone can please tell me where I need to change my code?
Thanks
Paul
Yes I have realized that and modified my code but still I have problem and def submission function is not validating two forms!! That means when ever I am trying to do form2.is_valid(), is returning false.
def submission(request):
title="Please submit your data"
form = SubmissionForm(request.POST)
contextsubmission={"title":title, "form":form}
if form.is_valid():
first_name = form.cleaned_data.get("first_name")
last_name = form.cleaned_data.get("last_name")
form2 = UploadForm(request.FILES)
contextsubmission ={"title":"Please upload your files","form2":form2,"valid":True}
if form2.is_valid():
file1 = form2.request.FILES.get("file1")
file2 = form2.request.FILES.get("file2")
contextbsumission={"title":"Thank you for your data submission"}
return render(request, 'submission.html', contextsubmission)