I'm trying to use JQuery widget Multiple select instead of default.
As you can see, I'm trying to render select options right into <select> tag but, it's weird that it is being rendered after </select> tag. (for loop is inside this tag, not after)
EDIT:
Simly, this is in the template:
<tag> {% for choice in field.choices %}{{ choice }}{%endfor%} </tag>
but Django renders it like it was:
<tag></tag> {% for choice in field.choices %}{{ choice }} {%endfor%}
This is rendered:
<div class="col-xs-5">
<select name="from[]" id="multiselect" class="form-control" size="8" multiple="multiple">
</select> # options are render after this tag
<option value="97">Slovak</option>
<option value="98">Russian</option>
<option value="99">Magyar</option>
<option value="100">English</option>
<option value="101">Croatian</option>
<option value="102">Spanish</option>
<option value="103">Dutch</option>
<option value="104">Estonian</option>
<option value="105">French</option>
<option value="106">DSADSA</option> etc....
This is the template:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load static %}
{% block head %}
<script type="text/javascript" src="{% static "js/scripts/multiselect.min.js" %}"></script>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('#multiselect').multiselect();
});
</script>
{% endblock %}
{% block content %}
<h1>Become a translator</h1>
<form id="user_form" method="post" action="">
{% csrf_token %}
{{ register_as_translator_form | crispy }}
<button class="accordion" type="submit" name="submit" value="Register">Register</button>
</form>
<div class="row">
<div class="col-xs-5">
<select name="from[]" id="multiselect" class="form-control" size="8" multiple="multiple">
{% for choice in register_as_translator_form.languages %}
{{ choice }}
{% endfor %}
</select>
</div>
<div class="col-xs-2">
<button type="button" id="multiselect_rightAll" class="btn btn-block"><i
class="glyphicon glyphicon-forward"></i></button>
<button type="button" id="multiselect_rightSelected" class="btn btn-block"><i
class="glyphicon glyphicon-chevron-right"></i></button>
<button type="button" id="multiselect_leftSelected" class="btn btn-block"><i
class="glyphicon glyphicon-chevron-left"></i></button>
<button type="button" id="multiselect_leftAll" class="btn btn-block"><i
class="glyphicon glyphicon-backward"></i></button>
</div>
<div class="col-xs-5">
<select name="to[]" id="multiselect_to" class="form-control" size="8" multiple="multiple"></select>
</div>
</div>
{% endblock %}
Unfortunately, it renders this:
Do you know what to do?
That could happen if register_as_translator_form.languages return string list, try this way:
<select name="from[]" id="multiselect" class="form-control" size="8" multiple="multiple">
{% for choice in register_as_translator_form.languages %}
<option value="{{ choice }}">{{ choice }}</option>
{% endfor %}
</select>
Related
I want to display a button next to a form instead of below:
This is how it looks:
should look like this:
corresponding code:
form:
class TheForm(forms.Form):
var = forms.ChoiceField(choices = choices, label = "")
temp
late:
{% if TheForm %}
<form method = "post">
{% csrf_token %}
{% bootstrap_form TheForm %}
{% bootstrap_button button_type="submit" content="OK" %}
</form>
{% endif %}
EDIT:
Got it working, but the code only works with one input field:
I imagined it would look like this:
but the produced code looks weird too:
<form method="POST" action="/singleDevice">
<input type="hidden" name="csrfmiddlewaretoken" value="...">
<div class="row">
<div class="col-9">
<div class="col-12">
<select name="env" class="form-select" id="id_env">
<option value="0">A</option>
<option value="3">NB</option>
</select>
</div>
<div class="col-12">
<label class="visually-hidden" for="id_Number">directly enter a number</label>
<input type="text" name="Number" class="form-control" placeholder="directly enter a number" id="id_Number">
</div>
</div>
<div class="col-3">
<button class="btn btn-primary" type="submit">OK</button>
</div>
</div>
</form>
Try bootstrap Grid system,
template.html
{% if TheForm %}
<form method = "post">
{% csrf_token %}
<div class="row"> <!-- creates a row of 12 parts -->
<div class="col-lg-9"> <!-- Creates a column with width of 9 parts out of the above 12 parts -->
{% bootstrap_form TheForm %}
</div>
<div class="col-lg-3"> <!-- Creates a column with width of remaining 3 parts out of the above 12 parts -->
{% bootstrap_button button_type="submit" content="OK" %}
</div>
</div>
</form>
{% endif %}
EDIT: CSS TO SET BUTTON AT THE END OF FORM
<form method="POST" action="/singleDevice">
<input type="hidden" name="csrfmiddlewaretoken" value="...">
<div class="row">
<div class="col-9">
<div class="col-12">
<select name="env" class="form-select" id="id_env">
<option value="0">PROD</option>
<option value="1">QA</option>
<option value="2">DEV</option>
<option value="3">NB</option>
</select>
</div>
<div class="col-12">
<label class="visually-hidden" for="id_Number">directly enter a number</label>
<input type="text" name="Number" class="form-control" placeholder="directly enter a number" id="id_Number">
</div>
</div>
<div class="col-3">
<button class="btn btn-primary form-submit-button" type="submit">OK</button>
</div>
</div>
</form>
base.css:
.form-submit-button{
display: inline-block;
margin-top: 95%; /* (approx) */
}
CHANGES: I have set a classname to the submit button and pushed the submit button to the bottom end of the y-axis using margin-top (percentage ensures that it stays below even on resizing)
I tried so many times , but i didn't get any solution for rendering image and content without html tags , i used {{content|safe}} for rendering content on template and i tried strip_tags for html tags removal but it didn't work well for url's , can anyone suggest me why i am getting html tags while rendering in templates.
Models.py
class Product(models.Model):
name = models.RichTextUploadingField(max_length="125")
description = models.RichTextUploadingField()
views.py
def poll():
context = questions.object.all()
return render(request, 'quiz.html', context)
template:
<html>
<body>
{% for q in context %}
<div id="section{{forloop.counter}}">
<script type="text/javascript">
marks.push(Number("{{marks}}"));
neg_marks.push(Number("{{neg_marks}}"));
</script>
<p id="{{forloop.counter}}"><span>{{forloop.counter}}.{{q.question|linebreaks}}
</span></p>
{% if q.figure %}
<img src="{{q.figure.url}}" alt="image" class="img-fluid" width="200px" height="200px"><br><br>
{% endif %}
<input type="radio" id="{{forloop.counter}}option1" name="{{forloop.counter}}" value="1">
<label for="{{forloop.counter}}option1">{{q.option_1|safe }}</label><br>
<input type="radio" id="{{forloop.counter}}option2" name="{{forloop.counter}}" value="2">
<label for="{{forloop.counter}}option2">{{q.option_2|safe}}</label><br>
{% if q.option_3 != "" %}
<input type="radio" id="{{forloop.counter}}option3" name="{{forloop.counter}}" value="3">
<label for="{{forloop.counter}}option3">{{q.option_3|safe}}</label><br>
{% endif %}
{% if q.option_4 != "" %}
<input type="radio" id="{{forloop.counter}}option4" name="{{forloop.counter}}" value="4">
<label for="{{forloop.counter}}option4">{{q.option_4|safe}}</label><br>
{% endif %}
{% if q.option_5 != "" %}
<input type="radio" id="{{forloop.counter}}option5" name="{{forloop.counter}}" value="5">
<label for="{{forloop.counter}}option5">{{q.option_5|safe }}</label><br>
{% endif %}
{% if forloop.first %}
<p role="button" class="btn btn-warning" id="next{{forloop.counter}}" onclick="next_section(this.id)"
style="float: right;">Next</p>
{% elif forloop.last %}
<p role="button" class="btn btn-warning" id="prev{{forloop.counter}}" onclick="prev_section(this.id)"
style="float:left;">Previous</p>
{% else %}
<p role="button" class="btn btn-warning" id="next{{forloop.counter}}" onclick="next_section(this.id)"
style="float: right;">Next</p>
<p role="button" class="btn btn-warning" id="prev{{forloop.counter}}" onclick="prev_section(this.id)"
style="float:left;">Previous</p>
{% endif %}
<br>
<hr>
</body>
</html>
there is no direct way to access images in ck-editor field ... but if you have the direction to the img you can get it like /media/uploads/2020/12/21/my_img.jpg
i suggest to use image field to store external field with image you want
and you have problem here : context = questions.object.all() it should be
context = {'context':questions.object.all()}
nam and mob field are userinput fields which i am using for user input and later i am using them for filtering
{% block content %}
<form class="form-signin" action="" method="POST">
{% csrf_token %}
<div class="mb-3">
<input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Nam" autofocus>
</div>
</div>
<div class="mb-3">
<select class="custom-select center-block" name="mob" id="mob" >
<option>{{ customer.sponsor }}</option>
{% for i in sponsor %}
<option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
{% endfor %}
</select>
<div class="invalid-feedback">
Please select a valid Existing Customer.
</div>
</div>
<div class="mb-3">
Search
Urls.py
path('customer_view',views.customer_view,name='customer_view')
Views.py
def customer_view(request):
print(request.method )
name1 = str(request.POST.get('nam'))
print(name1)
mobile1 = str(request.POST.get('mob'))
print(mobile1)
customers_list = customer.objects.filter(
mobile=mobile1) & customer.objects.filter(name=name1)
sponsors = customer.objects.all().distinct('mobile')
ctx = { 'customer': customers_list, 'sponsor': sponsors }
return render(request, 'pages/customer_view.html', ctx)
You use href which does not submit the form. You need a submit button and change your action of your form to your view url. Try this:
{% block content %}
<form class="form-signin" action="{% url 'customer_view' %}" method="POST">
{% csrf_token %}
<div class="mb-3">
<input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Nam" autofocus>
</div>
</div>
<div class="mb-3">
<select class="custom-select center-block" name="mob" id="mob" >
<option>{{ customer.sponsor }}</option>
{% for i in sponsor %}
<option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
{% endfor %}
</select>
<div class="invalid-feedback">
Please select a valid Existing Customer.
</div>
</div>
<div class="mb-3">
<input type="submit" value="Search"/>
.
.
...
In your form, you write a button as:
Search
But this thus means that this is just a link that links to a new page. As a result, the browser will make an (empty) GET request to the given url, and never submit the form.
You can construct a button that submits the form with:
<form class="form-signin" action="{% url 'customer_view' %}" method="post">
<!-- … -->
<button type="submit" class="btn btn-primary btn-sm" role="button">Search</button>
<!-- … -->
</form>
That being said, a search is often done with a GET request, so you might want to change method="get", and obtain the parameters through request.GET.get(..) instead.
I have an input in my form which I try to label in Russian, for example:
email = forms.CharField(label='ййй')
The problem occurs when the label contains only non-English characters, the label simply disappears.
Interestingly enough, when the label contains at least one English character, the label will appear, for example:
email = forms.CharField(label='йййa')
Works correctly.
The same problem occurs with attributes in the widget parameter
email = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'ййй'}))
This the template HTML code:
{% load widget_tweaks %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% if field.name == 'birth_date' %}
{% render_field field class="form-control date" id="date" name="date" %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
This is the output html code:
<h2>Sign up</h2>
<form method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='Q2EvMWMCPrV597OQV8aqJwtC4X1zuSQI9oVdjeZtgzQUYTAYSp5v22cz2zfOZgwn' />
<div class="form-group">
<input type="text" name="email" autofocus required class="form-control" id="id_email" />
</div>
<div class="form-group">
<label for="id_id_num">Id number:</label>
<input type="text" name="id_num" id="id_id_num" required class="form-control" maxlength="9" />
</div>
<div class="form-group">
<label for="id_password1">Password:</label>
<input type="password" name="password1" required class="form-control" id="id_password1" />
<small class="form-text text-muted"><ul><li>Your password can't be too similar to your other personal information.</li><li>Your password must contain at least 8 characters.</li><li>Your password can't be a commonly used password.</li><li>Your password can't be entirely numeric.</li></ul></small>
</div>
<div class="form-group">
<label for="id_password2">Password confirmation:</label>
<input type="password" name="password2" required class="form-control" id="id_password2" />
<small class="form-text text-muted">Enter the same password as before, for verification.</small>
</div>
<div class="form-group">
<label for="id_first_name">First name:</label>
<input type="text" name="first_name" id="id_first_name" required class="form-control" maxlength="30" />
</div>
<div class="form-group">
<label for="id_last_name">Last name:</label>
<input type="text" name="last_name" id="id_last_name" required class="form-control" maxlength="30" />
</div>
<div class="form-group">
<label for="id_birth_date">Birth date:</label>
<input type="text" name="birth_date" required id="date" name="date" class="form-control date" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Found a solution, simply add "u" before the string to make it unicode
email = forms.CharField(label=u'ййй')
{% extends "account/base.html" %}
{% load url from future %}
{% load i18n %}
{% block head_title %}{% trans "Signup" %}{% endblock %}
{% block content %}
</style>
<h1><b>Free Membership</b>Sign up Today</h1>
<h1>{% trans "Sign Up" %}</h1>
<p>{% blocktrans %}Already have an account? Then please sign in.{% endblocktrans %}</p>
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
{{ form.as_p }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<button type="submit">{% trans "Sign Up" %} »</button>
</form>
<h1><b>Free Membership</b>Sign up Today</h1>
{% endblock %}
{% block content2 %}
{% endblock %}
This is the original Code from Django All Auth.
What I want to do is bring it more to life like add some better HTML5 or CSS to it when I do it does not change?
Here's my login.html template (I'm using Bootstrap, Jquery and Font-Awesome for the icons).
{% extends "account/base.html" %} {% load i18n custom_tags account %} {% block head_title %}{% trans "Sign In" %}{% endblock %} {% block common_scripts %}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
// Load jquery straight from the cdn for this page.
$(function() {
// We need some mappings for Providers names from AllAuth to the icon class names.
$('.btn-google').addClass('btn-google-plus');
$('.btn-linkedin_oauth2').addClass('btn-linkedin');
$('.fa-linkedin_oauth2').addClass('fa-linkedin');
$('#id_login').addClass('form-control').removeAttr('autofocus').blur();
$('#id_password').addClass('form-control');
});
</script>
{% endblock %} {% block content %}
<div class="container col-md-12">
<div class="well login-well">
<div class="socialaccount_ballot">
<ul class="socialaccount_providers">
{% load socialaccount %} {% for provider in socialaccount.providers %} {% if provider.id == "openid" %} {% for brand in provider.get_brands %}
<li>
<a title="{{ brand.name }}" class="btn btn-block btn-social btn-md btn-{{ provider.id }} socialaccount_provider {{ brand.id }}" href="{% provider_login_url provider.id openid=brand.openid_url process=" login " %}">
<i class="fa fa-{{ provider.id }}"></i>Log in with {{ brand.name }}
</a>
</li>
{% endfor %} {% endif %}
<li>
<a title="{{ provider.name }}" class="btn btn-block btn-social btn-md socialaccount_provider btn-{{ provider.id }}" href="{% provider_login_url provider.id process=" login " %}">
<i class="fa fa-{{ provider.id }}"></i>Log in with {{ provider.name }}
</a>
</li>
{% endfor %}
</ul>
<hr>
<form class="django-login" method="POST" action="{% url 'login' %}">
{% csrf_token %} {% if form.non_field_errors %}
<div class="alert alert-warning">
<ul class="alert-message">
{% for error in form.non_field_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<div class="input-group {% if form.login.errors %}has-error{% endif %}">
<span class="input-group-addon glyphicon glyphicon-envelope"></span>
{{ form.login }}
</div>
<div class="input-group {% if form.password.errors %}has-error{% endif %}">
<span class="input-group-addon glyphicon glyphicon-lock"></span>
{{ form.password }}
</div>
<div class="fieldWrapper form-inline remember text-center">
{{ form.remember }}
<label for="id_remember" class="text-muted">Remember me</label>
</div>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />{% endif %}
<div class="btn-div pull-center">
<button class="btn btn-primary btn-block" type="submit">{% trans "Sign In" %}</button>
</div>
</form>
</div>
<hr>
<div>
<small><a class="text-muted" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a></small>
<br>
<small><a class="text-muted" href="{% url 'signup' %}">Sign up</a></small>
</div>
</div>
</div>
{% endblock %}
And how it looks:
You can put custom templates for allauth in your template directory under account folder. Django allauth would take the custom template from it and renders it.
Following is sample signup page I have created some days ago:
{% extends "base.html" %}
{% load staticfiles %}
{% load socialaccount %}
{% block extra_body %}account-bg{% endblock %}
{% block partial %}
<div class="container">
<div class="row">
<div class="account-form-container">
<div class="row">
<div class="col-md-12">
<h3>Create New Account</h3>
</div>
</div>
<div class="row">
<form class="form-horizontal signup-form" id="userSignupForm" action="{% url "account_signup" %}" method="post" role="form">
{% csrf_token %}
{% if request.GET.next %}
<input type="hidden" name="next" value="{{request.GET.next}}">
{% endif %}
{% if form.non_field_errors %}
<div class="fieldWrapper alert alert-danger">
<ul>
{% for error in form.non_field_errors %}
<li>{{ error | lower}}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<div class="col-lg-12">
<div class="form-group inner-addon left-addon {% if form.email.errors %}has-error{% endif %}">
<i class="icon-user"></i>
<input class="form-control login-field" type="text" id="id_email" name="email" placeholder="Email">
<span class="help-block">{{ form.email.errors }}</span>
</div>
<div class="form-group inner-addon left-addon {% if form.password1.errors %}has-error{% endif %}">
<i class="icon-lock"></i>
<input class="form-control login-field" type="password" id="id_password1" name="password1" placeholder="Password">
<span class="help-block">{{ form.password1.errors }}</span>
</div>
<div class="form-group inner-addon left-addon {% if form.password1.errors %}has-error{% endif %}">
<i class="icon-lock"></i>
<input class="form-control login-field" type="password" id="id_password2" name="password2" placeholder="Confirm Password">
<span class="help-block">{{ form.password1.errors }}</span>
</div>
<div class="form-group">
<div class="col-md-5">
<button type="submit" class="btn btn-complete btn-bold pull-right">Sign Up</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
I hope this would help you.