Scenario:I want to make a page for available slots for booking ,for which I want print time slot for 1 hour that is from i To i+1 and color it according to the availability.
I am newbie to django and can't figure out the way to make a calendar and that is why I am printing time values in HTML template.
Is there any other way to make this page and also is printing 'i+1' possible.
Views.py
#login_required
def slots(request):
k={ 'pro':range(8,17)
}
return render(request, 'login/slots.html',k)
slots.html
{% for i in pro %}
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td id="demo">{{ i }} to {{ i+1 }}</td>
</tr>
{% endfor %}
I know this might be a silly question but I am not able to figure it out,
any help is appreciated.
There is a built-in filter in Django template called add. You can add something to a value in templates using {{ value|add:"2" }}.
In this case specifically, try:
<td id="demo">{{ i }} to {{ i|add:"1" }}</td>
Related
In a django template, I'm accessing objects in a list filtered by the objects' datetime.
Here's a current image of the object_list onto an HTML table:
The time part of the datetime ("11:50a.m") must remain regardless of duplicates.
However, the date part of the datetime ("September 9"), must appear only once for each unique date.
Essentially, what I'm looking for would look something like:
How would I go about achieving this effect? I've tried using {% if forloop.first %}, but I'm unable to find a way to target all "firsts" of unique dates.
This is the current code which corresponds to the first image:
{% for event in object_list %}
<tr>
<td>{{ event.datetime|date:"F j g:ia e" }}: </td>
<td>{{ event.venue }}</td>
</tr>
{% endfor %}
I've also considered not using the date of the datetime object and manually coding the dates in the HTML, but wouldn't know how to eventually tie the DOM date to the object_list according to date.
That is exactly what the ifchanged tag is for.
I would split your output into two parts, and use ifchanged on the date only.
<tr>
<td>{% ifchanged %}{{ event.datetime|date:"F j" }}{% endifchanged %}</td>
<td>{{ event.datetime|date:"g:ia e" }}: </td>
<td>{{ event.venue }}</td>
</tr>
Here i put my code file so you can get idea about my code
In my index.html
{% load tag %}
<body>
<select name="select_year">
<option>2017</option>
<option>2018</option>
<option>2019</option>
<option>2020</option>
</select>
</br>
</br>
<table border="1">
<tr>
<td>No</td>
<td>Name</td>
<td>DOB</td>
<td>Year</td>
</tr>
{% for Manvi_User in ordering %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{ Manvi_User.first_name }}</td>
<td>{{ Manvi_User.dob}}</td>
<td>{{year|calculate_year:forloop.counter0}}</td>
</tr>
{% endfor %}
</table>
</body>
in vies.py
def index(request):
all_user = Manvi_User.objects.all()
ordering = all_user.order_by('dob')
return render(request, 'index.html', {'ordering': ordering})
in tag.py
#register.filter
def calculate_year(year, loop_counter):
try:
year = int(2017)
except ValueError:
return None
else:
return str(year + (loop_counter // 2))
in year i put static year using tag.py
if user select 2019 i want to display only data who have year 2019
As far as I understand what you're trying to do here, which is to filter the queryset depending on the year selected - the way you started is not really the way to do it.
You should look into django-filter app which makes it pretty easy to accomplish what you're trying here.
For this specific situation, you'd have to define your custom filter within filters.py, and then use that filter in your view.
i need some help in following situation.
After a successful searching for a user, i want to show the users information. It's a m2m flied, but I'm getting all objects from this model.
I do not know, how to filter users information.
Got this template:
{% for player in players %}
<tr>
<td>{{ player.last_name }} <span class="text-muted">({{ player.first_name }})</span></td>
<td>{{ player.gender }}</td>
<td>
{% for choice in search.league %}
<div class="">
{{ choice }}
</div>
{% endfor %}
</td>
This way it is showing all objects in League.
The field league is a M2M field from player.
I have this in my forms.Form
league = forms.ModelMultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, queryset=League.objects.all())
I realize that I must also send the users-information from the view, but I do not know how.
Thanks for helping.
I would say that you want to use something like {% for choice in player.league.all %} in your template and use it in your for-loop. You can find more information about this here: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.related_name
This question is a follow-up to the question I posted and the answers it received here: How to change the layout for templates in a Django Form-Wizard?
I have made a 4-step form wizard. I have connected up 4 custom templates corresponding to each of the forms. Each such template extends a template named form_base.html.
I want to customize the layout on each of these forms. So I need to manipulate the form elements individually. One of the elements on the first form is named "comment1". So in the template, I have:
<table>
<tr>
<th align="right">
Comment #1
</th>
<td>
{{ wizard.form.comment1 }}
</td>
</tr>
</table>
This partially works. It works fine if the user types a valid entry in that field. However if the user types invalid data, the error message does not appear. How can I fix this to show the error message properly?
The docs go over this thoroughly (https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#customizing-the-form-template). You can access and display the form errors and field errors by accessing them directly off the form and fields respectively. Here is an example.
{{ wizard.form.non_field_errors }}
<table>
<tr>
<th align="right">
Comment #1
</th>
<td>
{{ wizard.form.comment1 }}
</td>
{% if wizard.form.comment1.errors %}
<td>{{ wizard.form.comment1.errors }}</td>
{% endif %}
</tr>
</table>
Is it possible to render each checkbox individually, instead of it having to clump all the checkboxes together in a list, as it does by default? Something like
{{ myform.cbmultiple.0 }}
To render just the first checkbox? Actually the 0 would have to be a variable so I can loop...
The reason I'm asking is because I want to display these checkboxes in a rather complicated way, so the default widget doesn't work for me. I also don't really want to override the widget because it's much easier to render it using the template syntax than in python code, plus, that's a lot of work for a one-off.
No you can't do that because the whole HTML is generated by the widget's render method at once. You could only create your own widget class and override the render method so that it produces the HTML in a fashion that suits your purpose!
There is a way to render the CheckboxSelectMultiple() manually in the template so you can do what you want with it.
Take a look at this post for details.
The solution could be something like this:
<table>
<thead>
<tr>
<td> </td>
<td>V</td>
<td>S</td>
</tr>
</thead>
{% for pk, choice in form.options.field.widget.choices %}
<tr>
<td>{{ choice }}</td>
<td><label for="id_options_{{ forloop.counter0 }}"><input {% for option in app.options.all %}{% if option.pk == pk %}checked="checked"{% endif %}{% endfor %} type="checkbox" id="id_options_{{ forloop.counter0 }}" value="{{ pk }}" name="options" /></label></td>
</tr>
{% endfor %}
</table>