How do you manually place forms from a formset? - django

I have a template page where it's suppose to load a form from the formset if a database query returns FALSE.
<form method="post" class="form">
{% csrf_token %}
{% for form in formset %}
{% if comparelist.item2 %}
<div class="col-3">{{comparelist.item2.name}}</div>
<div class="col-3">{{comparelist.item2.price}}</div>
<div class="col-3">{{comparelist.item2.store}}</div>
{% else %}
<div>
{{form.form-0}}
</div>
{% endif %}
{% if comparelist.item3 %}
<div class="row">
<div class="col-3">{{comparelist.item3.name}}</div>
<div class="col-3">{{comparelist.item3.price}}</div>
<div class="col-3">{{comparelist.item3.store}}</div>
</div>
{% else %}
<div>
{{form.form-1}}
</div>
{% endif %}
{% if comparelist.item4 %}
<div class="row">
<div class="col-3">{{comparelist.item4.name}}</div>
<div class="col-3">{{comparelist.item4.price}}</div>
<div class="col-3">{{comparelist.item4.store}}</div>
</div>
{% else %}
<div>
{{form.form-2}}
</div>
{%endfor%}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
But obviously {{form.form-x}} doesn't work.
So how do I insert these forms in manually?

I'm still not entirely sure what the {{form.form-2}} syntax is supposed to do. But I think what you want is to simply refer to the forms directly via their position, just like you do with the comparelist items. So remove the for loop and do that:
<form method="post" class="form">
{% csrf_token %}
{{ formset.management_form }}
{% if comparelist.item2 %}
<div class="col-3">{{comparelist.item2.name}}</div>
<div class="col-3">{{comparelist.item2.price}}</div>
<div class="col-3">{{comparelist.item2.store}}</div>
{% else %}
<div>
{{formset.forms.0}}
</div>
{% endif %}
{% if comparelist.item3 %}
<div class="row">
<div class="col-3">{{comparelist.item3.name}}</div>
<div class="col-3">{{comparelist.item3.price}}</div>
<div class="col-3">{{comparelist.item3.store}}</div>
</div>
{% else %}
<div>
{{formset.forms.1}}
</div>
{% endif %}
{% if comparelist.item4 %}
<div class="row">
<div class="col-3">{{comparelist.item4.name}}</div>
<div class="col-3">{{comparelist.item4.price}}</div>
<div class="col-3">{{comparelist.item4.store}}</div>
</div>
{% else %}
<div>
{{formset.forms.2}}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>

Related

How to display error message with Django forms?

I would like to customize the Django login authentication form. The original form looks like this and it works perfectly:
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4 ">Log In</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Login</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">
Need An Account? <a class="ml-2" href="{% url 'register' %}">Sign Up Now</a>
</small>
</div>
</div>
{% endblock content %}
My goal is to modify the form style so that I can place the objects wherever I want. I was able to achieve this for the username and password fields, however I cannot display the error message just like in the original format.
This is what I tried:
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4 ">Log In</legend>
{% if formset.non_form_errors %}
<div class="alert alert-block alert-danger">
{% if formset_error_title %}<h4 class="alert-heading">{{ formset_error_title }}</h4>{% endif %}
<ul class="m-0">
{{ formset.non_form_errors|unordered_list }}
</ul>
</div>
{% endif %}
<div class="row">
<div class="col-md-4 col-sm-12 register-field">
{{ form.username|as_crispy_field }}
</div>
<div class="col-sm-12 register-field">
{{ form.password|as_crispy_field }}
</div>
</div>
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Login</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">
Need An Account? <a class="ml-2" href="{% url 'register' %}">Sign Up Now</a>
</small>
</div>
</div>
{% endblock content %}
Basically I saw that crispy uses a object called formset.non_form_errorshowever it looks like it is not working when I insert an invalid username/password.
Would you be able to suggest a smart and elegant way to achieve my goal please? This is what it should look like:
I was able to achieve my goal with this trick:
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4 ">Log In</legend>
<div class="row">
<!-- Added this new rows -->
{% if form.errors %}
<div class="col-12 register-field">
<div class="alert alert-block alert-danger">
<ul>
<li>Please enter a correct username and password. Note that both fields may be
case-sensitive.
</li>
</ul>
</div>
</div>
{% endif %}
<!-- ------------------- -->
<div class="col-md-4 col-sm-12 register-field">
{{ form.username|as_crispy_field }}
</div>
<div class="col-sm-12 register-field">
{{ form.password|as_crispy_field }}
</div>
</div>
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Login</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">
Need An Account? <a class="ml-2" href="{% url 'register' %}">Sign Up Now</a>
</small>
</div>
</div>
{% endblock content %}
Basically I check if there are any errors and then I create a div with the custom error message.

styling form render in Django

