I am wondering how I can display each service array separately and not actually loop through all the django form query. This is how the loop is currently constructed. However I want to only view a few services - how can I achieve this. Here is the current loop...
<div class="form-group">
<label class="control-label" for="id_services_0">Services</label>
<div id="id_services" for="id_check_{{ forloop.counter0 }}">
{% for service in form.services.field.queryset %}
<div style="min-height:100px;" class="col-lg-3 checkbox" id="id_check_{{ forloop.counter0 }}">
<label for="id_services_{{ forloop.counter0 }}">
<div for="id_collapse_{{ forloop.counter0 }}">
{% if service.picture %}
<img class="img-responsive" src="{{ service.picture.url }}" height="50" width="50" alt="">
{% else %}
<img style="display:none;" src="https://placehold.it/50x50" alt="">
{% endif %}
<input class="" id="id_services_{{ forloop.counter0 }}" name="services" type="checkbox" value="{{ service.id }}" data-price="{{ service.fee }}">
{{ service }}<br>
<a class="more-details" role="button" data-toggle="collapse" href="#id_collapse_{{ forloop.counter0 }}" aria-expanded="false" aria-controls="collapseExample">More Details</a>
<div class="collapse" id="id_collapse_{{ forloop.counter0 }}">
<div class="well">
<p>
<small>
{{ service.description }}
</small>
</p>
</div>
</div>
</div>
</label>
</div>
{% endfor %}
</div>
</div>
First 10
{% for service in form.services.field.queryset.all|slice:":10" %}
From 2 to 4
{% for service in form.services.field.queryset.all|slice:"2:4" %}
n-th element
Remove for-loop and
first
{{ form.services.field.queryset.all.0 }}
second
{{ form.services.field.queryset.all.1 }}
Specific rows in services
QuerySet and Manager:
services/query.py
class ServiceQuerySet(QuerySet):
def opened(self):
return self.filter(status='OPEN')
services/manager.py
class ServiceManager(Manager):
def get_query_set(self):
return ServiceQuerySet(self.model, using=self._db)
def opened(self):
return self.get_query_set().opened()
Overwrite manager in model:
class Service(models.Model):
....
status = models.CharField(choices=['OPEN', 'CLOSE'], max_length=10)
objects = ServiceManager()
Use in template:
{{ form.services.field.queryset.opened.0 }}
{% for open_service in form.services.field.queryset.opened %}
Related
I'm currently building a website that makes use of django-allauth and I've come across a problem regarding error messages while using a custom User model.
My custom User model is called CustomUser, I've noticed that when django-allauth handles errors regarding the username, it uses the name of the user model as the starting word to the error message's sentence. Example image is linked below.
Image of the error message
How can I change this error message? I'd like to stay away from overriding any django-allauth views if possible, though I'd be happy with any solution!
This is my Django form code that makes use of django-crispy-forms:
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
<div class="row">
<div class="col-12 col-lg-7">
{{ form.email|attr:"autofocus"|as_crispy_field }}
</div>
<div class="col-12 col-lg-5">
{{ form.username|as_crispy_field }}
</div>
<div class="col-6 col-lg-4">
{{ form.first_name|as_crispy_field }}
</div>
<div class="col-6 col-lg-4">
{{ form.last_name|as_crispy_field }}
</div>
<div class="col-12 col-lg-4">
{{ form.birthday|as_crispy_field }}
</div>
<div class="col-12 col-sm-6">
{{ form.password1|as_crispy_field }}
</div>
<div class="col-12 col-sm-6">
{{ form.password2|as_crispy_field }}
</div>
<div class="col-12 text-center">
{{ form.captcha|as_crispy_field }}
{% for error in form.captcha.errors %}
{% if error %}
<style>
#div_id_captcha {
margin-bottom: 0px !important;
}
</style>
<span class="invalid-feedback d-block mb-3">
<strong>
You must complete the captcha to register
</strong>
</span>
{% endif %}
{% endfor %}
</div>
</div>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<button type="submit" class="btn btn-primary btn-light mb-3">Register <i class="fa fa-user-plus fa-fw"></i></button>
<p>Already have an account?<br /><small>Login here</small></p>
</form>
The following is my current code:
{% for comment in commentlist %} <!-- Comment section -->
<h5>
{{ comment[0] }}
</h5>
<h6>Created {{ comment[1].strftime('%d/%m/%Y at %I:%M%p') }} By: {{ comment[2] }}</h6>
{% if session.username == comment[2] %}
<form action="{{ url_for('detail', slug=entry.slug) }}" class="form-horizontal" method="post">
<input class="form-control" name="commentid" type="hidden">
<div class="form-group">
<div class="col-sm-offset-0 col-sm-1">
<button class="btn btn-default1" type="submit">Delete!</button>
</div>
</div>
</form>
{% endif %}
<br></br>
{% endfor %}
When the button it pressed it should return the value of
{{ comment[3] }}
This value I should then be able to fetch from Flask in the form of
request.form.get('commentid')
Any ideas of how this could be done would be much appreciated!
<input class="form-control" name="commentid" type="hidden" value="{{ comment[3] }}">
I'm currently coding a website using Django and Bootstrap.
I created the templates and models first, and now, I'm implementing the controllers. All is not implemented yet, but I needed some help on this. I was wondering how to render a Django authentication form with the Boostrap grid system. I'm using Boostrap 4 and Django 2.0.4. My older form was like this :
<div class="jumbotron">
<form class="form-horizontal" method="post" action="{% url 'login' %}">
{% csrf_token %}
<div class="form-group">
<div class="col-lg-4 offset-lg-4">
<label class="control-label" for="usernameInput">{{ form.username.label_tag }}</label>
<input id="usernameInput" type="text" name="{{ form.field.html_name }}" value="{{ form.username }}" class="form-control"
placeholder="Username">
</div>
</div>
<div class="form-group">
<div class="col-lg-4 offset-lg-4">
<label class="control-label" for="passwordInput">{{ form.password.label_tag }}</label>
<input id="passwordInput" type="password" name="{{ form.field.html_name }}" value="{{ form.field.value }}" class="form-control"
placeholder="Password">
</div>
</div>
<div class="container" id="btn_login">
<button type="submit" class="btn btn-primary btn-md" value="login" role="button">Log in</button>
<input type="hidden" name="next" value="{{ next }}"/>
</div>
</form>
<span class="container" id="forgotten_password">
Forgot your password ?
</span>
</div>
And here is the new one :
<div class="jumbotron">
{% load widget_tweaks %}
<form class="form-horizontal" method="post" action="{% url 'login' %}">
{% 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" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
<div class="container" id="btn_login">
<button type="submit" class="btn btn-primary btn-md" role="button">Log in</button>
</div>
</form>
<span class="container" id="forgotten_password">
Forgot your password ?
</span>
</div>
But as you can obviously tell, this is not rendering the same way.
For example, I'd like to take back the width of the input.
For the rest, I use this line in my urls.py
re_path(r'^login/$', auth_views.login, {'template_name': 'main/login.html'}, name='login'),
And this one in my settings.py to get redirected to the right page :LOGIN_REDIRECT_URL = 'my_pls'
I googled a lot and finally used this link (in case you case notice something I didn't understand) : https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html#understanding-the-rendering-process
You should use custom HTML attributes on your **forms.py**.
It's simple:
from django import forms
class your_form(forms.Form):
attrs_for_the_field = {
'class': 'form-control',
'placeholder': 'Write here!',
}
field = forms.CharField(widget=forms.CharField(attrs=attrs_for_the_field))
With this code you will render the following HTML:
<input type="text" name="field" class="form-control" placeholder="Write here!" id="id_field">
Take a look at https://docs.djangoproject.com/en/2.0/ref/forms/widgets/ in order to know how Django represents an HTML input element.
You also should read https://docs.djangoproject.com/en/2.0/ref/forms/fields/ so that you could understand how it works.
You can add all the HTML configuration for the fields in the form code in forms.py . Than the form can be displayed with just {{ form.as_p}}.
Width of input can be retrieved with jQuery or JavaScript .
I'm using django 1.11 and python 3.5.2.
This is the important part of my code in views.py
ListaValoresLeidosXML = []
for sec in secciones:
print(sec.contenidoxml)
treexml = ET(fromstring(sec.contenidoxml))
Lista = []
for child in treexml.findall('caracteristica'):
nombre = child.find('nombre').text
Lista.append(nombre)
ListaValoresLeidosXML.append(Lista)
for lista in ListaValoresLeidosXML:
for valor in lista:
print(valor)
print('*****')
return render(request, 'editarCatElem.html', {
'elemento': ElemSeg,
'link': tutorial,
'subelementos': subelementos,
'secciones': secciones,
'listaXML' : ListaValoresLeidosXML
})
I'm reading a string field in my database, it contents a xml-formatted text, using treexml I can get all the attributes.
Finally I have a list of lists, each list have values of a section, it means if I have n sections then I'll have n lists inside a list.
I'm sending a list called ListaXML to my template.
In my template I'm trying to iterate it
{% for seccion in secciones %}
<div style="display: block" class="form-group">
<div style="display: block" class="form-group">
<label class="control-label col-md-2" for="nombreSeccion">Sección <span class="required">*</span></label>
<div class="col-md-2"><input type="text" value='{{ seccion.nombre }}' id="nombreSeccion" name="secciones[][nombreSeccion]" class="form-control col-md-2"></div>
<label class="control-label col-md-2" for="descSeccion">Descripción <span class="required">*</span></label>
<div class="col-md-2"><input type="text" value='{{ seccion.descripcion }}' id="descSeccion" name="secciones[][descripcionSeccion]" class="form-control col-md-2"></div>
</div>
<div class="col-md-offset-2 col-md-7">
<div class="form-horizontal form-label-left" style="background:lightblue; padding:8px;">
{% for caracteristica in listaXML %} # <-- Here is the problem!!
<div class="form-group">
<label class="control-label col-md-2" for="nombreCampo">Campo <span class="required">*</span></label>
<div class="col-md-4">
<input type="text" value='{{ caracteristica }}' name="secciones[][campos][][nombreCampo]" id="nombreCampo" class="form-control col-md-2">
</div>
</div>
{% endfor %}
<div class="form-group">
<button type="button" class="btn btn-primary col-sm-offset-0 col-sm-3" onclick="agregarCampo(event);"><i class="fa fa-plus"> </i> Agregar campo</button>
<button type="button" class="btn btn-danger col-sm-offset-1 col-sm-3" onclick="eliminarCampo(event);"><i class="fa fa-minus"> </i> Eliminar campo</button>
</div>
</div>
</div>
</div>
{% endfor %}
The problem is I need to do this with every repetition:
{% for caracteristica in listaXML.0 %}
{% for caracteristica in listaXML.1 %}
{% for caracteristica in listaXML.2 %}
{% for caracteristica in listaXML.3 %}
...
I need to increment the final number (the index of the list).
I've tried
{% for caracteristica in listaXML.forloop.counter0 %}
or
{% for caracteristica in listaXML.{{forloop.counter0}} %}
but it doesn't work.
Any idea on how the code works?
Looks like you just need a double for loop :
{% for lista in listaXML %}
{% for caracteristica in lista %}
<div class="form-group">
[...]
{% endfor %}
{% endfor %}
Have form.
class AddLogo(forms.Form):
title = forms.CharField(max_length=100)
date = forms.TimeField()
image = forms.ImageField()
And have standard Django preview form.
from django.contrib.formtools.preview import FormPreview
from django.http import HttpResponseRedirect
from company.models import Company
class add_logo_preview(FormPreview):
form_template = 'add_logo_preview.html'
def done(self, request, cleaned_data):
return HttpResponseRedirect('/company/hey/')
In template
{% extends "base.html" %}
{% block block_js %}
{% endblock block_js %}
{% block content %}
<h1>Preview your submission</h1>
{% with form as job %}
<div class="job_block">
<div class="job_content">
<div class="job_title">
<h1>{{ job.title.data }}</h1>
<span class="date">{{ job.date.date.data }}</span>
</div>
<div class="job_company">
<div class="company_logo">
{{ job.image.url }}
</div>
<div class="company_description">
<div class="name">{{ job.company_name.data }}</div>
<div class="location">Главный офис: {{ job.company_location.data }}</div>
<div class="website">{{ job.company_url.data }}</div>
</div>
</div>
<article>
{{ job.description.data|safe }}
</article>
</div>
<aside>
<h4>Поделиться вакансией</h4>
</aside>
</div>
<div class="job_feedback">
<h3>Откликнутся на вакансию</h3>
<p>{{ job.apply_terms.data|safe }}</p>
</div>
{% endwith %}
<p>Security hash: {{ hash_value }}</p>
<form action="" method="post">{% csrf_token %}
{% for field in form %}{{ field.as_hidden }}
{% endfor %}
<input type="hidden" name="{{ stage_field }}" value="2" />
<input type="hidden" name="{{ hash_field }}" value="{{ hash_value }}" />
<p><input type="submit" value="Submit" /></p>
</form>
<h1>Or edit it again</h1>
<h1>Шаг первый: создайте свое объявление</h1>
<form action="/job/preview/" method="POST" class="form">{% csrf_token %}
<h3>Опишите вакансию</h3>
<fieldset>
<div class="label_box">
<label for="">Назавние вакансии</label>
</div>
<div class="fields_box">
{{ form.title }}
</div>
</fieldset>
<fieldset>
<div class="label_box">
<label for="">Категория</label>
</div>
<div class="fields_box">
{% for cat in form.category %}
{% if cat.choice_value %}
{{ cat }}
{% endif %}
{% endfor %}
</div>
</fieldset>
<fieldset>
<div class="label_box">
<label for="">Головной офис</label>
</div>
<div class="fields_box">
{{ form.company_location }}
</div>
</fieldset>
<fieldset class="one_col">
<div class="label_box">
<label for="">
Описание вакансии
</label>
</div>
<div class="fields_box">
{{ form.description }}
</div>
</fieldset>
<fieldset>
<div class="label_box">
<label for="">
Apply this job.
</label>
</div>
<div class="fields_box">
{{ form.apply_terms }}
</div>
</fieldset>
<h3>О компании</h3>
<fieldset>
<div class="label_box">
<label for="">Название</label>
</div>
<div class="fields_box">
{{ form.company_name }}
</div>
</fieldset>
<fieldset>
<div class="label_box">
<label for="">Логотип</label>
</div>
<div class="fields_box">
{{ form.image }}
</div>
</fieldset>
<fieldset>
<div class="label_box">
<label for="">URL</label>
</div>
<div class="fields_box">
{{ form.company_url }}
</div>
</fieldset>
<fieldset>
<div class="label_box">
<label for="">Email</label>
</div>
<div class="fields_box">
{{ form.company_email }}
</div>
</fieldset>
<fieldset>
<div class="label_box">
<span>
Подсветить это объявление?
</span>
<label for="">{{ form.highlight }} Подсветить?</label>
</div>
</fieldset>
<input type="submit" value="Просмотреть">
</form>
{% endblock %}
I try: {{ form.image.url }} but does not work. I understand one: Django do not upload file, but how i cane change this? How i can make preview image in django FormPreview?
Convert your image to a base64 string, add a field to your form for that base 64 string. In the preview template, display the base64 string. And when finally saving the image, you will need to create a file, write the content of the base 64 string to it, and then save that file to your model.