django-templates

Django won't serve static files while using development server

此生再无相见时 提交于 2019-12-05 14:55:45
I just started a new development server for a website I am working on and I can't seem to get the Django development server to serve the static files I have for CSS and other things. The CSS for the admin site loads fine. I am running it in a virtualenv sandbox. In settings.py I've messed around with MEDIA_ROOT and MEDIA_URL. So far for MEDIA_ROOT I've tried. MEDIA_ROOT = '/home/wluw/wluw/wluw/media' and MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media') I changed my ADMIN_MEDIA_PREFIX to ADMIN_MEDIA_PREFIX = '/admin_media/' my MEDIA_URL looks like this MEDIA_URL = '/media/' and the

Django : ModelForm with conditions

孤人 提交于 2019-12-05 14:29:42
I'm trying to create a form variable. As default Player have the level 0 and he can just change is name. Later when he is level 1, he can change is name and is avatar. When he is level 3, he can change is name, is avatar and is job. Etc... Models.py: class Player(models.Model): level = models.SmallIntegerField(default=0) name = models.CharField(max_length=50) avatar = models.URLField(default='http://default-picture.com/01.png') job = models.TextField(null=True) Fomrs.py: class ProfileForm(forms.ModelForm): class Meta: model = Player fields = ['name', 'avatar', 'job'] widgets = { 'name': forms

django forms error_class

不问归期 提交于 2019-12-05 12:05:48
Is there a way to give a form a special error rendering function in the form definition? In the docs under customizing-the-error-list-format it shows how you can give a form a special error rendering function, but it seems like you have to declare it when you instantiate the form, not when you define it. So you can define some ErrorList class like: from django.forms.util import ErrorList class DivErrorList(ErrorList): def __unicode__(self): return self.as_divs() def as_divs(self): if not self: return u'' return u'<div class="errorlist">%s</div>' % ''.join([u'<div class="error">%s</div>' % e

How to use inbulit django templatetags in google-app-engine

家住魔仙堡 提交于 2019-12-05 11:17:06
I am trying to use Django in built templatetags like markup and humanize in my google app , but its not working. I added markup and humanize in the INSTALLED_APPS. Still not working. How to use that? Here is how to do it for humanize, others should be similar. At the end of the controller that invokes your template there is a function that looks like: def main(): run_wsgi_app(application) Add the following two lines just after def main(): from google.appengine.ext.webapp import template template.register_template_library( 'django.contrib.humanize.templatetags.humanize') No need to add {% load

Django template cycle for alternating rows - without loop

本小妞迷上赌 提交于 2019-12-05 11:10:12
Perhaps this is a non-question, but how do you make use of the Django {% cycle %} functionality, or something similar, when you're not in a loop? Specifically, I have an HTML table that I'm writing by hand, since it's not the sort of thing you need to do in a loop. I want the rows to alternate, like this: <tr class="{% cycle 'even' 'odd'%}"></tr> <tr class="{% cycle 'even' 'odd'%}"></tr> <tr class="{% cycle 'even' 'odd'%}"></tr> But I'm not using a loop, so this always results in even . I don't want a situation where I want to insert one row later, and then have to change the classes of all

How to output text from database with line breaks in a django template?

人走茶凉 提交于 2019-12-05 10:47:46
问题 I have text saved in a database record that looks like this. This is the text This is on a new line with a space in between When I output it on the Django template, it comes out like this This is the text This is on a new line with a space in between How can I output the text on my django template to reflect the way it appears in the database? 回答1: Use linebreaks or linebreaksbr filter: {{ text|linebreaks }} Or surround the text with <pre>...</pre> . <pre>{{ text }}</pre> 回答2: I linebreaks

How to validate mulitple forms in a single formView class Django

北慕城南 提交于 2019-12-05 10:17:13
问题 I have a formView class as you can see below:- view.py class ThreadForm(FormView): template_name = 'thread.html' form_class = ThreadModelForm success_url = '/success' def form_valid(self, form): # This method is called when valid form data has been POSTed. # It should return an HttpResponse. print form.cleaned_data return super(ThreadForm, self).form_valid(form) def get_context_data(self, **kwargs): context = super(ThreadForm, self).get_context_data(**kwargs) context['second_form'] =

jinja2: How to make it fail Silently like djangotemplate

假如想象 提交于 2019-12-05 10:09:02
Well i don't find the answer I'm sure that it's very simple, but i just don't find out how to make it work like Django when it doesn't find a variable i tried to use Undefined and create my own undefined but it give me problems of attribute error etc. def silently(*args, **kwargs): return u'' class UndefinedSilently(Undefined): __unicode__ = silently __str__ = silently __call__ = silently __getattr__ = silently but when i try this here it fails TypeError: 'unicode' object is not callable : {%for dir_name, links in menu_links.items()%} You are trying to go arbitrarily deep into your undefined

Get the version of Django for application

◇◆丶佛笑我妖孽 提交于 2019-12-05 10:04:38
问题 I am starting a new (actually very old) project which I know is in Django. I am getting lost knowing the exact version of Django it has been build upon. Is there a way I can know the version of Django my application is running? 回答1: The only way is to take a guess. I would start by looking at the created date of the settings.py file (or other base project files) Release dates for versions: 1.0: September 2008. (?) 1.1: July 29, 2009 [1] 1.2: May 17, 2010 [2] 1.3: March 23, 2011 [3] Having in

Compare request.path with a reversed url in Django template

我的未来我决定 提交于 2019-12-05 09:13:36
I understand that request.path will give me the current URL. I am currently working on my base.html template with CSS tabs and I want the template to know which tab is currently "active" and pass class="active-tab" to an <a> tag. So I wanted to do something like <a href="{% url orders_list %}" {% if request.path = reverse('orders_list') %} class="active-tab" {$ endif %} >Orders</a> But I'm sure you can't do that if comparison. I also only want the base (?) URL ignoring any GET parameters. Any suggestions or tips also welcomed. Thanks in advance! You can use a custom template tag from django