django-templates

How to make Django template engine to render in memory templates?

折月煮酒 提交于 2020-01-12 02:58:54
问题 I am storing my templates in the database and I don't have any path to provide for the template.render method. Is there any exposed method which accepts the template as a string? Is there any workaround? 回答1: Instantiate Template with the string to use as a template. 回答2: Based on the the docs for using the template system: from django.template import Template, Context t = Template("My name is {{ my_name }}.") c = Context({"my_name": "Adrian"}) t.render(c) 回答3: In Django < 1.8: from django

Two annotations in a single query

时光毁灭记忆、已成空白 提交于 2020-01-11 13:34:19
问题 I am trying to build a query set to do a count, the query set involves three models. class Owner(models.Model): name = models.CharField(max_length=10, null=False) class Location(models.Model): name = models.CharField(max_length=10, null=False) owner = models.ForeignKey(Owner, on_delete=models.SET_NULL, null=True, blank=True) class Asset(models.Model): name = models.CharField(max_length=10, null=false) owner = models.ForeignKey(Owner, on_delete=models.SET_NULL, null=True, blank=True) location

Django use a variable within a template tag

感情迁移 提交于 2020-01-11 13:05:25
问题 I am using the static template tag in my Django template: {% load staticfiles %} <img src="{% static "my_app/myexample.jpg" %}" alt="My image"/> But instead of "my_app/myexample.jpg" , I need to use a file name that is a property of my model object (i.e. {{ SampleModel.0.propertyValue }} , which I am passing as context to this template. But how do I include {{ ... }} within the static template tag? It throws an error. Any way out? 回答1: You can use use a variable in the static template tag.

Using a Django template_tag on pages that extend from the view that loads the tag

情到浓时终转凉″ 提交于 2020-01-11 09:18:08
问题 I've added a template tag to my app which I load in a view located in inc/base.html . This view contains my basic HTML layout. All my other views begin {% extends "inc/base.html" %} . In one of my views I want to refer to my template tag, which is loaded in inc/base.html using this code: {% load spb_utils %} . If I try to use on of the template tags inside base.html it works fine, but if I try it any other view, it errors unless I manually add {% load spb_utils %} to the extended view as well

django template url from another app

好久不见. 提交于 2020-01-11 06:21:08
问题 I have to be missing something silly. I have a {% url %} in a template where the action is from another app. It isn't working but I have no clue if there is something different about using view functions from other apps or if I am just doing something silly. call/template/call/file.html <form action="{% url 'upload_image' %}"></form> picture/urls.py from .views import PictureList, PictureCreate, PictureDetail, PictureUpdate, PictureDelete, upload_image ... url(r'^upload_image/$', upload_image

Django can't find template

♀尐吖头ヾ 提交于 2020-01-11 05:38:47
问题 I know many people have asked, this question, but despite hardcoding the path to my template directory I can't seem to get Django to find my template. Here is settings.py TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', #django.template.loaders.eggs.Loader', ) TEMPLATE_DIRS = ( "/Users/full/path/to/marketing_site/templates", ) This is the views.py file: def static (request, feature_page): # this will get the appropriate

get the list of checkbox post in django views

久未见 提交于 2020-01-10 08:45:19
问题 I have this code in my template: {% for email in emails %} {%if email%} <input type="checkbox" name="email" value="{{email}}" /> {{email}}<br /> {% endif %} {% endfor %} so the output in the template is: email1@sample.com email2@sample.com email3@sample.com ... in my views I printed my request.POST about this. print request.POST print request.POST['email'] for email in request.POST['email']: print email i got this output: <QueryDict: {u'email': [u'email1@sample.com', u'email2@sample.com', u

Django Dropdown populate from database

南笙酒味 提交于 2020-01-10 08:31:07
问题 If I pass items to the template through the view, and I want the user to select one of the values that gets submitted to a user's record, I would only have dun a for loop in the template right? What would that look like? In the template: <form method="POST" <select> </select> </form> Model: class UserItem(models.Model): user = models.ForeignKey(User) item = models.ForeignKey(Item) class Item(models.Model): name = models.CharField(max_length = 50) condition = models.CharField(max_length = 50)

django url parameters before include url with namespace

我的未来我决定 提交于 2020-01-10 05:37:05
问题 In my last question I asked how to get urls working for parameter before included urls.py and it worked. Django {% url %} when urls with parameters like: url(r'^foo/<parameter>/$', include(some.urls)) Now I want to use the same included urls.py with namespaces . urls.py urlpatterns = patterns('', url(r'^/foo/(?P<parameter_1>\d+)/', include('bar.urls', namespace='foo', app_name='foo')), ) bar.urls.py urlpatterns = patterns('', url(r'^/bar/$', 'bar.views.index', name='bar'), url(r'^/bar/(?P

Reverse for 'update_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comment\\/(?P<news_pk>[0-9]+)$']

こ雲淡風輕ζ 提交于 2020-01-10 04:38:26
问题 I'm coding a news site.Now I'm detailing with the comment post function.And meet the issue says: Reverse for 'update_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comment\\/(?P<news_pk>[0-9]+)$'] I have tried many ways and times but still can't solve it and I can't find any wrong with my code.And really need your help. The comment post function is in the news_detail.html. There are two important apps in my project news and operation comment models is under operation Here is