django-templates

Using strftime on a django datetime produces a UTC time in the string

强颜欢笑 提交于 2020-01-10 02:32:08
问题 I have the following code in one of my models: def shortDescription(self): return self.name + ' ' + self.class_date.strftime("%I:%M") self.class_date is a timezone aware DateTimeField , self.class_date.is_aware() is True , USE_TZ is True . The shortDescription returns a string that gives the time in UTC rather than the default timezone, putting {{ aclass.class_date }} in the template displays the time in the correct zone. Is strftime always working on the base, native time? Or what else is

How to iterate over nested dictionaries in django templates

吃可爱长大的小学妹 提交于 2020-01-09 13:21:30
问题 I'm not sure the most efficient way to iterate over my nested dictionaries to print a matrix of the total and good values for every fruit for each date. Take for instance the two lists and dictionary below: fruits = ['apples','oranges','bananas'] harvest_dates = ['2011-07-23','2011-07-22','2011-07-21'] harvest_data = { 'apples': { '2011-07-23': { 'total': 100, 'good': 80}, '2011-07-22': { 'total': 97, 'good': 92}, '2011-07-21': { 'total': 90, 'good': 85} }, 'oranges': { '2011-07-23': { 'total

How to iterate over nested dictionaries in django templates

浪子不回头ぞ 提交于 2020-01-09 13:14:17
问题 I'm not sure the most efficient way to iterate over my nested dictionaries to print a matrix of the total and good values for every fruit for each date. Take for instance the two lists and dictionary below: fruits = ['apples','oranges','bananas'] harvest_dates = ['2011-07-23','2011-07-22','2011-07-21'] harvest_data = { 'apples': { '2011-07-23': { 'total': 100, 'good': 80}, '2011-07-22': { 'total': 97, 'good': 92}, '2011-07-21': { 'total': 90, 'good': 85} }, 'oranges': { '2011-07-23': { 'total

When is it appropriate to use Django context processors?

↘锁芯ラ 提交于 2020-01-09 12:35:53
问题 If about half of my views require the same data set, is it appropriate to use a context processor to make the data always available, or is there a better way to avoid repeating the code to get that data across several views without querying the data if it won't be used in the view? 回答1: The RequestContext initializer will run any context processors listed in the settings file, but it also takes a list of additional processors to run. Any general purpose context processors can be put in

how to start the forloop.counter from a different index

笑着哭i 提交于 2020-01-07 09:49:21
问题 I have 2 seperate forloops and i am using forloop.counter in bothloops. I want to start the second forloop counter from the ending of first forloop {% for i in something1 %} <tr> <td>{{ forloop.counter }}</td> <td>i.username</td> </tr> {% endfor %} {% for j in something2 %} <tr> <td>{{ forloop.counter }}</td> <td>j.username</td> </tr> {% endfor %} if the first forloop ends at 10 then i want to start the next for loop from 11.plz help 回答1: I'm not comfortable with Django, so I show a couple of

JS stops working on child template when I perform an AJAX call to change the queryset

ε祈祈猫儿з 提交于 2020-01-07 03:21:50
问题 My comments.html child template looks like this: <div class="commentsContainer"> {% for comment in comment_list %} ... {{ comment.text }} ... {% endfor %} </div> When I click on a button I call this AJAX function: $('.comments_new').on('click', function() { $.ajax({ type: 'GET', url: '/new_comments/', data: { }, success: function (data) { $('.commentsContainer ').replaceWith(data); } }) }); which calls this view to change the queryset (the inital queryset is comment_list = Comment.objects

Django timezone localization not working as expected

青春壹個敷衍的年華 提交于 2020-01-07 02:44:07
问题 I'm using Django 1.4.3 and Postgres 9.1.3. Here is my template where message.created_at is a python datetime object and it clearly tells me that the datetime object is stored in GMT as I can debug by passing e in date filter. The conversion to my local time which is IST is not happening though I used the block and filter given in the docs. It still renders the date time value in GMT. What am I missing? {% load tz %} {% localtime on %} <div class="m_td_block"> <span>{{ message.created_at|date:

How to concept a web application (dashboard) made of Django in terms of backend structure

别等时光非礼了梦想. 提交于 2020-01-06 15:22:11
问题 My goal is to build a python/django based dashboard related to soccer where the user can select a soccer team of his choice in the sidebar and then gets all related data displayed in the dashboard in return. The dashboard will mostly consist of simple figures and more dynamic charts (using Plotly etc.). I attached the html frontend so you can get a brief feeling of how it will look like. As for the backend, I am not quiet sure what might be a good or even the best solution structurewise. I

Reverse for 'todo-user' with arguments '('',)' not found. 1 pattern(s) tried

我们两清 提交于 2020-01-06 08:43:12
问题 I am trying to add an user when clicking on a link but I have the following error : Reverse for 'todo-user' with arguments '('',)' not found. 1 pattern(s) tried: ['todo/(?P[^/]+)/$'] My views.py def todo_user(request, todo_id): todo.username.add(request.user) todo.save() return render(request, '/') Template <a href="{% url 'todo-user' todo.id %}"></a> Urls.py path('validate/<todo_id>/', views.todo_user, name='todo-user), Views.py for the template render : def home(request, token): todo

How to change the display of Django admin when use ManyToManyField

折月煮酒 提交于 2020-01-06 08:33:14
问题 I'm coding a news website.News model has a ManyToManyField foreign key named tag . Here is my News model: class News(models.Model): title = models.CharField(max_length=100, verbose_name='title') category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="cate", blank=True, verbose_name='Category') tag = models.ManyToManyField(Tag, blank=True, verbose_name='Tags') class Meta: verbose_name = "新闻" verbose_name_plural = verbose_name def __str__(self): return self.title Here is