I have two distincts template which must be extended in three diffrents page.
Is there a solution to extends a template with if block?
I tried this without success.
{% if 'project' in request.get_full_path %}
{% extends 'index.html' %}
{% elif 'network' in request.get_full_path %}
{% extends 'base.html' %}
{% elif 'zoneset' in request.get_full_path %}
{% extends 'base.html' %}
{% endif %}
{% load crispy_forms_tags %}
{% block title %} Network {% endblock title %}
{% block content %}
<div class="row">
<div class="col-md">
<div class="card card-body">
<h6>Update</h6>
<form class="" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form|crispy}}
<input type="submit" name="" value="Update">
</form>
</div>
</div>
</div>
{% endblock %}
If you work with template inheritance, {% extends … %} [Django-doc] should be the first template tag. You can not use an {% if … %} block, nor is it a good idea to detect the parent based on the full path of the request.
You can do this in the view, and work with:
def my_view(request):
context = { 'parent': 'index.html' }
return render(request, 'my_template.html', context)
and then in the template use:
{% extends parent %}
…
Related
in the route code I printed the form.validate_on_submit() output, It always comes out to be false and renders the index.html file
#app.route('/',methods=['GET','POST'])
def index():
form=RegistrationForm()
print(form.validate_on_submit())
if form.validate_on_submit():
return "validation successfull"
else:
return render_template('index.html',form=form)
here's the form class, all the impportant keywords are instantiated in the original file
class RegistrationForm(FlaskForm):
username=StringField('Username',validators=[
DataRequired(message='Username required'),
Length(min=4,max=25,message='Username must be 4 to 25 characters long')
])
password=PasswordField('Password',validators=[
DataRequired(message='Password required'),
Length(min=4,message='Password must be more than 4 characters long')
])
confirm_pswd=PasswordField('Confirm Password',validators=[
DataRequired(message='Password required'),
EqualTo('password',message='Password must Match')
])
submit=SubmitField('Submit')
here's the index.html file complete code
I've used jinja templating and displayed the form that is passed in the route
{% extends 'layout.html' %}
{% block title %} Registration {% endblock title %}
{% block content %}
<h1>GetStarted</h1>
<form action="{{url_for('index')}}" method="POST">
<div>
{{form.username.label(class='form-control-label')}}
{{form.username( class = 'form-control',
placeholder = 'Username',
autofocus = true)}}
{% if form.username.errors %}
<div>
{% for error in form.username.errors %}
<p><small>{{error}}</small></p>
{% endfor %}
</div>
{% endif %}
</div>
<div>
{{form.password.label(class='form-control-label')}}
{{form.password( class = 'form-control',
placeholder = 'Password')}}
{% if form.password.errors %}
<div>
{% for error in form.password.errors %}
<p><small>{{error}}</small></p>
{% endfor %}
</div>
{% endif %}
</div>
<div>
{{form.confirm_pswd.label(class='form-control-label')}}
{{form.confirm_pswd( class = 'form-control',
placeholder = 'Retype Password')}}
{% if form.confirm_pswd.errors %}
<div>
{% for error in form.confirm_pswd.errors %}
<p><small>{{error}}</small></p>
{% endfor %}
</div>
{% endif %}
</div>
<div>
{{form.submit}}
</div>
</form>
{% endblock content %}
You need add:
<form action="{{url_for('index')}}" method="POST">
{{ form.hidden_tag() and form.csrf_token }}
** for some reason my form is not showing in the template can someone tell me why ,
think you.
**
views.py
i think the problem is here but i cant find it i followed the documentation and got nothing also
def contact(request):
print(request.POST)
forms=contactform(request.POST or None)
if forms.is_valid():
print(forms.cleaned_data())
context= {
"title":"contact",
"form": forms
}
return render(request,"form.html",context)
```
> forms.py
```
from django import forms
class contactform(forms.ModelForm):
full_name=forms.CharField()
email=forms.EmailField()
text=forms.CharField(widget=forms.Textarea)
```
> form.html
```
{% extends "gta.html" %}
{% block ferr%}
{% if title %}
<h1>{{title}}</h1>
{% endif %}
<form method="POST" action="."> {% csrf_token %}
{{forms.as_p}}
<button type="submit">submitt</button>
</form>
{% endblock %}
This should sort you.
{% extends "gta.html" %}
{% block ferr%}
{% if title %}
<h1>{{title}}</h1>
{% endif %}
<form method="POST" action="."> {% csrf_token %}
{{form.as_p}}
<button type="submit">submitt</button>
</form>
{% endblock %}
I'm calling a template in extended template.
I can't not find 'how to extended template call the other template in jinja2?'
base.html
<div class="main-container">
{% block content %}
{% endblock %}
</div>
child.html
{% extends "base.html" %}
{% block content %}
<div class="child">
<button class="modal"/>
{% modal content %}
{% endblock %}
</div>
{% endblock %}
modal content.html
<div class="modal-content">
...some code
</div>
I want call modal content in child.html.
If you're meaning you want to add the content.html into the child.html page, use include
Within child.html:
{% include 'content.html' %}
On DetailView template I am not able to print name,date of post only '|' sign is being printed but on ListView it is working fine. Here is code written in files. Thanks!!
blog/urls.py
urlpatterns = patterns('',url(r'^$', ListView.as_view(queryset =
Blog.objects.all(), template_name ='blog.html')),
url(r'^(?P<pk>\d+)/$', DetailView.as_view(model=Blog, template_name='detail.html')), )
blog.html
{% extends 'base.html' %}
{% block content %}
{% for post in object_list %}
<ul> {{ post.name }} ||{{ post.date }}</ul>
{% endfor %}
{% endblock %}
detial.html
{% extends 'base.html' %}
{% block content %} <h2> <a href="/blog/{{ post.id }}">{{ post.name }}
</a>||{{ post.date }}</h2> {% endblock %}
context_object_name Designates the name of the variable to use in the context. Default is "object"
Try
{% extends 'base.html' %}
{% block content %} <h2> <a href="/blog/{{ object.id }}">{{ object.name }}
</a>||{{ object.date }}</h2> {% endblock %}
See SingleObjectMixin and making-friendly-template-contexts
The django contrib comments form i'm using:
{% get_comment_form for post as form %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
{% if next %}
<div><input type="hidden" name="next" value="{{ next }}" /></div>
{% endif %}
{% for field in form %}
{% if field.is_hidden %}
<div>{{ field }}</div>
{% else %}
{% if field.name == 'comment' %}
{% if field.errors %}{{ field.errors }}{% endif %}
<p
{% if field.errors %} class="error"{% endif %}
{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
{{ field.label_tag }} {{ field }}
</p>
{% endif %}
{% endif %}
{% endfor %}
<p class="submit">
<input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" />
</p>
</form>
After submitting the form, it redirects to http://127.0.0.1:8000/comments/posted/?c=..
That means it calls the template django/contrib/comments/templates/comments/posted.html
The content of django/contrib/comments/templates/comments/posted.html:
{% extends "comments/base.html" %}
{% load i18n %}
{% block title %}{% trans "Thanks for commenting" %}.{% endblock %}
{% block content %}
<h1>{% trans "Thank you for your comment" %}.</h1>
{% endblock %}
That doesn't extends my project's base.html.
I need to customize/override that template so that it extends my project's base.html. How can i do that?
If i can't do that, then if i upload my django web project on server, then how would i edit the content of django/contrib/comments/templates/comments/posted.html so that it looks like that:
{% extends "path/to/myproject/templates/base.html" %}
{% load i18n %}
{% block title %}{% trans "Thanks for commenting" %}.{% endblock %}
{% block content %}
<h1>{% trans "Thank you for your comment" %}.</h1>
{% endblock %}
On local pc, for this time i changed/edited the content of django/contrib/comments/templates/comments/posted.html hard-coded to extends my project base.html.
Can anyone give some idea to solve this? I have searched a lot to solve this.
Just override it in your project's "templates" directory:
<project_root>/templates/comments/posted.html
It doesn't seem to be well documented in either the comments app or Django's general template documentation, but it works the same as overriding admin templates (which is documented).