Included page doesn't display its full content - python

I wanna include a page called group_sharer.html in another page:
{% block content %}
<script type="text/javascript" src='/static/media/js/group_sharer.js'></script>
<form action="." method="POST" id="my-form">
{% csrf_token %}
<select name="campaign" id="">
{% for campaign in campaigns %}
<option value="{{campaign.id}}">{{campaign.title}}</option>
{% endfor %}
<option value="test">test</option>
</select>
<input type="submit">
</form>
{% endblock content %}
Ive tried to include it to the home page with
{% include "group_sharer.html" %}
but its didn't delivered with its full data
What should i do know to implement the data at Group_sharer.html to the home page .

Your code is correct, the thing that is missing is campaigns,
Since you require these you also need to pass this context into the template thats including it.
From the docs:
An included template is rendered within the context of the template that includes it.

Related

Django: Extent with snippets or better solution

I have the following template:
{% extends "account/base.html" %}
{% load i18n %}
{% load account %}
{% load crispy_forms_tags %}
{% block head_title %}{% trans "Password Reset" %}{% endblock %}
{% block content %}
<div class="container">
<div class="row justify-content-center">
<div class="col-8">
<h1>Reset your password</h1>
{% if user.is_authenticated %}
{% include "account/snippets/already_logged_in.html" %}
{% endif %}
<p>{% trans "Forgotten your password? Enter your e-mail address below, and we'll send you an e-mail allowing you to reset it." %}</p>
<form method="POST" action="{% url 'account_reset_password' %}" class="password_reset">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Reset My Password</button>
</form>
<p><em>{% blocktrans %}Please contact us if you have any trouble resetting your password.{% endblocktrans %}</em></p>
</div>
</div>
</div>
{% endblock %}
account/base.html contains this here:
{% extends "base.html" %}
base.html then contains tags etc. I am currently struggling with finding the best solution while considering DRY. In my template I have written:
<div class="container">
<div class="row justify-content-center">
<div class="col-8">
[more text]
</div>
</div>
</div>
I have several of these templates and in each of them I am currently writing the same ... I am now wondering is the right solution to include these opening / closing div-tags in two files and then add
{% include "div-open.html" %}
[more text]
{% include "div-closing.html" %}
Or is there any better solution to this? It doesn't feel right, that's why I am asking you now how you would solve this.
I depends if HTML repetition should be considered as DRY violation. I you have custom form fields which you want to include, than having {% include 'simple_custom_field.html' with field=form.some_fied %} where
simple_custom_field.html
<input
type="{{ field.field.widget.input_type }}"
class="form-control {{ field.field.widget.attrs.class }} {{ class }}"
id="{{ field.id_for_label }}"
name="{{ field.html_name }}"
placeholder="{{ field.label }}"
{% if field.value is not None %} value="{{ field.value }}"{% endif %}>
or like that...
is good idea, or you can render whole form like that also if you have long blocks or separate blocks of HTML in multiple files, then go for it. But its bad idea for including simple and fundamental parts of HTML. It should be transparent for the programmer.
If you want to do it another way, there are ways how to add custom template tags and "modify" the language you write templates in.
Here is docs how to do it: Writing custom template tags | Django docs

Invalid Endblock tag in django

