django-templates

How can I print the 'date received' only one time while using a for loop?

假如想象 提交于 2019-12-23 04:06:12
问题 I am trying to group messages by received today, received yesterday, ect. To clarify, I am trying to have a single header that says "Today" and then list those messages underneath. I am NOT trying to print "Today" along with each message that was received on that day (which is currently what's happening). I currently have the TODAY and YESTERDAY headers inside of my for loop so I understand that is why it is printing these headers for each mail message, but as previously stated I just want to

Django, custom tag… how?

青春壹個敷衍的年華 提交于 2019-12-23 03:23:07
问题 I would like to make a django custom tag to display 10 entry titles from the category where the user is reading an article. How can I do this? I need to pass the category from the actual entry. 回答1: The best way to do this would be with an inclusion tag. This is a tag that renders a template fragment which renders the 10 related articles. You just pass in the current article into the tag, and return the context for the template fragment - ie the related articles. @register.inclusion_tag(

Django-easy-pdf, header on every page of the document

爱⌒轻易说出口 提交于 2019-12-23 03:13:02
问题 I am trying to export some data in a PDF file. I am using: Django 1.9.12 django-easy-pdf 0.1.0 Python 2.7 The export works fine and all (no problem with my view) but I am struggling with adding a page header into every page of the document. At this point I can only render it on the first page. I have no such problem with the footer, it renders properly on every page. My template is as follows: {% extends "easy_pdf/base.html" %} {% block extra_style %} <style type="text/css"> @page { size:

How can I put a block of dynamically generated content into a django template?

╄→гoц情女王★ 提交于 2019-12-23 02:53:19
问题 I want to include things like twitter status, or delicious tags, in my django templates. These things are dynamic, yet regular. How would this be done? 回答1: There are a number of ways to handle this, so you can choose a method that best matches your own personal style or requirements: Template context variable : as answered by Alex you can put your content into a context variable that is included in the context of every template created by every view. Django even provides a mechanism for

Using both sort & filter on a QuerySet

拥有回忆 提交于 2019-12-23 02:45:11
问题 I have a list of userprofiles that I want to be able to sort and filter. I have been able to do this manually, by manually typing in the URLs, but I haven't been able to code the template page to allow the persistence of a previously-applied filter or sort. Here is the url and template code that I currently have -- # in urls url(r'^talent/filter\:(?P<position>[A-Za-z]+)/sort\:(?P<sort>[A-Za-z]+)$', 'talent_filter', name='talent_filter_sort'), url(r'^talent/filter\:(?P<position>[A-Za-z]+)/$',

Is it possible to use django's custom template tags to insert code in other blocks in the template?

跟風遠走 提交于 2019-12-23 02:42:05
问题 I'm writing a custom template tag that wraps an HTML element with some code to make it editable. This is backed up by some CSS, and JS that takes care of sending the data to the server to save it. This component requires the inclusion of <script type="text/javascript" src="../myscript.js"></script> at the bottom of the page and <link rel="stylesheet" type="text/css" href="../mystyle.css"> at the top. I already have two "js" and "css" template blocks in the page template. My question - is

How to perform pagination for context object in django?

爱⌒轻易说出口 提交于 2019-12-23 02:28:36
问题 I have tried something like this in views.py: class HomePage(TemplateView): template_name = "clouderp/index.html" def get_context_data(self, **kwargs): context = super(HomePage, self).get_context_data(**kwargs) qs = Blog.objects.all() context['blog_list'] = qs page = self.request.GET.get('page') paginator = Paginator(qs, 4) try: users = paginator.page(page) except PageNotAnInteger: users = paginator.page(1) except EmptyPage: users = paginator.page(paginator.num_pages) context['users'] = users

Django ChoiceField get currently selected

社会主义新天地 提交于 2019-12-22 18:12:16
问题 I have a simple ChoiceField and want to access the 'selected' item during my template rendering. Lets say the form gets show again (due to error in one of the fields), is there a way to do something like: <h1> The options you selected before was # {{ MyForm.delivery_method.selected }} </h1> (.selected() is not working..) Thanks ! 回答1: It would be accessed by {{ myform.delivery_method.data }} <h1> The options you selected before was # {{ MyForm.delivery_method.data }} </h1> 回答2: @Yuji

Set background color for a field depending on its value

孤者浪人 提交于 2019-12-22 11:35:35
问题 I have a Customer table and one of the fields here is for Status ....I want to have the background colour of the cell containing this field to be set according to it's value e.g green for the status "Closed"; yellow for the status "Pending" and so on. What's the best way to do this...possibly something that'll be easy to modify if need be in future? 回答1: On your css create class for td as follows, considering you want to display your customer table information in a html table, td.closed{

Optimize code in Django - view ManyToMany as matrix

邮差的信 提交于 2019-12-22 10:47:13
问题 I'm trying to show user group permissions in Django and show them in a "Drupal" style like a matrix. It works, but it takes too long to make the query and paint it in the template. Is there some way to improve my code? view img up(accomplished),down(views and template.html) views : def GroupPermissionsView(request): title = "Groups Permissions" groups = Group.objects.all() permissions = Permission.objects.all() context = Context({ 'title': title, 'groups': groups, 'permissions': permissions,