Django concat variable in template - django

i have this template on my Django
{% for car in cars%}
<div class="form-row">
<label for="detail-{{ detail.id }}"> <b>{{ detail.rejected_field.title }}</b> :
<img id="car-image" src="{{ car_manager_image_ + car.id }}">
</label>
</div>
{%endfor%}
car_manager_image is another image url sent via extra_context['car_manager_image_'+car.id] on the backend
my question is, how do i concate the data on Django Template?
im expecting result like 'car_manager_image_1'

You can use django build in add filter to concat both strings. Your code should be changed to
<img id="car-image" src="{{ car_manager_image_|add:car.id }}">

Related

FlatPicker in Django Edit custom form showing time, only days

I have a Django app, with new template where datetimepicker works well. I created an edit template but the widget do not load automatically, so I added them. But for some reason, i do not know how to bring the datetimepicker, I can only get the datepicker.
Does anybody knows please ?
The precise line in the edit template looks like:
<div>
<input id="Restriction_Start_Date_id" type="text" name="Restriction_Start_Date" value="{{ restricted_name_obj.Restriction_Start_Date|date:'n/j/Y G:i' }}" class="form form-control datepicker" >
</div>
While on the new template I have :
<div>{{ form.Restriction_Start_Date.label_tag }}</div>
<div>
{{ form.Restriction_Start_Date.errors }}
{{ form.Restriction_Start_Date }}
</div>
Solution found, I used the CDN and the following :
<link href="https://cdn.jsdelivr.net/npm/flatpickr#4.5.2/dist/flatpickr.min.css"type="text/css" media="all" rel="stylesheet">
<div>
<input type="hidden" name="Restriction_Start_Date" required="" id="Restriction_Start_Date" fp_config="{"id": "fp_14", "picker_type": "DATETIME", "linked_to": "fp_13", "options": {"mode": "single", "dateFormat": "Y-m-d H:i:S", "altInput": true, "enableTime": true}}"class="flatpickr-input" value="{{ restricted_name_obj.Restriction_Start_Date|date:'n/j/Y G:i' }}">
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/flatpickr#4.5.2/dist/flatpickr.min.js"></script>
<script type="text/javascript"src="https://cdn.jsdelivr.net/gh/monim67/django-flatpickr#1.0.0/static/js/django-flatpickr.js"></script>
(Not beautiful but it will do the job for now.

Django : Factorize HTML code

I am building a website about movies, actors etc... And I need your advices in order to factorize my templates.
Currently I have 5 templates :
base.html : containing header menu, footer [located in the master templates folder]
actors : display all the actors (extends base.html) [located in the templates folder from the application 'movie-library']
actor : display one actor and all tbe movies he played in (extends base.html) [located in the templates folder from the application 'movie-library']
movies : display all the movies (extends base.html) [located in the templates folder from the application 'movie-library']
movie : display one movie (extends base.html) [located in the templates folder from the application 'movie-library']
So, in actor.html and movies.html it's the same HTML code to display one movie.
And in actors.html and movies.html I have the same HTML to display a pagination bar.
For example here is the code used to display a movie :
<div class="col s3 module">
<div class="card movie-card">
<div class="card-image">
<img src="{{ MEDIA_URL }}{{ movie.photoshoot.folder }}/1.jpg" alt="{{ movie.title }} thumbnail">
</div>
<div class="card-content">
<span class="card-title grey-text text-darken-4">
{{ movie.title }}
</span>
<p>
<i class="material-icons blue-text accent-2-text">hd</i>
{% if movie.movie is not None %}
<i class="material-icons blue-text accent-2-text">local_movies</i>
{% endif %}
{% if movie.photos is not None %}
<i class="material-icons blue-text accent-2-text">image</i>
{% endif %}
<span class="grey-text darken-1-text right">{{ movie.movie.duration }}</span>
</p>
</div>
</div>
</div>
How could I factorize those to piece of code ?
Thank you.
You can use include tag for movies and custom tag for pagination

How to individually call a form to a html in django?

First time creating a webapp in django. please bear with me.
So I have this code in my html:
<h1><font color="Orange" face="Borda">{{ template_title }}</h1>
<form method='POST' action=''>{% csrf_token %}
<font color="Orange" face="Borda" style="background-color: transparent;">{{ form }}</font>>
<input type='submit' value='SUBMIT'>
</form>
now inside {{ form }} is all the views and forms. now what I want to do is to specifically call it individually (with style).
for example if I have:
email=CharField()
username=CharField()
and I wanted to call them in my html as:
<label class="sr-only" for="r-form-first-name">**EMAIL/USERNAME**</label>
<input type="text" name="r-form-first-name" placeholder="First name..." class="r-form-first-name form-control" id="r-form-first-name">
How do I do that? I already have a bootstrap template prepared for my login but other documentation suggest I use crispy forms, which I dont want to. Please tell me how if there is other ways of calling it.
Since it's your first time building a django app, i suggest you take your time building it, try reading relative doc pages for the parts you're working with.
Here's an example from the docs (tailored for your case):
<div class="fieldWrapper">
{{ form.username.errors }}
<label for="{{ form.username.id_for_label }}">Your message:</label>
{{ form.username }}
</div>
https://docs.djangoproject.com/en/1.10/topics/forms/#rendering-fields-manually

Manually rendering subfields of a MultiValueField

In my Django 1.7 app, I'm trying to take advantage of the MultiValueField class to implement a password/confirm password form element, i.e. two separate password fields asking the user to enter and then confirm a new password. I already got this working with two separate fields and a clean() method on my form, but the "single" MultiValueField seems a better way of upholding the DRY principle, especially since I'll need to duplicate this not only in my user registration form, but also when users want to change their passwords.
However, my front-end is pretty specific, and not the least bit like Django's default form output, so I'm manually rendering my form fields. This works great -- until I get to the MultiValueField. For each form field, my HTML looks like this:
<div class="row collapse">
<div class="small-2 columns">
<span class="prefix"><i class="fi-mail"></i></span>
</div>
<div class="small-10 columns {% if form.email.errors %}error{% endif %}">
{{ form.email }}
{% if form.email.errors %}<small class="error">{{ form.email.errors }}</small>{% endif %}
</div>
</div>
I need to do similar formatting for each of the subfields of form.password, but nothing I've tried has given me a rendered subfield; the closest I've come is {{ form.fields.password.fields.0 }} in my template, which gives me output like <app.fields.PassField object at 0x7fb619821ef0>, however this obviously isn't a rendered form field.
Is there something simple and obvious that I'm missing, or is what I'm trying to do just not possible in Django?
The code in Render only one part of a MultiWidget in Django is appropriate for your problem, as farthVader suggests.
I had a similar problem and I ended up solving it this way:
<input type="text" id="{{ form.password.html_name }}_0" name="{{ form.password.html_name }}_0" value="{{ form.password.value.0|default_if_none:'' }}"/>
<input type="text" id="{{ form.password.html_name }}_1" name="{{ form.password.html_name }}_1" value="{{ form.password.value.1|default_if_none:'' }}"/>

get_profile alternative in for loop django

I'm trying to get information of a users avatar based in an extended profile model. I usually call the information via get_profile(). However on this occasion the call is within a for loop in the template and I get errors if one of the users are the same.
How would I go about avoiding this error?
{% for fevent in fevents %}
<!-- EVENT ><! -->
<div class="event">
<div class="pic">
{{ fevent.getPublishedPredictionsCount }}
<img src="{% thumbnail fevent.artwork.get_absolute_url 100x98 crop,upscale %}" alt="" width="100" height="98" />
<div class="overlay">
</div>
</div>
<h1>{{ fevent.title|trunchar:30 }}</h1>
{% autoescape off %}
{{ fevent.getEventPredictionScore|makestars }}
{% endautoescape %}
<ul class="details">
<li class="cat">
Category: {{ fevent.catagory }}
</li>
<li class="location">
{{ fevent.location }}
</li>
<li class="date">
{{ fevent.date_and_time }}
</li>
<li class="time">
7:00pm - 8:00pm
</li>
</ul>
<!-- CLEAR ><! --><div class="clear"></div>
<div class="hype">
<div class="avatar">
<img src="{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}" alt="" width="120" height="120" />
</div>
<p>{{ fevent.description|trunchar:200 }}… Read More</p>
</div>
<!-- CLEAR ><! --><div class="clear"></div>
</div>
<!-- END OF EVENT ><! -->
{% endfor %}
The problem is here:
{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}
Error Message returned:
Caught MultipleObjectsReturned while rendering: get() returned more than one UserProfile -- it returned 2! Lookup parameters were {'user__id__exact': 4L}
To expand on what Matt said, while using get_or_create is a good idea, you should definitely define your profile model's User link with a OneToOneField, instead of a ForeignKey.
user = models.OneToOneField(User, verbose_name=_(u'user'))
Now, if you forget to use get_or_create(), or somehow accidentally try to create a duplicate profile for the same user, the database will raise an IntegrityError.
That error means there are two UserProfile objects in the database matching the query used by get_profile, not that get_profile was called twice. You need to remove one of those profile objects from the database and make sure there aren't multiples created again. You should be able to use the get_profile method multiple times without issue. Maybe you have a get_or_create call in that function without checking the proper values.