django-templates

django: form.fields not iterating through instance fields

倾然丶 夕夏残阳落幕 提交于 2019-12-17 19:29:02
问题 I am trying to iterate through form.fields in a template and for: {% for field in form.fields %} {{ field }}, {% endfor %} I am getting a list of the field names ("name, description...") instead of the html code that is rendered when using the following: {{ form.name }}, {{ form.description }} (the output in this case is: <input id="id_name" type="text" name="name" maxlength="200" /><input id="id_description".... Any hints? Thanks! 回答1: You want to iterate over "form," not "form.fields". The

Django Template does not exist error, although it shows 'file exists'

与世无争的帅哥 提交于 2019-12-17 19:24:33
问题 I am not able to render any html pages in Django 1.7. My 'index.html' is in ' project/seatalloc/templates/index.html ' and my view.py in project/seatalloc/views.py looks like: def index(request): return render(request, 'index.html', dirs=('templates',)) project/project/settings.py has templates dirs set: TEMPLATE_DIRS = ( '/Users/Palak/Desktop/academics/sem3/cs251/lab11/project/seatalloc/templates', ) urls.py: urlpatterns = patterns('', url(r'^seatalloc/', include('seatalloc.urls')), url(r'

Django: Parse JSON in my template using Javascript

南楼画角 提交于 2019-12-17 17:44:23
问题 I have this in my view: string_location = myaddress2 geodata = [] for place, (lat, lng) in g.geocode(string_location,exactly_one=False): geodata.append((place, (lat, lng))) geodata_results = len(geodata) data = {"geodata": geodata, "geodata_results":geodata_results } return render_to_response("business/business_view.html", data, context_instance=RequestContext(request)) How would I "handle" / convert geodata into JSON and pass it to my template so that I can "loop" through it like an array?

How do you limit list objects template side, rather than view side

♀尐吖头ヾ 提交于 2019-12-17 17:33:57
问题 One of the ways to limit objects is to add a limitation to a function like this def ten_objects(): obj = Model.objects.all()[0:10] # limit to 10 return {'objects': obj} However how do you achieve this inside a template rather than inside a view? I know you can filter through objects within a template and limit characters but how do you actually limit the amount of objects displayed in a loop. Via the template. for example the following code will loop through all objects.... <ul> {% for new in

In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin?

会有一股神秘感。 提交于 2019-12-17 16:17:48
问题 I have a workflow for a model in the Django admin that is very similar to the users' workflow. First, I have a form with basic fields and then, a second form with the rest of the data. It's the same workflow as auth.user I need to remove "save and continue" and "save and add another" buttons to prevent the user breakoing the workflow. I have tried to add it as extra_context extra_context = { 'show_save_and_add_another': False, 'show_save_and_continue': False } and pass it through ModelAdmin

How to access outermost forloop.counter with nested for loops in Django templates?

孤街醉人 提交于 2019-12-17 15:37:25
问题 Is it possible to access the forloop.counter for the outermost for loop in the following template in Django: {% for outerItem in outerItems %} {% for item in items%} <div>{{ forloop.counter }}. {{ item }}</div> {% endfor %} {% endfor %} forloop.counter returns the innermost for loop's counter in the above example 回答1: You can use forloop.parentloop to get to the outer forloop , so in your case {{forloop.parentloop.counter}} . 回答2: you can also use with Caches a complex variable under a

How do I get odd and even values in a Django for loop template?

♀尐吖头ヾ 提交于 2019-12-17 15:34:56
问题 I have this code {% for o in some_list %} Now I want to do some stuff if I am on an even line. How can I do that? 回答1: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#divisibleby {% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %} 回答2: In first level cycle: {% cycle 'odd' 'even' %} Reference: Documentation for cycle template tag 回答3: <div class="row"> {% for post in posts %} {% cycle 'odd' 'even' %} {% if cycle == 'odd' %} <div class="col-md-6">Odd posts</div> {%

How do you insert a template into another template?

扶醉桌前 提交于 2019-12-17 15:29:19
问题 I have a very basic template (basic_template.html), and want to fill in the with data formatted using another partial template. The basic_template.html might contain several things formatted using the partial template. How should I structure the code in views.py? The reason I am doing this is that later on the will be filled using Ajax. Am I doing this right? 回答1: You can do: <div class="basic"> {% include "main/includes/subtemplate.html" %} </div> where subtemplate.html is another Django

Django doesn't display newline character when rendering text from database

喜夏-厌秋 提交于 2019-12-17 15:24:45
问题 I am using Django for development, retrieving some text containing a newline character from the database. However, when I render it to a template using a template tag it doesn't show the newline character. What is the problem? 回答1: You have to remember that your templates are producing HTML. In HTML, a newline character is just another white space, it doesn't mean to put the following text onto a new line. There are a number of ways to force new lines in HTML. You can wrap your text with a

Sorting a Django QuerySet by a property (not a field) of the Model

こ雲淡風輕ζ 提交于 2019-12-17 10:48:30
问题 Some code and my goal My (simplified) model: class Stop(models.Model): EXPRESS_STOP = 0 LOCAL_STOP = 1 STOP_TYPES = ( (EXPRESS_STOP, 'Express stop'), (LOCAL_STOP, 'Local stop'), ) name = models.CharField(max_length=32) type = models.PositiveSmallIntegerField(choices=STOP_TYPES) price = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) def _get_cost(self): if self.price == 0: return 0 elif self.type == self.EXPRESS_STOP: return self.price / 2 elif self.type == self