I'm trying to customize how my form is displayed by using a form_snippet as suggested in the docs. Here's what I've come up with so far:
{% for field in form %}
<tr>
<th><label for="{{ field.html_name }}">{{ field.label }}:</label></th>
<td>
{{ field }}
{% if field.help_text %}<br/><small class="help_text">{{ field.help_text }}</small>{% endif %}
{{ field.errors }}
</td>
</tr>
{% endfor %}
Of course, field.html_name is not what I'm looking for. I need the id of the input field. How can I get that?
Also, is there a way I can determine if the field is required, so that I can display an asterisk beside the label?
Found both answers here. My new script looks like this:
{% for field in form %}
<tr>
<th>{% if field.field.required %}*{% endif %}<label for="{{ field.auto_id }}">{{ field.label }}:</label></th>
<td>
{{ field }}
{% if field.help_text %}<br/><small class="help_text">{{ field.help_text }}</small>{% endif %}
{{ field.errors }}
</td>
</tr>
{% endfor %}
Stupid incomplete docs :\
{{field.label_tag}} should have what you're looking for to populate the 'for' attribute on the label.
You might want to try {{field.required}} and see if it works, I seem to remember something like that in my own forms.
http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
You can get the input field's id attribute like this: {{ field.auto_id }}
Related
I have the following code on a page:
PHP code section
function onSelecaoGE(){
$this['temp'] = post('selecaoge');
}
and on page html
<div class="col ptxt1">
<select class="form-control"
name="selecaoge"
data-request="onSelecaoGE"
data-request-update="_foe_rec_nivel: '#selnivel'"
id="escolhage">
<option disabled selected> -- Escolha um Grande Edifício --</option>
{% for ge in ge_all %}
{% if ge.era_id == 0 %}
{% set conta = ge.recompensa_ge_conta2[0].count %}
{% else %}
{% set conta = ge.recompensa_ge_conta[0].count %}
{% endif %}
<option value="{{ ge.id }}">{{ ge.nome }} ({{ conta }})</option>
{% endfor %}
</select>
</div>
<div class="col ptxt1" id="selnivel">
{% partial "_foe_rec_nivel" %}
</div>
and on partial just have
{{ temp }}
also i have jquery loaded and {% framework extras%} in layout file.
Everything looks good as per documentation and some examples i found.
But when i make any selection on the select it starts the ajax call, but it never ends.
After several changes i can't figure out what is wrong. Any help will be appreciated.
TIA
JL
With the help of "alxy" on Slack we found that the problem is related with the version of jquery loaded. I'm loading jquery-3.3.1.slim.min.js that appears to not have complete ajax support. I changed to load the regular version jquery-3.3.1.min.js and is working perfectly.
JL
I want to show data in order of column where the num are given in octobercms,
default.htm
{% set post = __SELF__.post %}
<div class="content">{{ post.content_html|raw }}</div>
{% if post.featured_images.count %}
<div class="featured-images text-center">
{% for image in post.featured_images %}
<p>
<img
data-src="{{ image.filename }}"
src="{{ image.path }}"
alt="{{ image.description }}"
style="max-width: 100%" />
</p>
{% endfor %}
</div>
{% endif %}
<p class="info">
Posted
{% if post.categories.count %} in
{% for category in post.categories %}
{{ category.name }}{% if not loop.last %}, {% endif %}
{% endfor %}
{% endif %}
on {{ post.published_at|date('M d, Y') }}
</p>
<p class="info">
{% for blocks in post.blocks %} // here the output of this loop must show in order which are given in column in same table which is relation_sort_order
<b> {{ blocks.title}}</b>
{{ blocks.content_html|raw }}
{% endfor %}
</p>
how i can show the blocks in number in relation_sort_order field
Assuming that the post variable is a custom Model object, and blocks is a relation on that object, the best way to do what you are asking to is to update the relationship definition in the model class that defines your post object.
You didn't include the code for that, but it probably looks something like this:
public $hasMany = [
'blocks' => 'Acme\Blog\Models\Block'
]
You can update that to include a sort order by default, see this section of the OctoberCMS documentation
But in short, you would change the above to this:
public $hasMany = [
'blocks' => [
'Acme\Blog\Models\Block',
'order' => 'relation_sort_order asc'
]
];
Then anytime in you accessed the blocks relationship from your Post class, it would automatically sort it in that order.
My template is, my context variable is zipped into three different variables
{% extends "question/base_home.html" %}
{% load staticfiles%}
{% block questions %}
{% for result, prev_answers, answerForm in context.zipped_results%}
<form action="/q/answer/" method="post" style="display:none;" class="answer_form" >
{% csrf_token %}
{% if forloop.counter == 1 %}
{# below code did not work #}
{{ context.zipped_results.2.management_form }}
{% endif %}
{# neither this #}
{# {{ context.zipped_results.2.management_form }} #}
{# this worked by but it disturbs the way I display forms #}
{# {{ context.answersFormset}} #}
{{ answerForm.as_p }}
{# {{ answerForm.management_form }} #}
<input type="hidden" name="q_id_a" id="q_id_a" value="{{ result.object.id }}">
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
{% endfor %}
{% endblock questions %}
and my post view is
def answer(request):
if request.method=='POST':
form=AnswerForm(request.POST)
answerFormset=formset_factory(AnswerForm)
formset=answerFormset(request.POST)
if formset.is_valid():
for form in formset:
print form
answer=form.cleaned_data['answer']
q_id=request.POST.get("q_id_a", "")
a=Answers.objects.create(questions_id=q_id,answer=answer, time_stamp=datetime.datetime.now(), upvote=0, downvote=0)
return HttpResponse("your answer is posted succesfully")
else:
return HttpResponse("somethign went wrong while posting the answer")
I followed the documentation mentioned here but i always end up with this error. The only case when I am not getting this error is when i use {{ context.zipped_results.2 }}, as per the document when formset is displayed this say management_form need not be used in template. I think the issue is with the management_form. I am not able to figure out how to use it.
I am zipping the results in context variable as context={'results':results, 'form':form, 'zipped_results':zipped_results, 'answersFormset':answersFormset}
I can't seem to get some very basic ruby to work within Jekyll. I have an ultra-simple loop to get my posts from inside /_posts :
<ul>
{% for post in site.posts %}
<li>
<a href="{{ post.url }}">
{{ post.title }}
</a>
</li>
{% endfor %}
</ul>
The posts inside _posts are called
2013-03-16-how-i-do.md
2012-01-01-hello-world.md
Inside _config.yml, I've set permalink: pretty.
What's odd is that it doesn't seem to even iterate through that loop at all— if I add some text inside the loop, even before the li, it doesn't post show up at all.
I'm currently using ruby-2.0.0-p0 — have there been any syntax changes that might be causing this?
Thank you!
First of all are you calling {{ content }} so that Jekyll knows where to run the query?
If you are then this is how I am calling my posts
{% for post in site.posts limit: 10 %}
<a href="{{post.url}}">{{ post.title }}
{{ post.date | date:"%B %d, %Y" }}
{% endfor %}
I am working with the jekyll static site builder, and I am having
difficulty performing the following:
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
{% for post in site.categories[{{ category }}] %}
<li> {{ post.title }}</li>
{% endfor %}
↩
{% endfor %}
I have a post category in my jekyll site called 'test', and I can
display posts from it with the following:
{% for post in site.categories.test %}
<li> {{ post.title }}</li>
{% endfor %}
However, i want to build an archive page automatically, and in order
to do this, I need to embed the category from the outer loop (the loop
that visits all the categories), and use it inside the inner loop to
access posts from that specific category. What do I need to do to get
the first snippet to work how I want it to?
EDIT: Alternatively, is there another way to get the results that I want?
When you do for category in site.categories ,
category[0] will give you the category name,
category[1] will give you the list of posts for that category.
That's the way Liquid handles iteration over hashes, I believe.
So the code you are looking for is this one:
{% for category in site.categories %}
<h2 id="{{ category[0] }}-ref">{{ category[0] }}</h2>
<ul>
{% for post in category[1] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
<p>↩</p>
{% endfor %}
I've taken the liberty of fixing some markup issues - I've added <ul>...</ul> around the post link list, a <p> around the last link, a semi-colon after the 8617, and also fixed the id at the top (was missing the -ref part).
Regards!
How about...
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
<ul>
{% for post in site.posts %}
{% if post.category == category[0] %}
<li> {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
↩
{% endfor %}
Sure, it's pretty inefficient and generates a bunch of extra whitespace, but it gets the job done.
[The original was missing the tags. Just added them. Also, to get ride of the whitespace, one can collapse everything from for post in site.posts to endfor onto a single line.]
{% for post in site.categories.category %}
- OR -
{% for post in site.categories.category[0] %}
Also, I'm not sure why kshep's example doesn't work...