Django-bootstrap3 error "should contain a valid Django Form" - django-forms

I was learning the basics of django-bootstrap3. I am getting this error when I run the code:
Parameter "form" should contain a valid Django Form.
My code snippet is as follows:
{% load bootstrap3 %}
<form action="/detail.html/myForm" method="post" class="form">
{%csrf_token %}
{% bootstrap_form myForm %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% endbuttons %}
</form>
I am confused between the meaning of form action and {% bootstrap_form myForm %}

action in your form defines the url where it will be directed once you click on submit button. i.e. in your example, once you click submit button, it will be directed to "/detail.html/myForm" with a POST request. And the view handling this url will respond accordingly.
Whereas, your {% bootstrap_form myForm %} will load the your form in your template.

Related

Loop through liquid advanced custom field to return multiple metafield values

I've built the following liquid for loop to retrieve & output data from a repeating advanced custom field in Shopify. The ACF namespace is faq, and contains heading and content data. My current loop is as follows:
<div class="feed-faqs">
{% if page.metafields.faq != blank %}
{% assign faqs = page.metafields.faq %}
{% for item in faqs %}
{% assign i = forloop.index %}
<div class="item item--{{ i }}">
{{ heading[i] }}
{{ content[i] }}
</div>
{% endfor %}
{% endif %}
</div>
However, on the frontend, this loop returns the following:
<div class="feed-faqs">
<div class="item item--1">
</div>
<div class="item item--2">
</div>
</div>
Is what I'm trying to achieve (to output multiple values from a repeating ACF field) possible with this approach, and if so, where have I gone wrong in fetching the header & content data?
Worked it out, so leaving this answer for anyone else in the future:
<div class="feed--faqs">
{% if page.metafields.faq != blank %}
{% assign faqs = page.metafields.faq.heading %}
{% for value in faqs %}
{% assign i = forloop.index0 %}
<div class="item item--{{ i }}">
<h4>{{ page.metafields.faq.heading[i] }}</h4>
<p>{{ page.metafields.faq.content[i] }}</p>
</div>
{% endfor %}
{% endif %}
</div>
Metafield value type is set to 'Json String'.
For reference, I'm using the ArenaCommerce Advanceds Custom Fields app: https://apps.shopify.com/advanced-custom-field.

Octobercms select with ajax not working as expected

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

sorting front matter from markdown file

i'm fairly new to jekyll, but i'm really loving the things you're able to do with it, and the speed for deployment.
so, my site is managed with siteleaf and deployed to github pages. right now, i'm working to build out pages to showcase my past projects, and i need to be able to create dynamic content- have the ability to add multiple meta fields with the same names i.e. multiple fields called center_image or text_block that i can display based on their sort order in the admin panel/markdown file that is creating the page. here's the code i've written so far, it's currently showing the content correctly, but it doesn't allow me to 1. have multiple iterations of the same meta field or 2. sort the content dynamically.
{% for photo in page.project_content.full_width_image %}
<div class="fullwidth-image content-block">
<img src="{{ photo }}">
</div>
{% endfor %}
{% for photo in page.project_content.center_image %}
<div class="center-image content-block">
<img src="{{ photo }}">
</div>
{% endfor %}
{% for photo in page.project_content.split_left_image %}
<div class="split-image-left content-block">
<img src="{{ photo }}">
</div>
{% endfor %}
{% for photo in page.project_content.split_right_image %}
<div class="split-image-right content-block">
<img src="{{ photo }}">
</div>
{% endfor %}
{% for content in page.project_content.text_content %}
<div class="text-block content-block">
{{ content | markdownify }}
</div>
{% endfor %}
so, to sum up, i need to be able to sort this content and also include multiple iterations of the same content type. does anyone have any ideas?
thanks!
jesse
after contacting support, here's what i found.
first, i needed to change the YAML front matter from being key fields to object fields. this creates an array of content, like so
project_content:
- type: full-width-image
image: "/uploads/hero.jpg"
- type: text
body: This is another test content block.
- type: text
body: text two.
then, my markup is like so...
{% for block in project.content %}
{% case block.type %}
{% when 'full_width_image' %}
<img src="{{ block.image }}">
{% when 'text' %}
{{ block.text | markdownify }}
{% endcase %}
{% endfor %}
and my output becomes:
<div class="full-width-image content-block">
<img src="/uploads/hero.jpg">
</div>
<div class="text content-block">
<p>This is another test content block.</p>
</div>
<div class="text content-block">
<p>text two.</p>
</div>
this is exactly what i needed to do, and i can change the sort order of the items in my siteleaf admin!

Django1.10 - /i18n/setlang/ CSRF token missing or incorrect

After selecting a language in a language switcher form, I got a CSRF error:
The form is in base.html
<form action="{% url 'set_language' %}" method="post" class="form-inline">
{% csrf_token %}
<input name="next" type="hidden" value="" />
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-globe" aria-hidden="true"></i></div>
<select name="language" class="form-control" id="lang-switcher">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
</div>
</div>
</form>
I submit the form with JS:
jQuery('#lang-switcher').change(
function(){
jQuery(this).closest('form').trigger('submit');
});
my template file art.html is as follow:
{% extends "base.html" %}
{% load staticfiles %}
{% load i18n %}
{% block content %}
....
{% endblock %}
views.py is as follow:
from django.template import RequestContext
from django.shortcuts import render_to_response
from .models import Exhibition, Picture
def index(req):
highlight = Exhibition.objects.latest()
exhibitions = Exhibition.objects.all()[1:]
return render_to_response('art.html', RequestContext(req,{'highlight': highlight, 'exhibitions': exhibitions}))
urls.py is as follow:
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
url(r'^art/', index),
url(r'^$', views.flatpage, {'url': '/home/'}, name='home')
]
I can load the page w/o problems, then when I submit the form to switch language I get the CSRF error.
edit
I must add that the form works well when I'm on root.
I finally solved it by using a different render function in my view:
return render(req, 'art.html', {'highlight': highlight, 'exhibitions': exhibitions})

how to handle formset in django post view. error: ManagementForm data is missing or has been tampered with

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}

Resources