I need a call a form on an HTML template where the user posts data which saves to model
The code is running without any errors
But the html page display only title and button
No text input fields
I have a form which is to be displayed on a html page so the user can input data and it saves the data into the model.I am not getting any errors while executing th code but the template does not display the form it just shows the title and submit button
def boqmodel1(request):
form = boqform(request.POST)
if form.is_valid():
obj=form.save(commit=False)
obj.save()
context = {'form': form}
return render(request, 'create.html', context)
else:
context = {'error': 'The post has been successfully created.
Please enter boq'}
return render(request, 'create.html', context)
MyTemplate
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create boq"/>
</form>
MY Url
urlpatterns = [
url(r'^create/', views.boqmodel1, name='boqmodel1'),
path('', views.boq, name='boq'),
]
First of all, your first request, without submitting form is GET. When you submit a form you send POST.
The form is not displaying, because your form is not valid in the first place. Your function should look like this:
def boqmodel1(request):
context = {}
if request.method == "GET":
form = boqform()
context["form"] = form
# if you post a form do all the saving
if request.method == "POST":
form = boqform(request.POST)
context = {'form': form}
if form.is_valid():
obj=form.save()
return render(request, 'create.html', context)
else:
context["errors"] = form.errors
return render(request, 'create.html', context)
If method is GET init your form and pass it to your context, so you can display it on frontend.
If method is POST, init your form with your data from frontend (request.POST), check if the form is valid. If it is valid - save it. If it is not valid, return your errors and display them as you wish.
Related
I mading a project and i need to get checkbox values in sequence, but django do not return anything when that checkbox are unchecked.
how can i do that return False instead of nothing?
forms.py
class myFormExemple(forms.Form):
checkbox = forms.BooleanField(required=False)
views.py
def MyViewExemple(request):
if request.method == 'POST':
print(request.POST.getlist('checkbox'))
context = {
'form': myFormExemple
}
return render(request, "cadastro/myHTMLTemplate.html", context)
and myHTMLTemplate.html:
<form method="post" id='form'>
{% csrf_token %}
{{form.checkbox}}
<button type="submit">save</button>
</form>
If the checkbox is not checked, the POST request will indeed not contain the name of that checkbox, that is how the HTML form works.
You can however validate and clean the data with your form, and transform this into a boolean with:
def MyViewExemple(request):
if request.method == 'POST':
form = myFormExemple(request.POST)
if form.is_valid():
print(form.cleaned_data['checkbox'])
context = {
'form': myFormExemple()
}
return render(request, "cadastro/myHTMLTemplate.html", context)
We thus construct a form with request.POST as data source, and then if the form is valid, we can obtain a boolean with form.cleaned_data['checkbox'].
Django's forms are thus not only used to render a form, but also to process data of a form, validate that data, and convert the data to another type.
I'm entering a duplicate value (already saved in another instance of the same model) in my form to test the unique=True attribute. form.is_valid() returns 'False', as expected, but I don't receive any prompt in the template. Shouldn't I get prompted something like "obj with this value already exists"? The page simply reloads... What am I missing?
forms.py
def update_route(request, pk):
instance = Route.objects.get(id=pk)
if request.method == "POST":
form = RouteForm(request.POST)
if form.is_valid():
data = form.cleaned_data
instance.name = data['name']
instance.priority = data['priority']
instance.url = data['url']
return redirect('campaigns:routes_list')
form = RouteForm(instance=instance)
context= {
'form': form,
}
return render(request, "campaigns/route_form.html", context)
models.py
class Route(models.Model):
name = models.CharField(max_length=48)
priority = models.SmallIntegerField(choices=PRIORITY_LEVEL, default=0, unique=True)
url = models.URLField()
Template
<form method="post" action="">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit">
</form>
Your update_route() view handles the condition in which the submitted form is valid (form.is_valid()), but not the condition in which the form is invalid.
The errors you are looking for are stored in the form object that you created with RouteForm(request.POST). The errors are generated when the is_valid() method is called.
This form object needs to be added to the context dict and rerendered to the user for the errors to surface. But your code currently overwrites that object with form = RouteForm(instance=instance), so the POST data and the related errors disappear.
One solution could be to handle it in the conditional statement:
if form.is_valid():
...
else:
context = {'form': form}
return render(request, "campaigns/route_form.html", context)
Another solution could be to create a conditional statement for GET requests, for example:
elif request.method == 'GET':
form = RouteForm(instance=instance)
When I try to use a form to edit an object that includes an image upload I get "This field is required". A similar form works fine to create the object, but when I retrieve the object and attempt to modify other fields, it fails on the image.
#-------models.py:
class Star(models.Model):
firstname = models.CharField(max_length=32)
lastname = models.CharField(max_length=32, blank=True)
portrait = models.ImageField(upload_to='images/')
#------views.py:
class StarForm(forms.ModelForm):
class Meta:
model = Star
fields = ["firstname", "lastname", "portrait"]
def staredit(request, star_id):
instance = Star.objects.get(pk=star_id)
form = StarForm(request.POST or None, instance=instance)
context = {
"form": form,
}
return render(request, "stars/edit.html", context)
def starchange(request):
form = StarForm(request.POST, request.FILES)
if form.is_valid():
newstar.save()
context = {
"message": "The form was posted",
}
return render(request, "stars/edit.html", context)
else:
context = {
"message": form.errors,
}
return render(request, "stars/edit.html", context)
#-----edit.html
<form action="/starchange" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{{message}}
Error message:
portrait
This field is required.
You are not updating the instance, since you never have passed the instance to the view that should update it. When you make a POST requrest, the browser only submits the content of the form elements. There is no data about what has rendered the previous form, that data is lost.
You should specify the instance to update, so:
from django.shortcuts import get_object_or_404
def starchange(request, pk):
obj = get_object_or_404(Star, pk=pk)
form = StarForm(request.POST, request.FILES, instance=obj)
if form.is_valid():
form.save()
context = {
"message": "The form was posted",
}
return render(request, "stars/edit.html", context)
else:
context = {
"message": form.errors,
}
return render(request, "stars/edit.html", context)
in the urls, you thus should specify the primary key of the object to update:
urlpatterns = [
# …,
path('starchange/<int:pk>/', views.starchange, name='starchange')
]
and in the template, you should make a POST request to a view with the given instance:
<form action="{% url 'starchange' pk=form.instance.pk %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
This is one of the main reasons why often the same view is used both for a GET and POST request, since it makes it removes a lot of duplicate logic. Furthermore it is also more clean: you can use GET to retrieve the page, and POST to submit the page.
Note: In case of a successful POST request, you should make a redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.
Note: It is often better to use get_object_or_404(…) [Django-doc],
then to use .get(…) [Django-doc] directly. In case the object does not exists,
for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using
.get(…) will result in a HTTP 500 Server Error.
I have got a modal contact form. The form works fine, but the page reload resubmits previously entered values.
view.py
...
if request.POST:
form = ContactForm(request.POST)
if form.is_valid():
entry = form.save()
form = ContactForm()
c.update(locals())
return render(request, 'template.html', c )
...
template.html
<form action="." method="post" id="contact-form">
...
</form>
I tried to clear the form in the GET request. Form fields are cleared, but this does not prevent submission on the page reload.
else: #request.GET
form = ContactForm()
c.update(locals())
return render(request, 'template.html', c )
I have seen many examples suggesting a redirect. I do understand this approach, but in case of a modal form I would like to avoid a redirect.
How to prevent modal form re-submissions? Is the redirect really the only way?
Solution seems very simple:
Replaced this line...
return render(request, 'template.html', c )
... with the one below:
return HttpResponseRedirect(request.path_info)
I am working on a web page that takes a set of song titles and artist names, and finds the lyrics for the songs.
I have a minimal search page with a POST form and a submit button.
def search(request):
formset = modelformset_factory(Song, fields=('title', 'artist', 'song_pk'))
return render(request, 'lyricfind/search.html', {'formset': formset})
for the view, and
<form method="post" action="/results/">
{% csrf_token %}
{{ formset }}
<input type="submit" value="Submit">
</form>
for the template.
However, when I enter data in the text input fields and click the submit button, an error
The view lyricfind.views.results didn't return an HttpResponse object. It returned None instead.
is thrown.
I do not see what causes this error.
I wasn't sure about how I should write my view, so the view could be the cause.
def results(request):
SongFormSet = modelformset_factory(Song)
if request.method == 'POST':
formset = SongFormSet(request.POST)
if formset.is_valid():
return HttpResponseRedirect(reverse('lyricfind/results.html', {'formset': formset}))
return render(request, 'lyricfind/search.html', {'formset': formset})
The template, results.html, just contains some plain text.
I believe urls are properly configured
url(r'^', include('lyricfind.urls', namespace='lyricfind')),
and then in lyricfind.urls,
url(r'^$', views.search, name='search'),
url(r'^results/$', views.results, name='results'),
try changing code to this:
def results(request):
SongFormSet = modelformset_factory(Song)
if request.method == 'POST':
formset = SongFormSet(request.POST)
if formset.is_valid():
# SAVE or Process Data Then:
return redirect('/some-save-confirmation-result-page')
# or if your doing just a search, should use GET and can return a rendered template.
return render(request, 'lyricfind/search.html', {'formset': formset})