I'm using Django 2.x
I'm using Formset to be able to add multiple records at one.
the form is rendered in template file as
{{ chapter_questions.as_table }}
Which renders template like below which looks ugly.
I'm using Bootstrap template to design my template to look like
To render template like second image, I'm writing the fields manually as
<div class="form-group">
<div class="col-sm-12 col-xs-12">
<label for="question">Word</label>
<input name="chapterquestion_set-0-word" type="text" class="form-control border-color-2" placeholder="Word" id="question">
{% if chapterquestion_set.word.errors %}
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger">
<ul>
{% for error in chapter_questions.word.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}
</div>
<div class="col-sm-12 col-xs-12">
<label for="definition">Definition</label>
<input name="chapterquestion_set-0-definition" type="text" class="form-control border-color-3" placeholder="Definition" id="definition">
{% if chapter_questions.definition.errors %}
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger">
<ul>
{% for error in chapter_questions.definition.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}
</div>
<div class="col-sm-12 col-xs-12">
<label for="audio"></label>
<input name="chapterquestion_set-0-audio" type="text" class="form-control border-color-4" placeholder="Audio" id="audio">
{% if chapter_questions.audio.errors %}
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger">
<ul>
{% for error in chapter_questions.audio.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}
</div>
</div>
This way I have to write the code multiple times for multiple objects. Also for adding a new object fields, I need to replicate whole code using JavaScript by replacing index number (0 in this case) by updating by 1.
How can I style the form widget so that I could take benefit of Django automatic field insertions and error display and only need {{ chapter_questions.as_bootstrap_div }} to render bootstrap form.
templates/fields/text-field.html
<div class="col-sm-12 col-xs-12">
{{field.label_tag}}
<input name="{{field.name}}" type="text" class="form-control border-color-2" placeholder="{{field.label}}" id="{{field.name}}">
{% if field.errors %}
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger">
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}
</div>
on your form
{% with chapterquestion_set.word as field %}
{% include 'fields/text-field.html' %}
{% endwith %}

Django fontsize tinymce

I am trying to use django tinymce in my project but the font is too small. I have googled it and learned I am meant to use content_css but without proper step by step approaches as to how exactly this should be done.
I am wondering if there was another way and if there isn't, could someone give me a simple step by step approach to solving it using the content_css.
Below is the forms.py
class PostForm(forms.ModelForm):
text = forms.CharField(help_text='Enter your Post here', widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
name = forms.CharField(widget=forms.HiddenInput(), initial='User')
created_on = forms.DateTimeField(widget=forms.HiddenInput(), initial=timezone.now())
class Meta:
model = Post
fields = ('title', 'text',)
{% extends 'blog/base.html' %}
{% load staticfiles %}
{% block body_block %}
<!-- <h1>TinyMCE Quick Start Guide</h1>
<form method='post'>
<textarea id = 'mytextarea'>Hello, World!</textarea>
</form> -->
{% if post %}
<div class="single">
<div class="container">
<div class="col-md-8 single-main">
<div class="single-grid">
<h4>{{ post.title|safe }}</h4>
<img src="{% static 'images/post1.jpg' %}" alt=""/>
<p>{{ post.text|safe }}</p>
</div>
<div class="comments">
<h2><u>Comments</u></h2>
{% if comments %}
{% for comment in comments %}
<h3>~{{ comment.commenter.first_name|title}} {{comment.commenter.last_name|title }}</h3>
<ul>
<li>
{{ comment.text|safe }}
</</li><br>
</ul>
<span class="hidden-xs"style="margin-left:70%;, font-family:Arial">Published: {{ comment.created_on }}</span >
{% if comment.commenter == request.user or user.is_superuser %}
<button style="margin-left:90%;,line-height: .9;color: red;, font-family:Arial;" type="button" name="button">Delete</button>
{% endif %}
{% endfor %}
{% else %}
No comments available
{% endif %}
</div>
<div class="content-form">
<h3>Leave a comment</h3>
{% if user.is_authenticated %}
<form id="comment_form" action="{% url 'blog:detail' post.slug %}" method="post">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.errors }}
{{ field }}<br/><br/>
{% endfor %}
<input class="btn btn-primary" type="submit" name="submit" value="submit">
</form>
{% else %}
You must be logged in to comment
{% endif %}
</div>
<ul class="comment-list " >
<h5 class="post-author_head">Written by {{ post.author.first_name|title }} {{ post.author.last_name|title }}</h5>
<li><img src="{% static 'images/avatar.png' %}" class="img-responsive" alt="">
<div class="desc">
<p>View all posts by: {{ post.author.first_name|title }} {{ post.author.last_name|title }}</p>
</div>
<div class="clearfix"></div>
</li>
</ul>
</div>
<div class="col-md-4 side-content">
<div class="recent">
<h3>RECENT POSTS</h3>
{% if recent_posts %}
<ul>
{% for post in recent_posts %}
<li>{{post.title|title }}</li>
{% endfor %}
</ul>
{% else %}
<li>No post has been posted</li>
{% endif %}
</div>
<div class="comments">
<h3>RECENT COMMENTS</h3>
{% if recent_comments %}
<ul>
{% for comment in recent_comments %}
<li>{{comment.commenter|title}} on {{comment.post|title}}</li>
{% endfor %}
</ul>
{% else %}
<li>No comments at the moment</li>
{% endif %}
</div>
<div class="clearfix"></div>
<div class="archives">
<h3>ARCHIVES</h3>
<ul>
<li>October 2013</li>
<li>September 2013</li>
<li>August 2013</li>
<li>July 2013</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
</div>
{% if comment.commenter == request.user or user.is_superuser %}
<button style="margin-left:90%;,line-height: .9;color: red;, font-family:Arial;" type="button" name="button">Delete Post</button>
<button style="margin-left:90%;,line-height: .9;color: red;, font-family:Arial;" type="button" name="button">Edit Post</button>
{% endif %}
{% else %}
asadsh
{% endif %}
{% endblock %}
To add content css files to tinymce, you have to change the tinymce.init object value to include your content_css.
Search for the initialization call in your script and add a line to your object like in this example:
tinymce.init({
...
content_css: [
'//example.com/js/your-css-here.css'
],
...
});
If a content_css part is already present, just add the URL to the array, i.e.
['url 1 here', 'url 2 here', 'your new url here']
In your custom css file, you can now set your desired font size, i.e.
body { font-size: 14px; }

