django-templates

How to render menu with one active item with DRY?

时光怂恿深爱的人放手 提交于 2019-12-03 00:53:24
问题 I would like to render a constructions like: <a href='/home'>Home</a> <span class='active'>Community</span> <a href='/about'>About</a> Where Community is selected menu item. I have menu with same options for several templates but I would not like to create combinations for each template: <!-- for Home template--> <span class='active'>Home</span> <a href='/comminuty'>Community</a> <a href='/about'>About</a> ... <!-- for Community template--> <a href='/home'>Home</a> <span class='active'

How to loop over form field choices and display associated model instance fields

故事扮演 提交于 2019-12-03 00:53:15
I have a ModelForm with a multiple choice field. The choices are populated instances of Hikers belonging to a specific Club. I want to customize the way my form displays, by displaying the choices in a table where the 1st column contains checkboxes, and a few more columns display the details of each hiker. So for example the columns are (checboxes, name, age, favourite hiking trail). I'm not sure how to approach this. How do I access and display the form field choices with it's associated model instance fields in my template. Anybody know of the Django way to do this? #models.py class Club

Why and When to use Django mark_safe() function

被刻印的时光 ゝ 提交于 2019-12-02 23:33:21
After reading the document, the mark_safe() still seems a myth. I guess it is related to CSRF stuff. But why and when shall the mark_safe() be used? Here is the documentation mark_safe(s)[source]¶ Explicitly mark a string as safe for (HTML) output purposes. The returned object can be used everywhere a string or unicode object is appropriate. Can be called multiple times on a single string. For building up fragments of HTML, you should normally be using django.utils.html.format_html() instead. String marked safe will become unsafe again if modified. For example: Django is a framework, which

Using the slice filter with context data from a Django QuerySet

匆匆过客 提交于 2019-12-02 23:05:51
问题 I am trying to split a list from my model across two columns, using this html code in the template: < div class ="col-md-6" > {%for value in object_list %} <ul>< ahref="/sites/{{value.url}}/">{{value.Site}}</a></ul> {% endfor %} I was planning to achieve this with the slice tag to filter the list, e.g.: {%for value in object_list|slice:"10:20" %} It does not work however, and I think it might be because I have context data i.e. {{value.Site}}, instead of just {{Site}} for example. This is the

Easy Way to Escape Django Template Variables

非 Y 不嫁゛ 提交于 2019-12-02 22:57:45
For a new project we're writing documentation about the Django template system. We use Django for the documentation project itself too, so Django picks up all our example variables in the sample code and tries to render them. The only way we found to get around this is to use {% templatetag %} , but that makes our code really unreadable. Is there maybe a way to make Django ignore all template variables in a specific section? Due to limitations in the Django template lexer (like being a kludgy hack), this is impossible. However, if you are willing to put your example code in separate files, you

django & the “TemplateDoesNotExist” error

非 Y 不嫁゛ 提交于 2019-12-02 22:50:56
Ive got an as it seems common beginners problem. im working on my first django project and when I set up my view I get an "TemplateDoesNotExist" error. Ive spend lots of hours on this now - and I know theres lots of topics on it but nothing helped me till now. I hope I can supply all the information needed so an advanced django user can probably directly see what Im doing wrong. im using the developement server. and windows 7 & sqlite3. this is the error I get: TemplateDoesNotExist at /skates/ allsk8s.html Request Method: GET Request URL: http://127.0.0.1:8000/skates/ Django Version: 1.4.3

How to add extra data to a Django MPTT model for display in template?

喜夏-厌秋 提交于 2019-12-02 21:57:58
问题 This question is related to this one. My Django Model: from mptt.models import MPTTModel, TreeForeignKey class myModel(MPTTModel): myIntA = models.IntegerField(default=0) myParent = TreeForeignKey('self', null=True, blank=True, related_name='children') My View: myModelList = myModel.objects.all() for i in range(len(myModelList)): myModelList[i].myIntB = i return render( request, 'myApp/myTemplate.html', Context( { "myModels": myModelList, } ) ) Is the above legal? You can see that I added a

Django setting for default template tag output when variable is None?

萝らか妹 提交于 2019-12-02 21:57:17
I am looking for a django setting or programmatic way to make all django template tags show the empty string when the value is None. For example, imagine that I have some django template: {{cat}} chases {{mouse}} if both cat and mouse are None, it will render as: None chases None I am aware that I can set each one using {{cat|default:""}} or {{mouse|default_if_none:""}} However, I am looking for some sort of setting that would allow me to set the default for all tags, without explicitly adding |default:"" to every tag. I am also aware of a setting called TEMPLATE_STRING_IF_INVALID . However,

django template turn array into HTML table

痴心易碎 提交于 2019-12-02 21:03:56
I have a list of 16 results, let's call it "results". I want to arrange them in a 4 x 4 table. Using the django template, how can I do this? (It doesn't seem like cycle would help me here) <table> {% for r in results %} ...? {% endfor %} </table> Thanks!! You can use the cycle tag for this. <table> {% for r in results %} {% cycle '<tr>' '' '' '' %} <td>{{r.content}}</td> {% cycle '' '' '' '</tr>' %} {% endfor %} </table> Would output something like... <table> <tr> <td>result 1</td> <td>result 2</td> <td>result 3</td> <td>result 4</td> </tr> <tr> <td>result 5</td> <td>result 6</td> <td>result 7

How can I use Django Social Auth to connect with Twitter?

流过昼夜 提交于 2019-12-02 20:58:04
I'm trying to use the Django Social Auth package to connect with Twitter, but I'm having difficulty understanding how exactly to do this as I can't find any examples. I am assuming that Django Social Auth is the best package to use for this purpose. I've looked at a few examples that use Facebook, and from this have added the following to my settings.py file: AUTHENTICATION_BACKENDS = ( 'social_auth.backends.twitter.TwitterBackend', 'django.contrib.auth.backends.ModelBackend', ) # overwriting default templates TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.static', 'django