Django: insert multiple input text and file - django

I have recently tried to learn Django and I want to know it's possible to insert multiple input with this form structure to table database? if it's possible, can you tell me how it is? Thank you!!!
<form class="log-in" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<button type="submit" class="btn btn-info">Submit</button>
</form>

What you are looking for are formsets: docs.djangoproject.com/en/3.0/topics/forms/formsets

Related

Django default value in in input field

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>

How to catch errors from form in Django?

I have a collapse div with login/sign-up forms. How I can catch errors if they are and show them on page?
My form in html:
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<p><label for="id_username">Username:</label> <input
id="id_username" type="text" name="username" maxlength="30" /></p>
<p><label for="id_password">Password:</label> <input
type="password" name="password" id="id_password" /></p>
<input type="submit" value="Log in" />
<input type="hidden" name="next" value="{{ request.get_full_path }}"
/>
</form>
Dude use the inbuilt login, it offers you with encryption of password. the method your using is little messy i would recommend to go with django inbuilt authentication. here's the link,
https://simpleisbetterthancomplex.com/tutorial/2016/06/27/how-to-use-djangos-built-in-login-system.html

Django form in a table can't get right parameters

<tbody>
{% for sec in sec_list %}
<tr>
<td>{{sec.c_id_id}}.{{sec.sec_id}}</td>
<td>{{sec.title}}</td>
<td>{{sec.name}}</td>
<td>{{sec.time}}</td>
<td>{{sec.r_no_id}}</td>
<td>{{sec.cur}}/{{sec.capcity}}</td>
<td><form method="post" role="form" action="">
{% csrf_token %}
<input class="hidden" type="submit" value="{{sec.c_id_id}}.{{sec.sec_id}}" name="course" id="course">
<input class="hidden" type="submit" value="{{sec.cur}}" name="num" id="num">
<input class="hidden" type="submit" value="{{sec.capcity}}" name="limit" id="limit">
<p class="form-action">
<input type="submit" value="选课" class="btn btn-link">
</p>
</form></td>
</tr>
{% endfor %}
</tbody>
I want to use a hidden form to transfer some parameters in a table.My code is above.But when I use
request.POST.get("limit",'')
,I got a null one.How to fix it
edit your html code by replacing the type="submit" to type="hidden",
<td><form method="post" role="form" action="">
{% csrf_token %}
<input class="hidden" type="hidden" value="{{sec.c_id_id}}.{{sec.sec_id}}" name="course" id="course">
<input class="hidden" type="hidden" value="{{sec.cur}}" name="num" id="num">
<input class="hidden" type="hidden" value="{{sec.capcity}}" name="limit" id="limit">
<p class="form-action">
<input type="submit" value="选课" class="btn btn-link">
</p>
</form></td>
Shouldn't those inputs be type=hidden instead of class=hidden?
<input type="hidden" value="{{sec.capcity}}" name="limit" id="limit">
The type=submit in all of them makes no sense to me as it is supposed to be used only in submit buttons.

django html form that is showing error this field is required

the post method is not getting the username record. the get method is working fine but html form is showing the following error:
This field is required
<h2>Users</h2>
<ul>
<form method="post" action=""><input type='hidden' name='csrfmiddlewaretoken' value='8gQo0iGRTDE7kayhFJqj2fOt7UkejlkG' />
<li>
<input type="text" value= mayur><br>
<input type="submit" value="follow" />
</li>
<li>
<input type="text" value= mayurnitrr><br>
<input type="submit" value="follow" />
</li>
<li>
<input type="text" value= lokesh><br>
<input type="submit" value="follow" />
</li>
</form>
</ul>
<p align="center">Back</p>
You haven't shown how you handle these but none of your inputs have a name parameter which you need to provide
<input type="text" name="somethingdescriptive" value="lokesh"><br>
request.POST should now contain the key "somethingdescriptive"

How to transfer changed content into plone theme with diazo?

Now I need the following search structure in the theme:
<div class="sideCol">
<aside class="siteSearch">
<form name="searchform" action="search" class="searchPage searchform" id="searchform">
<fieldset>
<legend>Website durchsuchen</legend>
<input class="searchPage text lang-de" name="SearchableText" type="text" size="25" title="Website durchsuchen" value="" placeholder="Suchbegriff..." />
<button type="submit"><i class="icon-search"></i></button>
</fieldset>
</form>
</aside>
</div>
All I need to get from Plones sunburst theme is the action link for the form element.
So I tried this:
<replace css:content-children="#portal-searchbox">
<xsl:variable name="action_link" select="form/#action" />
<form name="searchform" action="search" class="searchPage searchform" id="searchform">
<xsl:attribute name="action">${action_link}</xsl:attribute>
<fieldset>
<legend>Website durchsuchen</legend>
<input class="searchPage text lang-de" name="SearchableText" type="text" size="25" title="Website durchsuchen" value="" placeholder="Suchbegriff..." />
<button type="submit"><i class="icon-search"></i></button>
</fieldset>
</form>
</replace>
<replace css:content-children="#portal-searchbox" css:theme-children=".siteSearch" />
The problem ist that all I get in the theme is the structure of Plones Sunburst Search.
<div class="sideCol">
<aside class="siteSearch">
<form id="livesearch0" action="http://localhost:8080/mamuz/de/##search">
<div class="LSBox">
<label class="hiddenStructure" for="searchGadget">Website durchsuchen</label>
<input name="SearchableText" type="text" size="18" title="Website durchsuchen" placeholder="Website durchsuchen" accesskey="4" class="searchField" id="searchGadget" autocomplete="off">
<input class="searchButton" type="submit" value="Suche">
<div class="searchSection">
<input id="searchbox_currentfolder_only" class="noborder" type="checkbox" name="path" value="/mamuz/de/impressum">
<label for="searchbox_currentfolder_only" style="cursor: pointer">nur im aktuellen Bereich</label>
</div>
<div class="LSResult" id="LSResult">
<div class="LSShadow" id="LSShadow"></div>
</div>
</div>
</form>
<div id="portal-advanced-search" class="hiddenStructure">
Erweiterte Suche…
</div>
</aside>
</div>
I'm familiar with diazo but pretty new to xslt. What is wrong? I tired several types of placements like import before it gets modified. Nothing helps.
Using the replace directive on attribute itself should work:
<replace attributes="action"
css:content="#portal-searchbox form"
css:theme="#searchform" />