Django template row alternate for multiple content

I've a template design like this
<div class="row">
<div class="col-sm-8"></div>
<div class="col-sm-4"></div>
</div>
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-8"></div>
</div>
and I've tried using this article django template rows of multiple items
But output doesnt come as required. How can I do as required.
What I've tried:
{% for item in items %}
<div class="row">
{% if forloop.counter|divisibleby:2 %}
<div class="col-sm-4"></div>
{% else %}
<div class="col-sm-8"></div>
{% endif %}
</div>
{% endfor %}
Your code produce one row per item. If I'm correct, you want 2 items per row.
{% with items_length = items|length%}
<div class="row">
{% for item in items %}
<div class="col-sm-{% cycle '4' '8'%}"></div>
{% if forloop.counter|divisibleby:2 and forloop.counter < items_length %}
</div>
<div class="row">
{% endif %}
{% endfor %}
</div>
{% endwith %}
Something like that should work.
The following is one way to do it:
{% for item in items %}
<div class="row">
{% if forloop.counter0|divisibleby:2 %}
<div class="col-sm-8"></div>
<div class="col-sm-4"></div>
{% else %}
<div class="col-sm-8"></div>
<div class="col-sm-4"></div>
{% endif %}
</div>
{% endfor %}
Notice the use of forloop.counter0.

MultiValueDictKeyError

I have the following code. The first works, however the second throws a "MultiValueDictKeyError". I've tweaked the code around a bit but I haven't been able to fix it. Any and all help is greatly appreciated!
Working Code:
{% extends "base.html" %}
{% block base_content %}
<link rel="stylesheet" href="/media/themes/txt/css/employee_summary/report.css" />
<form id="myForm" method="post">
{% csrf_token %}
{% for field in form %}
<div class=box>
<div class="row">
<div class="2u">
<h1>{{ field.label_tag }}:</h1>
</div>
<div class="10u">
{{ field }}
</div>
</div>
</div>
{% endfor %}
{% for formset in inlines %}
<div class=box>
{% for form in formset %}
<div class="row">
<div class="12u">
<h1>{{ form }}</h1>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
{% for formset in inlines %}
{{ formset.management_form }}
{% endfor %}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
Non-Working Code:
{% extends "base.html" %}
{% block base_content %}
<link rel="stylesheet" href="/media/themes/txt/css/employee_summary/report.css" />
<div id="main-wrapper">
<div class="strongborder">
<div id="main" class="container boldtext">
<form id="myForm" method="post" class="12u">
{% csrf_token %}
{% for field in form %}
<div class="row">
<div class="2u">
<h1>{{ field.label_tag }}:</h1>
</div>
<div class="10u">
{{ field }}
</div>
</div>
{% endfor %}
{% for formset in inlines %}
{% for form in formset %}
<br>
<br>
<h1>{{ form.instance.form_name }}</h1>
{% for field in form %}
<div class="row">
{% if field.label != "Employee" and field.label != "Id" and field.label != "Delete" %}
<label class="2u">{{ field.label }}:</label>
<div class="10u">{{ field }}</div>
{% endif %}
</div>
{% endfor %}
{% endfor %}
{% endfor %}
{% for formset in inlines %}
{{ formset.management_form }}
{% endfor %}
<br>
<br>
<input type="submit" value="Save Changes">
</form>
</div>
</div>
</div>
{% endblock %}
Solved by myself: for some reason it wasn't liking the ID skip in
{% if field.label != "Employee" and field.label != "Id" and field.label != "Delete" %}
... yet it was fine with skipping the employee and delete fields.