I am receiving an error message when I go to one of my pages in my Django project, as it is saying that the End-block tag is invalid (asks whether I remembered to register or load). The error looks like this:
My code for this template - (login.html) - is below:
{% extends "learning_logs/base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.'</p>
{% endif %}
<form method="post" action="{% url 'users:login' %}">
{% csrf_token %}
{{ form.as_p }}
<button name="sumbit">log in</button>
<input type="hidden" name="next" value="{% extends 'learning_logs/index.html' %}" />
</form>
{% endblock content %}
I am very confused, and I am wondering whether anyone knows what the problem is?
Thanks
Milo
<input type="hidden" name="next" value="{% extends 'learning_logs/index.html' %}" />
Above line contains {% extends .... %}. To prevent being interpreted as a extends tag, use templatetag tag:
<input type="hidden" name="next" value="{% templatetag openblock %} extends 'learning_logs/index.html' {% templatetag closeblock %}" />
use {% endblock %} instead o f {% endblock content %}
for me it was wrongly auto-formatted html file. Check auto-formatting for html files.

CSS for Flask wtforms

I have a the following code for a Form that I have in my Flask application using Wtforms. I use FieldList to use two fields for one part.
class A(Form)
additional = FieldList(FormField(Additional), 'Additional', min_entries=1)
submit = SubmitField('Submit')
class Additional(Form):
choices = [('Funding Mechanism', 'Funding Mechanism'), ('Study Section Name', 'Study Section Name')]
critera = SelectField('Additional Criteria', choices=choices)
input = StringField()
The template uses wtf.quick_form:
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Grants - Find Grant{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>Specify</h1>
</div>
<div class="col-md-4">
{{ wtf.quick_form(form) }}
</div>
{% endblock %}
Currently the forms render in a squished and overlapping way like so:
How can I change the code so that it is formated in one line like below? It is a screenshot of #Niklas in Stockholm 's form from his question.
Thank you!
Since your form class A is calling class Additional as a FormField and only adding submit to the field, i added the submit button the Additional form itself and then called it in the view.
In the template, use
{{ wtf.quick_form(form, form_type="inline") }}
It outputs the page like this:
The form_type argument adds the .form-inline to the class attribute.
This is just a hack, surely your form will have more inputs than this, for that, you'll be writing the whole form template yourself.
The issue is that {{ wtf.quick_form(form) }} is calling wtf.form_field() on your FieldList additional in A instead of calling it on additional's subfields. Because of this, I don't think you will be able to use wtf.quick_form() on your particular form.
Instead, try templating your form like this:
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Grants - Find Grant{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>Specify</h1>
</div>
<div class="col-md-4">
<form class="form form-horizontal" method="post" role="form">
{{ form.hidden_tag() }}
{{ wtf.form_errors(form, hiddens="only") }}
{% for subfield in form.additional %}
{{ wtf.form_field(subfield) }}
{% endfor %}
{{ wtf.form_field(form.submit) }}
</form>
</div>
{% endblock %}
You can read more about wtf.form_field() on the Flask-Bootstrap documentation site.

Django Form View not receiving data

I am using Django Form View to split a large, single-page form into a multi-page form with separate templates for each one. I followed the documentation and other online examples, and most everything is going well.
However, the problem is that I have required fields in the form and they are failing validation because, as it turns out, the data is not being passed back to Django (as was verified by overriding 'is_valid()' and printing the form data, as well as printing {{ wizard.form.errors }} in my templates). It may be worth noting that the form worked fine before I split it up.
Below is a shortened version of the relevant code. If anyone has some pointers that would be great. I am no expert at this and the original code was not written by me, so I'll answer any questions as best I can. Thanks!:
VIEWS.PY
FORMS = [("venue", RegisterBusinessFormVenue),
("manager", RegisterBusinessFormManager),
("extras", RegisterBusinessFormExtras)]
TEMPLATES = {"venue": "registrationVenue.html",
"manager": "registrationManager.html",
"extras": "registrationExtras.html"}
class RegisterBusinessView(SessionWizardView):
file_storage = FileSystemStorage(location=os.path.join('/var/www/', 'logos'))
def get_template_names(self):
return TEMPLATES[self.steps.current]
def done(self, form_list, **kwargs):
return HttpResponseRedirect(reverse_lazy('submission_page_view'))
MODELS.PY
class RegisterBusinessFormVenue(forms.Form):
venue_name = forms.CharField(label='Venue Name', max_length=128)
class RegisterBusinessFormManager(forms.Form):
manager_first_name = forms.CharField(label='First Name', max_length=255)
class RegisterBusinessFormExtras(forms.Form):
website = forms.URLField(label="Website", required=False)
URLS.PY
url(r'^register/', RegisterBusinessView.as_view(FORMS), name='register_business_view')
TEMPLATE
{% extends "base.html" %}
{% load staticfiles %}
{% block stylesheet %}
<link rel="stylesheet" href="{% static 'css/registration.css' %}">
{% endblock stylesheet %}
{% block content %}
<div class="page-header">
<h1>Register your business</h1>
</div>
<form class="form-horizontal" role="form" action="" method="POST">{% csrf_token %}
{{ wizard.management_form }}
{{ wizard.form.errors}}
{{ wizard.form.non_field_errors}}
<legend>Event Venue Information</legend>
<div class="form-group required {% if wizard.form.venue_name.errors %}has-error{% endif %}">
<label for="inputVenueName" class="col-sm-3 control-label">{{ wizard.form.venue_name.label }}</label>
<div class="col-sm-9">
<input type="text" class="form-control"
name="{{ wizard.form.venue_name.name }}"
id="inputVenueName"
{% if wizard.form.venue_name.value %}value="{{ wizard.form.venue_name.value }}"{% endif %}
placeholder="Venue name where events are held"
spellcheck="true">
</div>
</div>
<div class="register-btn-block">
<button type="submit">Register</button>
</div>
</form>
{% endblock content %}
I answered my own question. It was a dumb mistake on my part: I wasn't using the wizard provided id's and name's for my html markup. In my template above where I have the 'label for' and 'id' attributes, they should be populated with the value '{{ wizard.form.venue_name.id_for_label }}'. For the 'name' attribute, it should be populated with '{{ wizard.form.venue_name.html_name }}'.
The pre-defined values that can be used are found here. After making these changes, then the form data was properly submitted to the server.

Django doesn't seem to detect my login.html, block problem?

I'm creating a web app with django 1.2.4.
I am using contrib.auth.views.login, I have followed every step but it seems I have forgotten something cause I don't see the login form. Here is my folder structure:
/templates/
base.html
/myapp/
object_list.html
...
/registration/
login.html
...and here is my login.html:
{% extends "base.html" %}
{% block mylogin %}
<div class="horizontal">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form action="{% url django.contrib.auth.views.login %}" method="post">
{% csrf_token %}
<div class="login_box">
<div class="login_text">{{ form.username.label_tag }}</div><div class="login_input">{{ form.username }}</div>
<div class="password_text">{{ form.password.label_tag }}</div><div class="password_input">{{ form.password }}</div>
<input id="button_login" type="submit" value="" />
</div>
</form>
</div>
{% endblock %}
...and in my base.html I have:
<div id="some_div">
{% block mylogin %} {% endblock %}
</div>
I have a basestyle.css included in base.html and the other templates inherit correctly too... it seems to be a block problem...
Any solution??
Thnak you
Instead of inserting of a block I used the include tag in base.html, just like this:
{% include "registration/login.html" %}
If you’d prefer not to call default (django provided) template registration/login.html, you can pass the template_name parameter via the extra arguments to the view in your URLconf.
For example, this URLconf line would use myapp/login.html instead:
(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}),
Reference : Django official documentation
It solves my problem. Hope this works for others.

Categories