django-templates

How do I correctly extend the django admin/base.html template?

醉酒当歌 提交于 2019-12-10 12:32:26
问题 This seems like it should be simple but I must be doing something wrong. I've extended admin templates for individual apps before, but this is the first time I've tried extending to modify something across the board. I want to change the color of the help text across the entire admin, so I want to extend the extrastyle block of the base.html template. So in my main templates folder I created admin/base.html with this code in it: {% extends 'admin/base.html' %} {% block extrastyle %} {#

Passing a variable to display in django template

情到浓时终转凉″ 提交于 2019-12-10 12:25:19
问题 Hi I'm new to django and there's something I can't figure out. How exactly can I display a variable set in a different file? choices.py: testvar= 'This is a test variable' views.py from .choices import testvar template.html {{testvar}} Is that it? The import seems to work properly but the string doesn't show. Thanks. 回答1: Your view has to be something like: from django.shortcuts import render def index(request): testvar = 'value' return render(request, 'template.html', {'testvar': testvar})

getting template syntax error in django template

别等时光非礼了梦想. 提交于 2019-12-10 12:18:18
问题 my code in view : tracks = client.get('/tracks', order='hotness', limit=4) artwork_url=[] for track in tracks: artwork_url.append(str(track.artwork_url).replace("large", "t300x300")) val={"tracks":tracks,"artwork_url":artwork_url} return render_to_response('music/tracks.html',val) in .html {% for track in tracks %} <li> <div class="genre-image"> <img src="{{ artwork_url[forloop.counter] }}"> </div> {% endfor %} Error: Exception Type: TemplateSyntaxError Exception Value: Could not parse the

Django form textarea doesn't appear in template

泪湿孤枕 提交于 2019-12-10 12:13:56
问题 I'm new in Django and I'm trying to create a form. The thing is that when I render the form, the textfield doesn't appear, but the button does. The project is a twitter-like app, the form is where the user puts the text to then post it. forms.py: from django import forms class TweetForm(forms.Form): text = forms.CharField(widget=forms.Textarea(attrs={'rows': 1, 'cols': 85}), max_length=160) country = forms.CharField(widget=forms.HiddenInput()) view.py: class PostTweet(View): def post(self,

Can I make a view using UpdateView and DeleteView together?

梦想的初衷 提交于 2019-12-10 11:55:49
问题 I am looking for some information about generic views in django. I want a page that will render a form for an object. On my page I want one submit button to update the object and another submit button that will delete the object. Is it a possible to do this using a single view? 回答1: Yes this is possible. It is possible create a view to handle update and delete functionality using both class based views and function based views. You will find it much easier using function based views. (I'm not

Django template inheritance breaks site layout

梦想与她 提交于 2019-12-10 11:55:39
问题 I'm currently using django 1.3.1, trying to implement template inheritance, to minimize the amount of duplicated code. I'm also using twitter-bootstrap v2.0. I have a base html-file, containing the navbar. It contains a single {% block %} which is below the navbar, inside a div-tag, like this: <div class="container"> {% block content %} {% endblock %} <footer> <p>Designad och skapad av Johan Rende, jrende@kth.se, 2012</p> </footer> </div> This is what the site looks like without inheritance,

How turn {{ fieldset.fields }} in Django template into a string?

*爱你&永不变心* 提交于 2019-12-10 11:46:06
问题 What does Django return with using {{ fieldset.fields }}? How can I make it a string? In my template, I have this: {% for fieldset in adminform %} <li> {{ fieldset.fields }} </li> {% if "nanoadded" in fieldset.fields %} <li> nanoadded is here </li> {% else %} <li> nanoadded is NOT here </li> {% endif %} {% endfor %} Here is what is returned: [('arri', 'aconcentration', 'acat', 'anotes', 'agtlt', 'id'), ('nanoadded', 'response', 'select_charc')] nanoadded is NOT here So I am assuming that the

How Django's url template tag works?

梦想的初衷 提交于 2019-12-10 11:38:56
问题 How does {"% url 'news_id' %"} work? I do have a url pattern url(r'^((?:\w|-)+)/$', 'comments.views.home', name='news_id') but I still get NoReverseMatch . It says Reverse for 'news_id' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['((?:\w|-)+)/$'] views.py from django.shortcuts import render from comments.models import User from django.http import HttpResponseRedirect, HttpResponse from django.template import RequestContext, loader, Context from comments

Django: form to query database

别说谁变了你拦得住时间么 提交于 2019-12-10 11:35:51
问题 I want a user to be able to perform the following query: Fetch all the people, without Phd, with full time contract with a contract within two dates. that translates in Django: Contract.objects.filter( person__is_doctor = False, type_contract = 'full', starting_date__gte = start_date, ending_date__lte = end_date ) How can I make a form/view/template to allow the user enter both start_date and end_date and show the results? models class Person(models.Model): name = models.CharField(max_length

How to access multidimensional dictionary on Django template

时光总嘲笑我的痴心妄想 提交于 2019-12-10 11:34:09
问题 I am trying to access a multidimensional dictionary in a Django template. I am able to view first level keys, but since second level keys I cannot see anything. In example dictionary is composed in this way: dictionary = {} dictionary[first_level] = {} dictionary[first_level][second_level] = {} ... and so on From Django template I use: {% for flk in dict %} <!-- Using nested for from the following, no output is shown --> {% for slk in dict.flk %} <th>First level key : {{ flk }} Second level