I have a django html template which has this form in it
<form action="/image_info/" method="POST" id='image_form'>
{% csrf_token %}
<button class="imginfo" name='info_button' value={{x.desc}} type="submit">info</button>
</form>
The above form will POST the value to views.py whenever the submit button is pressed. but the problem is {{x.desc}} is a sentence and has blank spaces in it so only the first word is getting posted. I need the whole sentence. How should I do it. here x is a model and desc is its object.
Thanks you.
You just need to add quotes to your value attribute
<button class="imginfo" name='info_button' value="{{x.desc}}" type="submit">info</button>
<form action="/image_info/" method="POST" id='image_form'>
{% csrf_token %}
<input type="hidden" value="{{ x.desc }}">
<button class="imginfo" name='info_button' type="submit">info</button>
</form>
use hidden input
Related
I have the following abbreviated HTML for an intermediate Django admin page:
<!DOCTYPE html>
{% extends base_url %}
{% block content %}
<form action="" method="post">
...
<select name="my_name" id="my_id" required>
<option disabled selected> --- </option> <br />
...
</select>
...
<input type="hidden" name="action" value="my_action" />
<input type="submit" name="apply" value="Update" />
</form>
{% endblock %}
However, my required attribute does not seem to work, and clicking "Update" submits the form even if no selection has been made.
Am I missing something special about how Django builds intermediate pages?
Happy to provide more code if needed, just removed most of it and replaced with ... for brevity's sake.
Edit: I was able to sidestep the issue by raising an error message if my field wasn't filled out, but that seems messier since it kicks the user back to the Admin page each time:
is_valid_form = 'my_id' in request.POST
if not is_valid_form:
fail_message = (
"Error: All fields are required.")
admin_page.message_user(
request, fail_message, messages.ERROR)
return HttpResponseRedirect(request.get_full_path())
The project have url as follows,
path('post/<str:state>/',SearchView.as_view(),name='search-data')
I have a HTML form, upon filling and submitting it supposed to pass filled form data to URL.
<form action={% url 'search-data'%} method="get" >
{% csrf_token %}
<input type="text" name="fname">
But it does not work as supposed to be.
When form submitted it gives below URL
http://127.0.0.1:8000/%7Burl?csrfmiddlewaretoken=2RZfZ4cxLB...
You don't have to pass <str:state> argument in your urlpatterns just pass path('post/search',SearchView.as_view(),name='search-data') or whatever you want but problem is when you pass an argument like this post/<str:state>/ than you have to specify that in your form action also
like this {% url 'search-data' state %} initialy you don't have any state so that's why you have to get the state name from your form so finnaly your code look like this
<form action={% url 'search-data'%} method="get" >
{% csrf_token %}
<input type="text" name="fname">
<input type="submit" value="search">
</form>
and than in your views you've to get it from request.GET method like this
def search(request):
state = request.GET.get('fname', None)
.... do whatever you want
return response_or_data
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input class="button" type="submit" value="Submit">
</form>
What will happen after I click the Submit button?
Syntax
<form action="URL">
the URL shows Where to send the form-data when the form is submitted.
Example
On submit, send the form-data to a file named "process_input.php" (to process the input):
<form action="/process_input.php" method="get">
<input type="text" id="name" name="name">
</form>
read more: https://www.w3schools.com/tags/att_form_action.asp
My problem is i set a form from a model to change the value of the field "description" of this model :
Model :
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
birth_date = models.DateField(null=True, blank=True)
profile_img = models.ForeignKey(Image,on_delete=models.CASCADE,related_name='images',null=True)
description = models.TextField()
Form :
class ChangeUserDescription(ModelForm):
class Meta:
model = Profile
fields = ['description']
widgets = {
'description': forms.Textarea()
}
labels = {
'description':'Description'
}
template :
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Save changes</button>
</form>
But in result of this code I obtain this :
<input type="hidden" name="csrfmiddlewaretoken" value="brsd4oO0qhMw2K8PyCIgSgEMqy7QFvEjTHaR6wTJmyWffJaCX5XyOMDLrGldZ3ji">
<button type="submit">Save changes</button>
The issue is that i get : type="hidden" in the input whereas i want it to be visible and i do not specified in the widgets that it must be hidden.
The hidden input field that you are referring to is the following:
<input type="hidden" name="csrfmiddlewaretoken" value="brsd4oO0qhMw2K8PyCIgSgEMqy7QFvEjTHaR6wTJmyWffJaCX5XyOMDLrGldZ3ji">
This is Django's CSRF token, which you have correctly included in your form, and should always be an <input type="hidden".
You have not shown us your template code, but as long as you correctly pass the form variable to your template, the following code should work:
<form method="post">
{% csrf_token %}
Please enter your description here: {{ form.description }}
<button>Submit</button>
{% if not form %}
You have forgot to add the <tt>form</tt> variable to your template!
{% endif %}
</form>
using this for django model form, we cant create beautiful front end.
<form method="POST" action="">
{% csrf_token %}
<div class="esor">
Name: {{form.Name}}<br>
Class: {{form.Class}}<br>
Publisher: {{form.Publisher}}<br>
</div>
<input type="submit" value="submit">
</form>
Isn't there any ways so we can use html code like:
<form method="GET" action="#">
<input type="text" name="name" placeholder="Name of book">
</form>
instead of {{form.Name}} or {{form.as_p}}
Yes, you can get a beautiful interface that way, just pass the name you use in the form.py
Example:
forms.py
class NameForm(forms.ModelForm):
class Meta:
model = MyModel
fields = [
"whatever"
]
Html Template
<form method="POST">{% csrf_token %}
<input type="text" name="whatever" placeholder="Write whatever">
</form>
There are two approach I normally use:
Install widget tweaks, so you can customize your input easily:
{% load widget_tweaks %}
{{ form.Name|add_class:"css_class"|attr:"placeholder:Name of book" }}
Or 2, in your forms.py, set all the attributes you want for the field:
Name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Name of book'}))
You can do this by giving the name of your input element same as the name of your form field. And you can get name of your form field by displaying {{ form }} in html and then inspecting the form field in your browser and then the name attribute of that field will be name of the form field.