I need to set a placeholder/default value for the input field below (the first field in the form)
I need to send the data ex_name.id in order to validate the form, which it does, but I want the information displayed in the field to be ex_name.exercise.
I was able to accomplish this in the option fields (fields 2 and 3), but not for the input field. If I make the input field an option field, it doesn't send the data when I submit it because the field is readonly and it has to stay readonly. value must be ex_name.id/ex_name.pk, but I don't want to display a number to the end user. How to I display different text there while still retaining value="{{ex_name.id}}"?
<div class="form-group col-lg-4 col-md-6 col-sm-12">
<form action="" method="post">
{% csrf_token %}
<label for="exercise">Exercise</label>
<input class="form-control" id="id_exercise" type="text" name="exercise" value="{{ex_name.id}}" required="" readonly>
<label for="unit">Select Unit</label>
<select class="form-control" id="id_unit" name="unit" value="">
{% for unit in unit_list %}
<option value="{{unit.id}}">{{unit.unit_name}}</option>
{% endfor %}
</select>
<label for="location">Location</label>
<select class="form-control" id="id_location" name="location" value="">
{% for loc in locations %}
<option value="{{loc.id}}">{{loc.location}}</option>
{% endfor %}
</select>
<label for="quantity">Pax</label>
<input class="form-control" id="id_quantity" type="text" name="quantity" value="" required="">
<input class="btn btn-primary mt-3" type="submit" value="Submit">
</form>
</div>
Related
I am using DRF for CRUD operations. In my Project, I have function-based view taking request as parameter. I am aware of the fact that request.data is only applicable for DRF-Specific requests. I am also aware of the fact that it is a QueryDict.
I am taking input from HTML form as name, roll and city of a Student. But when I print the request.data (aka QueryDict), I only get csrfmiddlewaretoken and its value and found that other key-value pairs are missing. Where is my other data (i.e roll, name and city)
e.g {"csrfmiddlewaretoken": "JSACyyvAPqNcCPXNZQsNzPcca7ah3N7arkhFM9CuqpNammk6f43wQ4GyGg5QwU6w"}
forms.html (Template):
<form class="form" action="/save-data/" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" class="form-control" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="roll">Roll:</label>
<input type="number" id="roll" class="form-control" placeholder="Enter your Roll Number">
</div>
<div class="form-group">
<label for="city">City</label>
<input type="text" id="city" class="form-control" placeholder="Enter your City">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Views.py
#api_view(['POST', 'GET'])
def save_data(request):
requested_data = request.data
print(requested_data)
return HttpResponse("Success", content_type='application/json')
It was supposed to contain other data fields as well!
To make it clear I want do the following with just one line:
{%if model %}
<input type="text" class="form-control" id="title" value="{{model.title}}" placeholder="Enter Title">
{% else %}
<input type="text" class="form-control" id="title" value="" placeholder="Enter Title">
{% endif %}
I tried this:
<input type="text" class="form-control" id="title" value="{% model.title if model else "" %}" >
And it didn't work:
Invalid block tag on line 15
I don't think I have to make a custom template tag for this simple kinda things.
Thanks in advance
You may try the following:
<input type="text" class="form-control" id="title" value="{%if model %} {{ model.title }}{% endif %}" placeholder="Enter Title">
please anyone can help me ? How can I convert a field string to list ? I have a field called Size, it means that the field contains somthing as : "XXL, XL, X, SM".
So I want to get something as :
<form class="form-control" action="index.html" method="post">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="taille">
<label class="custom-control-label" for="taille">SM</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="size">
<label class="custom-control-label" for="size">XXl</label>
</div>
<div class="customenter code here-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="size">
<label class="custom-control-label" for="size">XL</label>
</div>
</form>
What's your recommandations guys ?
Thank you guys, I already got the answer. I solved it by creating a table with the field article_size in models.py file.
Here is the working code:
<form class="form-control" action="index.html" method="post">
{% for size in article.size_article.all %}
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="taille{{size.article_size}}">
<label class="custom-control-label">{{size.article_size}}</label>
</div>
{% endfor %}
</form>
I'm currently trying to override a Django allauth template. So far, I followed all the recommendations on similar questions to mine:
overriding default templates of django-allauth
How to override template in django-allauth?
However, my goal is just change the default Django allauth without making new views or forms. (In previous projects I used to override the default forms constructor classes with the helper of Django Crispy Forms which is not possible in this particular case.)
I believe there is a way to ignore the {{form}} variable and just define a working form from scratch. Currently, I have my template being successfully being displayed when I navigate to 'account_login'. However, when I press Sign In nothing happens.
Here is my myapp/templates/account/login.html file:
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}
Sign In to {{ SITE_NAME }}
{% endblock %}
{% block body %}
<div class="centered">
<img class="logo" src={% static 'imgs/logo.png' %} alt="" width="100" height="100">
<form id="login_form" class="form-signin" method="post" action="{% url 'account_login'%}">
{% csrf_token %}
<label for="inputLogin" class="sr-only">Email address or Username</label>
<input type="text" id="login" class="form-control" placeholder="Email or Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="password" class="form-control" placeholder="Password" required>
<div class="note mb-3">
<label>
<input type="checkbox" value="remember" id="remember"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<div class="note mb-3 text-center">
<p>Forgot? Retrieve your login.</p>
<p>Not registered? <a href={% url 'account_signup'%}>Sign Up!</a></p>
</div>
</form>
</div>
{% endblock %}
Yes, you can customize it the way you want. It is rather simple.
When you use {{ form.as_p }} the page renders the form. And if you inspect the form you will notice that the page rendered the following form:
notice the id="" and the name="" you have to name it the exact same.
With that your code should look like this:
<form id="login_form" class="form-signin" method="post" action="{% url 'account_login'%}">
{% csrf_token %}
<label for="inputLogin" class="sr-only">Email address or Username</label>
<input type="text" name="login" id="id_login" class="form-control" placeholder="Email or Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="id_password" class="form-control" placeholder="Password" required>
<div class="note mb-3">
<label>
<input type="checkbox" name="remember" value="remember" id="id_remember"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<div class="note mb-3 text-center">
<p>Forgot? Retrieve your login.</p>
<p>Not registered? <a href={% url 'account_signup'%}>Sign Up!</a></p>
</div>
</form>
And you should be good to go.
So, I have this in my template:
<form action="./validate_code/" method="POST">
{% if error %}
<p>Sorry, that wasn't right.</p>
{% endif %}
<label for="class_code">Your class code: </label>
<input type='text' id='class_code'/>
<input type="color" id="color"/>
{% csrf_token %}
<input type="submit" value="submit">
</form>
Which compiles to:
<form action="./validate_code/" method="POST">
<label for="class_code">Your class code: </label>
<input type='text' id='class_code'/>
<input type="color" id="color"/>
<input type='hidden' name='csrfmiddlewaretoken' value='tGA4jKF1pd1QFC6NSAM9eNFvZqss0r4m' />
<input type="submit" value="submit">
</form>
And when I click submit, firefox sends this parameter:
csrfmiddlewaretoken:"tGA4jKF1pd1QFC6NSAM9eNFvZqs60r4m"
That's it. No text, no color. I have no idea what's going on.
None of your fields have a name attribute, so the browser sends no data.
You should really be using Django's forms framework for this, though.
As mentioned above, you need to specify a "name" attribute in your input fields. You don't need to use Django forms, but if you are submitting the form normally, any fields you are expecting to be sent need to have a name.
<form action="./validate_code/" method="POST">
{% if error %}
<p>Sorry, that wasn't right.</p>
{% endif %}
<label for="class_code">Your class code: </label>
<input type="text" name="class_code" id="class_code">
<input type="color" name="color">
{% csrf_token %}
<button type="submit">Submit</button>
</form>