django-1.4

Django: problems while loading custom filters in the base template file while using template inheritance

前提是你 提交于 2019-12-11 02:27:36
问题 When doing the {% load custom_filters %} in the template, after {% extends "base.html" %} everything works fine, but when I move the load to the base.html template the filter gets a weird behaviour. This is my custom_filters.py : from django import template from django.template.defaultfilters import stringfilter register = template.Library() # To cut off strings at a specified character, at first occurance. Example: # time = 19:30:12.123456 # {{ time|cut:'.' }} # returns: 19:30:12 @register

Load Django “static” template tag library globally without explicitly loading it in every file

大城市里の小女人 提交于 2019-12-10 21:27:59
问题 I want to use the static tag in templates like so: <img src="{% static "img/test.jpg" %}"> I've found that that requires me to put {% load static %} at the beginning of every template file. Since I'm using it everywhere, I would like it to be a globally available tag so I don't need to put {% load static %} to use it. In my settings I do have: TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.static', ) I saw both of these questions: Make django static tag globally available and

Django1.4: Generic way to set language links in template to work with i18n_patterns?

走远了吗. 提交于 2019-12-09 10:33:59
问题 I started to play with new i18n_patterns in Django 1.4. Basically, i want to have language links for each of my supported languages on all of my templates headers. I have implemented my header as a separate template that is being included in other templates. Is there a way to keep my header generic and solve this without passing the current view name or current url in template context? I guess it comes to a question how do i retrieve the current view or url from inside the template in a

{% url %} gives me NoReverseMatch error while reverse() returns the url just fine. Why?

你离开我真会死。 提交于 2019-12-09 04:46:40
问题 I don't know if this SO question is of the same problem that I am about to describe, but it does share the same symptoms. Unfortunately, it still remains unresolved as I am writing. So here is my problem. I am trying to add James Bennett's django-registration app to my django project. I have pretty much finished configuring it to my needs - custom templates and urls. Just when I thought everything was good to go. I got NoReverseMatch error from using {% url 'testing' item_id=123 %} (I also

Cross-referencing foreign keys in Django 1.4

风流意气都作罢 提交于 2019-12-08 09:04:31
I have a Django project where I would need two models to have a foreign key to each other. However this is not possible because two Python files would have to import each other which Python doesn't allow. What is the best way to solve this problem? So my code currently looks like this: countries/models.py: from django.db.models import Model, ForeignKey from users.models import Profile class Country(Model): president = ForeignKey(Profile) users/models.py: from django.db.models import Model, ForeignKey from countries.models import Country class Profile(Model): citizenship = ForeignKey(Country)

Whats is the best way to migrate folder and files structure from django1.3 to django1.4?

筅森魡賤 提交于 2019-12-05 17:03:37
问题 I have a little project created with django1.3 and I want to migrate it to django1.4 but since the files structure change a little, what is the best way to migrate? 回答1: Read https://docs.djangoproject.com/en/dev/releases/1.4/ first. For a quick run, just update env from Django1.3 to 1.4, tweak settings file and project code by fixing any incompatibility warning and imports issue. For a clean update, better to create an empty project w/ the same name of the current project and migrate it w/

How to set initial values for a ModelForm when instance is also given

眉间皱痕 提交于 2019-12-05 00:13:11
问题 It seems like if a ModelForm is given an instance, it ignores any values you provide for initial and instead sets it to the value of the instance -- even if that instance is an empty model record. Is there any way to create a form with an instance and have it set initial data? I need it because I'm saving related records and they don't appear to save correctly unless the ModelForm is given an instance when created. I'm sure the answer to this is straightforward and I'm just missing something

Filter on prefetch_related in Django

你说的曾经没有我的故事 提交于 2019-12-03 22:39:56
Is there a way of filtering prefetched objects? I need to get the latest() of the prefetched objects but prefetch_related doesn't work if you use latest because the query is changed? The example here does what I need but I was hoping there's a simpler workaround... https://github.com/ionelmc/django-prefetch#example Webthusiast As of Django 1.7, filtering prefetched objects is possible. See this SO answer , and the Django documentation . It is very simple method which is hardly comparable with those app, but hope you will find it useful: class Author(models.Model): name = models.CharField(max

How to set initial values for a ModelForm when instance is also given

一个人想着一个人 提交于 2019-12-03 15:56:09
It seems like if a ModelForm is given an instance, it ignores any values you provide for initial and instead sets it to the value of the instance -- even if that instance is an empty model record. Is there any way to create a form with an instance and have it set initial data? I need it because I'm saving related records and they don't appear to save correctly unless the ModelForm is given an instance when created. I'm sure the answer to this is straightforward and I'm just missing something obvious. Here is the relevant code: in the view: form = form_class(person=person, conference=conference

Django1.4: Generic way to set language links in template to work with i18n_patterns?

寵の児 提交于 2019-12-03 12:51:46
I started to play with new i18n_patterns in Django 1.4. Basically, i want to have language links for each of my supported languages on all of my templates headers. I have implemented my header as a separate template that is being included in other templates. Is there a way to keep my header generic and solve this without passing the current view name or current url in template context? I guess it comes to a question how do i retrieve the current view or url from inside the template in a generic way. BTW, i discovered that my previous approach with set_lang view to change the active language