django-templates

How to check if django template variable is defined?

断了今生、忘了曾经 提交于 2019-12-04 14:59:36
问题 I have a django template which is used from many views. The template has a block for messages used to notify user of anything that should take their attention. Whether a message is sent or not depends on the views. Some views may send a message variable to the template while others may not. view_1: message = "This is an important message" render_to_response("my_template.html", {'message':message, 'foo':foo, 'bar':bar}, context_instance = RequestContext(request)) view_2: message = "This is an

Invoke Django template renderer in memory without any files from strings?

泄露秘密 提交于 2019-12-04 14:49:43
I have built a Macro language for my users that is based upon the Django template language. Users enter into UITextFields their template/macro snippets that can be rendered in the context of larger documents. So I have large multi-line string snippets of django template code that should be populated with variables that are also stored in memory. I don't want to ever have to dump anything to files, I need to render these template How can I invoke the Django template renderer on a template that is stored in a string in memory (in python instance variables)? The variables that should populate

ListView and CreateView in one template Django

北慕城南 提交于 2019-12-04 14:09:28
问题 I'm designing a page in which people can view and create objects of a certain sort (the objects are instances of the model Project). As I understand it, I can't do it in one view without horribly messy code, so I am trying to understand how I can use one template to show two views (the ProjectCreateView and the ProjectListView). Right now, this is what I am working with: views.py: class ProjectCreateView(CreateView): model = Project template_name = "fileupload/project_list.html" fields = [

integrating third-party django apps and templates

本秂侑毒 提交于 2019-12-04 13:54:25
问题 I'm new to Django and fairly confused about how third-party apps are best integrated. In my (possibly naive) interpretation of DRY, I want to minimize copy/pasting not only my own code but other people's code as well, so I'm happy with, for example, the pattern of using contrib.auth as mostly a black box if I needed additional info on auth.User (by extending auth.User with a UserProfile object or inheritance). And this was how I imagined I'd use most third-party apps as well. However, I

Django admin template overrides not working in production environment

对着背影说爱祢 提交于 2019-12-04 13:18:07
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 +sales +Customer change_form.html +Transaction change_form.html +devices # lots of templates under

How to use two different Django Form at the same template?

人走茶凉 提交于 2019-12-04 13:12:21
问题 My forms.py: class AlertForm(forms.ModelForm): class Meta: model=Alert fields = ('high','medium', 'user') widgets = { 'user': forms.HiddenInput() } AlertCountFormset = modelformset_factory(Alert, form = AlertForm) Another Django Form class: class NotifierForm(forms.ModelForm): high = forms.ChoiceField(choices=NOTIFIER_TYPE) medium = forms.ChoiceField(choices=NOTIFIER_TYPE) low = forms.ChoiceField(choices=NOTIFIER_TYPE) def save(self, commit=True): alert = super(NotifierForm, self).save(commit

Auto load content on scroll down

别来无恙 提交于 2019-12-04 12:21:07
问题 I am creating a blog on django/webfaction. My hompage currently displays 4 posts by default (posts are limited to 4 on queryset in urls.py ). Now I would like to load four more posts once user reaches the bottom of the page and keep on continuing that until last post is reached. How to achieve this? 回答1: If you want to load your content on reaching extreme bottom of document use following code: $(window).scroll(function() { if($(window).scrollTop() == $(document).height() - $(window).height()

How to test for use of a django template block?

落爺英雄遲暮 提交于 2019-12-04 11:15:58
I would like to do the following: {% if appnav %} <hr /> <div id="appnav"> <ul class="tabs"> {% block appnav %}{% endblock %} </ul> </div> {% endif %} ...however, testing for the present use of a block by templates further down the inheritance chain does not seem to work. Is there some other conditional that might do this? The template language doesn't provide exactly what you're looking for. Child templates can call the parent block with {{ block.super }} , but parent templates can't reference child templates. Your best bet will probably be to write a custom template tag. There are two

Putting a block inside another in Django

╄→尐↘猪︶ㄣ 提交于 2019-12-04 11:12:32
I have a Django template that I want to extend in multiple places. In some the div must be inside a form, and in others it must not be. To do this I put a block above and below the div so I could add and in them respectively. Desired: <form> <div class="my_div"> {% block div_content %} ... {% endblock %} </div> </form> Template: {% block div_top %}{% endblock %} <div class="my_div"> {% block div_content %} {% endblock %} </div> {% block div_bottom %}{% endblock %} Looking at this I can't help but think that there is a better way to do it. What is the standard Django way of doing this? Use of

Django SQL query duplicated n times

﹥>﹥吖頭↗ 提交于 2019-12-04 11:11:53
问题 I have a book model and a rating model, class Book(models.Model): title = models.CharField(max_length=255) slug = AutoSlugField(unique=True, populate_from='title') description = models.TextField() # more fields class Rating(models.Model): book = models.ForeignKey('library.Book') score = models.DecimalField(max_digits=2, decimal_places=1) the Query, books = {'books': Book.objects.filter(pk__in=Rating.objects.all().order_by('-score' ).values_list('book__id', flat=True))[:10] } template, {% for