I am a newbie to Django and web-development in general so be patient for maybe a very dumb question :)
I have a form generated from a model and in this form I have about 20 checkboxes. Now they are aligned in one long column and it looks not very nice from a UI point of view. I'd like this column to be split in several ones but still have this form be automatically generated from the model. What would you suggest me to do?
In the generated HTML individual checkboxes look like this:
<li><label for="id_boxes_0"><input type="checkbox" name="boxes" value="1" id="id_boxes_0" /> some name</label></li>
You don't need to change anything in Python code, but you'll need to layout the form in the template instead of using {{ form.as_ul }}. You can iterate over the form to get the fields. For the simplest possible approach, something like the following could put twenty fields in two columns of ten:
{% for field in form %}
{% ifequal forloop.counter 11 %}</ul><ul>{% endifequal %}
<li>{{ field }}</li>
{% endfor %}
Personally I never use the as_* helper methods in real code, as far as I'm concerned they're only useful for rough prototyping.
Related
i want in same line print two value look like "1. Question ...".
but first {{ }} after set new line. look like this,
"1."
"Question ..."
{% for q in question %}
<p> {{ forloop.counter }}. {{ q.question|safe }}</p>
{% endfor %}
How can i print two value in same line in template ?
I want this:
1.Question
2.Question
...
Based on your comment, you say that q.question is the content of a CKEditor. Often times, these output at least wrap the content inside a <p> tag. In this case, the result output generated by Django would a nested <p> tag inside the <p> from your template:
<p>1. <p>Question</p></p>
This is invalid HTML, but the browser tries to render it as best as it can. I think you can either include the number inside the CKEditor and exclude it from your template or change your field to store a simple CharField, and keep your HTML unchanged.
This depends on the flexibility you want in your application.
Say I have a simple model with Title, Price, Description and Image fields. I would like 3 html outputs/blocks that I can reuse for each object of this model. For added 'complexity', 2 of the outputs will be combined to make the 3rd. So, here is what these 3 output might look like in a template.
output_image
<span class="output_image"><img src="{{ object.image.url }}" alt="{{ object.title}}></span>
output_info
<ul class="output_info">
<li>{{ object.title }}</li>
<li>Price: {{ object.price }}</li>
<li>{{ object.description }}</li>
</ul>
output_all (wraps up the previous 2)
<div class="output_all">
<span class="output_image">...</span>
<ul class="output_info">...</ul>
</div>
I might want to use output_image on a search page, and output_all on listing and advanced search, for example. Hence I only want to write this output once, and reuse it. What is the best way to achieve this?
Initially I thought to add methods/properties to the model class, which would return the formatted html. This worked, and in my template I could just call {{ object.output_all }}, for example. Is it bad practice to output html like this?
I have briefly looked at other ways too. Inclusion tags seems to work, but I have only tested with a 'complete' version of the output_all case (ie have not 'nested' the other 2 output tags within output_all yet). Is this the correct method, and is it possible to 'nest' the tags?
I would use inclusion tags for this. Keep presentation logic where it belongs, in templates.
I have a model Banner with fields image and name. I need data from this model to be displayed in groups of three on a moving Bootstrap carousel.
My current implementation is the most expensive and simple one I could think of.
banner1 = Banners.objects.filter(id__exact=1) repeated
for 9 entries in the Model.
My question is that is it possible to split one queryset
Banners.objects.all() into the three groups of three entries and then how would I go about displaying the three groups across three different slides of the Bootstrap Carousel?
This seems like a good place to use the django divisibleby filter. Since you need to add the extra grouping info every three instances, it could look something like this (very abstractly, since I don't know your specific template demands):
{% for banner in banners %}
{% if forloop.counter0|divisibleby:"3" %}
<!-- your carousel wrapping code here -->
{% endif %}
<!-- code for each individual banners here -->
{% if forloop.counter0|divisibleby:"3" %}
<!-- rest of your carousel wrapping code here -->
{% endif %}
where banners would be a context variable that contains Banners.objects.all()
I have a User model and the user's have university attribute. I want users to be able to scroll through a list of universities and choose one as a button, I don't want to have a dropdown selecter thing. So I passed in the UserInfo.UNIVERSITY_CHOICES (UserInfo is a one to one with the user model) to my template and try to iterate through it using:
<form method="get" action="/newUniversity/">
{% csrf_token %}
{% for school in universityList %}
<input class='submitbtn' type="submit" name="school" value="{{ school }}"></center>
{% endfor %}
</form>
However I get a bunch of buttons that say ('Harvard','Harvard') instead of just Harvard. I tried to index the tuple by doing school[1] but that gives me an error. Is there a way to do this or am I doing it a completely wrong way?
If UserInfo.UNIVERSITY_CHOICES is tuple (like the ones you would use with the django admin) you must choose which of the values you are after:
school.0
or
school.1
Notice Django's Template Language is not Python! You can see a reference of the language here:
https://docs.djangoproject.com/en/dev/topics/templates/
I have an Area model, and I have Service model. Area is a Foreignkey of Service.
I want a template which shows each Service grouped under its respective Area, i.e.
Area 1
- service a
- service b
Area 2
- etc.
I've passed in an object list of all services to service_list.html. I have a custom tag get_areas which returns the areas, on which I can create the Area divisions, and from which I can potentially pass an area name to a service filter. But since I can't filter (can I?) in {% for service in object_list %}, how can I filter the service list in each Area's section of the HTML?
Thanks so much in advance.
If you post your models I can give you the exact code, but in general something like this should work:
# Pass in 'areas' variable from view with all required areas
{% for area in areas %}
{{ area.name }}
{% for service in area.service_set.all %} #Gets all the services associated with an area
{{ service.name }}
{% endfor %}
{% endofor %}
Not sure you even need a custom tag, but maybe I just don't understand that part.
Take a look at the regroup template tag. It was built for exactly the same purpose
https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#regroup
One thing that I have realized is that once you get the point of displaying complex interrelated data in your templates, that it makes sense to transform the data into an appropriate object (generally a list of dictionaries) before passing them onto the template.
That way, you can test your information easier, and have a much easier time displaying it. (You'll have much powerful tools at your disposition in Python based Views than in Django Templating Language).
#Maz - thank you for that. I'm learning at the moment and need to look at service_set.
#arustgi - that worked perfectly. For the benefit of fellow novices stumbling over this, I pass in 'queryset': Service.objects.all() and use:
{% regroup object_list by area as area_list %}
{% for area in area_list %}
<h2 class="separate">{{ area.grouper }}</h2>
{% for service in area.list %}
<div class="column">
<h3>{{ service.title }}</h3>
{{ service.body }}
</div>
{% endfor %}
{% endfor %}
Concise, descriptive code. Many thanks, both of you