Django using a global Template and Views - python

so im new to Python and Django, im wondering if there is a concept like a template for the whole app/site like
<html>
...
<body>
{% content_goes_here %}
</body>
and than you got your views:
<tr>
{% name %}
</tr>
..
so i can use views as building blocks for my site but have a global template that does all the css/head/script/html stuff.
I know this concept from cakephp for example. How is this done in Django?

You can do this by using template inheretance

Related

Django multiple templates

I'm building a site where users can view their posts Like this. After building the quizzes portion, I tabbed to "blogs" where I realized I needed to import the blogs template to use it.
I'm using the quiz template already like this
{% extends '../main/base.html' %} {% block title %}View Quizzes{% endblock %} {% block content %}
but I need to access the blog template as well. How can I do this? Thanks!
You can include the blog template into the main one like:
{% include "path/to/blogs.html" %}
It's best to have a main template which includes blogs, quizzes... (instead of inheritance)
You don't need django to do this.

Can't display map on template using PointField django

Can we display a map on a template using django PointField?
I have a model with one PointField and then after creating a model form, I want to display the map in my template using that form.
View
def map_view(request):
form = ShopForm()
context = {
'form' : form
}
return render(request, 'ads/map.html', context)
Template
<html>
<head>
</head>
<body>
<div>
<form method="post">
{{ form }}
<input type="submit" >
</form>
</div>
</body>
</html>
I was wondering is it possible to display the map using that or do I need to something extra.
I was also having the same question and thankfully I got the right way of doing it.
Anyone who's facing this issue can follow this link
The tutorial is divided into three parts.
REMEMBER:
When you reach to the 2nd part and everything is done and you're writing python manage.py runserver
-in your template include all these
{% load leaflet_tags %}
{% leaflet_css %}
{% leaflet_js %}
Place this within your {% block content %} {% endblock content %} otherwise you'll end up rendering nothing
Have you tried using the following part of the documentation on your form?
https://docs.djangoproject.com/en/2.2/ref/contrib/gis/forms-api/#widget-classes:
from django.contrib.gis import forms
class ShopForm(forms.Form):
point = forms.PointField(widget=
forms.OSMWidget(attrs={'map_width': 800, 'map_height': 500})
)

Integrate Django blog project in HTML website

I have a regular website where HTML5, CSS3, JQUERY and static images have been used.
I also have a Blog written in Django and I would like to integrate it in the website.
I am really new to Django so I was wondering which is the best approach to use.
Should I integrate the website code as part of the Django project or there are some other solutions?
thanks!
You have 2 ways of integrating your current site with Django.
1) You can write API with DjangoRestFramework and make requests with jQuery AJAX in order to get content from Django.
2) You can use your current HTML files as your Django project templates for rendering content from Django.
You can use a Django template. The template defines placeholders and various bits of basic logic (template tags) that regulate how the document should be displayed. Usually, templates are used for producing HTML, but Django templates are equally capable of generating any text-based format.
If you've used a templating engine like ''. They look somehow similar.
<html>
<head>
<title>Ordering notice</title>
</head>
<body>
<h1>Ordering notice</h1>
<p>Dear {{ person_name }},</p>
<p>Thanks for placing an order from {{ company }}. It's scheduled to ship on {{ s\
hip_date|date:"F j, Y" }}.</p>
<p>Here are the items you've ordered:</p>
<ul>
{% for item in item_list %}
<li>{{ item }}</li>{% end for %}
</ul>
{% if ordered_warranty %}
<p>Your warranty information will be included in the packaging.</p>
{% else %}
<p>
You didn't order a warranty, so you're on your own when the products inevitably stop working.
</p>
{% endif %}
<p>Sincerely,<br />{{ company }}</p>
</body>
</html>
check here for more details https://djangobook.com/django-templates/

Django unable to understand template html format

I am learning Django framework, I setup Django on my local host and successfully run my first app, and i downloaded some web app from git and then i tried to understand it but when i opened template html the format used their was completly different from original html format, so i just want to know what scripting, or language they are using to build html files, Here is a sample code
{% extends "admin/change_list.html" %}
{% load i18n %}
{% block object-tools-items %}
{% if not is_popup %}
<a href="{{ recoverlist_url }}" class="recoverlink btn">
<i class="icon-repeat"></i>
{% blocktrans with cl.opts.verbose_name_plural|escape as name %}Recover deleted {{ name }}{% endblocktrans %}</a>
{% endif %}
{{block.super}}
{% endblock %}
I am unable to get what {% %} doing here, Is it a scripting tag just like jsp or something else, If it is a scripting that what kind of script is it ? because normally html files starts with
<head>
tag.
Is it a scripting tag just like jsp or something else
Yes.
If it is a scripting that what kind of script is it ?
Django Template language
because normally html files starts with <head> tag.
It's not HTML. It's a template language that render and outputs HTML.
I'd suggest you go through the Django tutorial.

Django: accessing raw urls of form media in templates

{{ form.media.js }} and {{ form.media.css }} are great to easily include media in Django templates.
My problem is that I would like to get access to the raw urls (without <\script type="text/javascript" src="raw_url"><\/script>) of these media (to include them using headjs).
I'd like to achieve something like that:
<script>
{% for script in form.media.js %}
head.js("{{ script.raw_url }}");
{% endfor %}
</script>
If you take a look at source code of Media class there will be a function:
def render_js(self):
return [u'<script type="text/javascript" src="%s"></script>' % self.absolute_path(path) for path in self._js]
So you can go the same way. Since we can't use underscored attributes in templates we can add method to the form class like this and use it:
from django import forms
class TestForm(forms.Form):
def get_media_js(self):
return [self.media.absolute_path(path) for path in self.media._js]

Categories