django-templates

How to use django template dot notation inside a for loop

£可爱£侵袭症+ 提交于 2019-12-06 11:35:23
问题 I am trying to retrieve the value of a dictionary key and display that on the page in a Django template: {% for dictkey in keys %} <p> {{ mydict.dictkey }} </p> {% endfor %} (let's say 'keys' and 'mydict' have been passed into the template in the Context) Django renders the page but without the dictionary contents ("Invalid template variable") I assume the problem is that it is trying to do mydict['dictkey'] instead of mydict[ actual key IN the variable dictkey ]? How does one "escape" this

django: calculate percentage based on object count

南笙酒味 提交于 2019-12-06 11:27:48
I have the following models: class Question(models.Model): question = models.CharField(max_length=100) class Option(models.Model): question = models.ForeignKey(Question) value = models.CharField(max_length=200) class Answer(models.Model): option = models.ForeignKey(Option) Each Question has Options defined by the User. For Example: Question - What is the best fruit? Options - Apple, Orange, Grapes. Now other user's can Answer the question with their responses restricted to Options . I have the following view: def detail(request, question_id): q = Question.objects.select_related().get(id

django how to loop through the context object passed back by a generic detailview?

狂风中的少年 提交于 2019-12-06 11:01:14
问题 I'm using a generic DetailView to display a project object. Can I loop through the fields somehow in my template or do I have to place every field. url(r'^(?P<slug>[-\w]+)/$', DetailView.as_view(model=Project, template_name='projects/detail_project.html',slug_field='slug', context_object_name='project'), name='project_detail'), I've got something like this in my template: {{ project.title }} {{ project.created_date }} etc... Is there a way to do something like this? <table> {% for field in

Reading RSS feed and displaying it in Django Template | feedparser

我们两清 提交于 2019-12-06 10:21:25
问题 Refer this blog: http://johnsmallman.wordpress.com/author/johnsmallman/feed/ I want to fetch the RSS feed for my application. The above blog is a wordpress blog. I am using feedparser import feedparser feeds = feedparser.parse('http://johnsmallman.wordpress.com/author/johnsmallman/feed/') Now feeds['feed']['title'] Outputs u"Johnsmallman's Blog \xbb John Smallman" My question is How exactly i present this in my app. Lets say this blog contains 100s of articles. So i want to loop over and

Django templatetag for rendering a subset of html

梦想的初衷 提交于 2019-12-06 10:09:47
问题 I have some html (in this case created via TinyMCE) that I would like to add to a page. However, for security reason, I don't want to just print everything the user has entered. Does anyone know of a templatetag (a filter, preferably) that will allow only a safe subset of html to be rendered? I realize that markdown and others do this. However, they also add additional markup syntax which could be confusing for my users, since they are using a rich text editor that doesn't know about markdown

customizing request.user with a proxy model that extends Django User model

折月煮酒 提交于 2019-12-06 09:51:42
class MyUser(User): class Meta: proxy = True def do_something: ... With the above, I extend the behavior of the Django User Model using the proxy model technique. I was hoping that I could somehow customize request.user , so that MyUser instance, instead of User instance, is assigned to it every time. How could I implement that, if possible? You can inherit a new middleware class from AuthMiddleware or create a separate middleware which will process request after django's auth and change request.user to your user instance. Carlitux I looked at code and my idea is here. Late but I think can be

Does Django's render_to_response cache unrendered templates or load from disk every time?

巧了我就是萌 提交于 2019-12-06 08:37:56
Fetching a template from disk is significantly slower than pulling it out of something like memcached, so loading them from disk each time is wasteful. Does Django cache unrendered templates in memory or in the CACHE_BACKEND or do I have to implement that myself? Django comes with a cached template loader as well. I can't seem to find anyplace where the filesystem loader might cache the templates. It sure does look like it loads them every time. This probably isn't really a problem, though. For one thing, the filesystem loader does exactly what it says, it reads the templates from disk every

Django admin template overrides not working in production environment

余生颓废 提交于 2019-12-06 08:13:01
问题 Like this question, my admin overrides aren't working in my production environment but they are in my development environment (same django version). I've tried reordering the INSTALLED_APPS tuple in settings.py with no change (was the answer to the question linked above). Here's how I have my project constructed: /WebDJ/ # project dir +devices # unrelated app, but it uses templates (see below) +sales __init__.py admin.py models.py # has Customer and Transaction model classes +templates +admin

Set background color for a field depending on its value

ε祈祈猫儿з 提交于 2019-12-06 08:05:58
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? mmrs151 On your css create class for td as follows, considering you want to display your customer table information in a html table, td.closed{ color: green; } td.pending{ color: yellow; } Now on your template you can loop your database table on

Django: How to return model formset in ajax and use in template

拟墨画扇 提交于 2019-12-06 08:03:18
I need to dynamically add forms to my formset during runtime using ajax, for which I am referring to Dynamically adding a form to a Django formset with Ajax I have multiple formsets on the same page with different prefixes. My models are designed like so: A user can have many phones. A phone can have many lines (if details are needed) Accessing Many to Many "through" relation fields in Formsets Once a user adds a new phone, I save the phone using ajax. The view is as follows def addUserPhone(request, customer_id, location_id, user_id, **kwargs): error_msg = u"No POST data sent." context = {}