Sending messages using Django Postman - python

I am trying to use Django-Postman and have gotten as far as being able to see the templates on the webpage after I press the link but I don't know how send messages works. According to the write view there should be a form loaded but all I get is the links to the other pages in the template. If someone could explain how to get this to work it would be fantastic.
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav ">
<li>MyCourse</li>
<li>Timetable</li>
<li>logout</li>
<li>Inbox</li>
</ul>
</div>

Recently had to set up postman myself, and based just off your snippet, I'm going to assume you wrote the ul element yourself.
So, if that's the case, postman actually expects to handle all of that for you. According to the docs, you need to create a base.html template inside your own template directory. A few blocks are expected to be present inside this template, namely {% title %} (text it will add to the page's title), {% extrahead %} (some extra js and css), {% content %} (would contain the missing forms you're looking for) and {% postman_menu %} (the menu links automatically generated by postman).
You could always create the menu links yourself, but I'd suggest you create a /postman folder in your app's template folder, then copy the base.html from the installed postman app (this contains the code for how to layout the ul). Just a little more django-esque, and usefull if you need to fiddle with the names of the template tags, etc, but that's up to you.
Hope this helps, happy coding.

Related

I cannot get images to work on my django project(this is my first one and im having trouble)

I have an img folder in the same directory as my html files. I want to put images onto my site but I am doing something wrong.
this is the code:
{% block a %}
<div class="row">
<div class="col-sm-4 center">
<img src="img/img01.jpeg">
</div>
</div>
{% endblock %}
this is what happens when I run the local server.
this is my file directory
Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”.
Those files need to be handled another way as your templates. The documentation can be found here.
It comes basically down to defining a seperate static directory inside your app. It is really good explained in the documentation.

Nested Django view with .html inserted using include tag

I would like to know if anyone has any best practice recommendations for rendering a view within a view in Django, or something with a similar effect where both views can have their own context and methods.
I am currently rendering the dropdown using the built-in include tag. So the nested .html is using the same Django view as the rest of the page. And all the context is coming from the main view as shown below. I would like to give this dropdown its own Django view so that I only perform certain database queries and other calculations if the dropdown is opened.
<div class="container-fluid">
<div class="row">
<div class="col-6">
</div>
<div class="col-2 d-flex justify-content-center">
{% include nested.html %}
</div> ...
It would be nice if someone with experience in this could let me know what they think, and if it even makes sense to try and split this into 2 views.
In summary. I would like to know if I can somehow render a separate view using something similar to the include Django tag. So that I have control over where the view gets rendered.
Below I have outlined my implementation of the solution below, using custom inclusion tags as suggested in the comments to my question.
You can make a custom template tag file in project directory and then register these in the template library. e.g:
template_tags.py
from django import template
from users.models import Organisation
register = template.Library()
#register.inclusion_tag('nested.html', takes_context=True)
def nested_template(context, param=None): #param to pass variables from main view
context.update({
'data_list': get_data_list(param)
'organisation_id': param,
})
return context
def get_data_list(self,param):
data_list = ... #do all queries needed.
return data_list
In the template tag you point to your nested.html which does whatever you need it to do. For example:
nested.html
{% for data in data_list %}
<h4> {{data}} </h4>
{% endfor %}
Then to include the template, in your main view template:
{% load nested_template %} #at the top or anywhere before using it
{% nested_template param %} #where you want the template included
Hopefully clear enough and may assist someone

How can I get the base url with Flask / Jinja2?

I have a base.html template which I would like to use for all pages. This base.html contains a navigation
<nav>
<li>Home</li>
<li>bar</li>
</nav>
This is no problem when I'm on the same level (e.g. localhost:5000/whatever), but when I'm in a subfolder (e.g. localhost:5000/whatever/insert) the links break.
This can be fixed by making the relative links absolute, e.g.
<nav>
<li>Home</li>
<li>bar</li>
</nav>
However, I don't know how to get the base_url. If possible, I would like to avoid adding base_url to each render_template call. And, if possible, I would also like to avoid to set base_url manually.
How is this problem solved with Flask / Jinja2?
Don't worry about a base url; if home and foo are routes in your Flask app, use the url_for() function to build your URLs instead:
<nav>
<li>Home</li>
<li>bar</li>
</nav>
Also see the URL Building section of the Flask Quickstart documentation:
Why would you want to build URLs using the URL reversing function url_for() instead of hard-coding them into your templates?
Reversing is often more descriptive than hard-coding the URLs.
You can change your URLs in one go instead of needing to remember to
manually change hard-coded URLs.
You can change your URLs in one go instead of needing to remember to manually change hard-coded URLs
URL building handles escaping of special characters and Unicode data
transparently.
The generated paths are always absolute, avoiding unexpected behavior of relative paths in browsers.
If your application is placed outside the URL root, for example, in
/myapplication instead of /, url_for() properly handles that for you.
If you have elements like,meta, a navbar, etc in your base.html that you would like to display across all pages in your site. You can type this at the top of each new page.
{% extends "base.html" %}
{% block content %}
<!-- write page specific html between here -->
{% endblock %}
its important to place
{% block content %}
{% endblock %}
within you base.html file.

Pelican Archives Not Displaying

How do you enable an archives tab on a pelican powered blog?
I see from the docs that it is a direct template by default, but it isn't showing up on my blog. Is there some additional field to enable it? I couldn't find any mention of it in the docs or tutorials, so I'm assuming I've missed something obvious.
Not sure if you ever resolved this; I was having the same issue as you with pelican's bootstrap3 theme. For some reason, setting a value for YEAR_ARCHIVE_SAVE_AS was not working.
After looking at the theme's base.html file, I got it working by adding the following to pelicanconf.py:
ARCHIVES_SAVE_AS = 'archives.html'
Lines 156-158 from my base.html file:
{% if ARCHIVES_SAVE_AS %}
<li><i class="fa fa-th-list"></i><span class="icon-label">{{ _('Archives') }}</span></li>
{% endif %}

Django - Proper way to separate site-specific content from templates?

I have had trouble putting my question into words to Google the answer, but here goes:
Let's say I have a few strings that appear in multiple places in my Django project. For example, the site owner's name, contact info, the text on the main page, etc.
Right now they are all hard-coded into my templates. Sometimes these need to be changed, and it's especially a pain to replace the strings that appear in multiple places.
What is the proper Django way to pull this data out of the templates and into one central location?
Thanks in advance, I want to do this the right way :)
You can use an app such as django-chunks which allows you to save small pieces of text in the database and add them to your templates using simple template filters (you reference each piece of text with a slug). So in your template you would have:
{% load chunks %}
<html>
<head>
...
</head>
<body>
<div id="content">
...
</div>
<div id="sidebar">
{% chunk "sidebar_disclaimer" %}
</div>
<div id="footer">
{% chunk "footer_text" %}
</div>
</body>
</html>
This would create two new entries in the admin interface where you could add and adjust the text that is to appear in the sidebar and footer
There are plenty of alternatives:
django-flatblock
django-siteblocks
django-generic-flatblocks

Categories

Resources