django-templates

Building HTML with templates versus building it in javascript?

别来无恙 提交于 2019-12-05 08:32:22
I recently started making web apps, and while I make some stuff that works, I'm not sure of the best practices. Up to now, I've mostly used the templating systems of Django, web.py and PHP. But now that I'm using jQuery and nice ajaxy magic to get data from the server without refreshing the client, I'm seeing the advantages of building the HTML directly in javascript (so I can just send a small json object to the client and have him figure out what to change and how). So now I have some bits that are rendered with templates, and some that are built in javascript, and even one horrid case where

Get only one field from django forms in template

无人久伴 提交于 2019-12-05 08:28:26
I have one form : class FormLogin(forms.Form): email = forms.EmailField(max_length=150) name = forms.CharField(max_length=20) How can I put just email field in my template ? I tried this : {{ form.fields.email }} But it returns <django.forms.fields.CharField object at 0x00000000043BCEB8> . You can just use: {{ form.email }} You don't need to use fields . Use: {{ form.email }} 来源: https://stackoverflow.com/questions/13521634/get-only-one-field-from-django-forms-in-template

Check if url matches in template

我与影子孤独终老i 提交于 2019-12-05 08:24:29
Is it possible to check in template that some url match any pattern from urls? This is something you'd normally want to do in a views.py file with the reverse () helper for named URLs with known args or resolve () for paths. If you do need this functionality in a template specifically, here is a hacky solution: @register.simple_tag def urlpath_exists(name): """Returns True for successful resolves()'s.""" try: return bool(resolve(path)) except Resolver404: return False Note : this doesn't guarantee that the URL is valid, just that there was a pattern match. You can use the "as" form of the url

Custom base_site.html not working in Django

旧城冷巷雨未停 提交于 2019-12-05 08:18:33
I am using Nitrous for playing with the Django framework. In tutorial 2 is shown how to change the base_site.html template. I've added in the TEMPLATE_DIRS = ( ) a new line: 'home/action/workspace/mysite/templates', And in base_site.html I changed the site name title of Django Administration into Administration: {% trans 'Administration' %} But I still see no changes on the website. I've tried different TEMPLATE_DIRS like: '~/workspace/mysite/templates', 'home/action/workspace/mysite/', 'home/action/workspace/mysite/templates/', And restarting the server. But I am doing something wrong. Note

How to concisely represent if/else to specify CSS classes in Django templates

我与影子孤独终老i 提交于 2019-12-05 08:01:14
In a Django template, I'd like to add CSS classes to a DIV based on certain "conditions", for example: <div class="pkg-buildinfo {% if v.release.version == pkg.b.release.version %}active{% else %}inactive{% endif %} {% if v.release.version == project.latest.version %}latest{% else %}notlatest{% endif %}"> ( note that v is a loop variable; the whole thing is inside a for loop) The above adds CSS classes "active" or "inactive" and "latest" or "notlatest" based on two conditions. This is however hard to read and verbose. I discovered that the with statement does not support assigning the value of

Facebook like and share button on ajax

天涯浪子 提交于 2019-12-05 07:30:45
Background: - Am using ajax to get a entity called as "foo_posts". In this post am using facebook share and like button {% for post in foo_posts %} <div class="foo"> {{ post }} <div class="fb-like" data-send="true" data-width="450" data-href="http://foo/foo/detail/{{ foo.id }}/" data-show-faces="true"></div> </div> {% endfor %} Now these posts are being populated using Ajax. Problem: - The facebook like and share gets initialized by $(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect

Django 1.8 with Jinja2: Contrib app Admin not working

怎甘沉沦 提交于 2019-12-05 07:24:43
I upgraded to a fresh install of Django 1.8 and began using Jinja2 as it said that it was supported now and Jinja2 has some features I could use in my project. After finishing adapting the templates for my app to Jinja2 and taking advantage of the new features now available, I discovered that the contrib app Admin no longer works. "TemplateDoesNotExist at /admin/login/" So it turns out that contrib app Admin only has templates made for DjangoTemplates and not for Jinja2. I did the naive thing first and made a symlink in [...]/site-packages/django/contrib/admin from templates to jinja2 but the

Django, category and subcategories

女生的网名这么多〃 提交于 2019-12-05 07:20:45
问题 im working category and subcategories with the DataModel, all is fine in this part, but i need to use my category and subcategories in my Menu Nav, im try to use this Jquery menu , and im rendering my menu with subcategories, but im lost with rendering the subcategories in the way: <ul> <li> <a href="#">Category</a> <!--subcategories--> <span>Subcategory 1 </span> <span>Subcategory 2 </span> ... </li> .... .... </ul> My problem: in the datamodel: with the 'self', i dont know how ill do a for

Return Django Models for Template Rendering after an Ajax Request

孤街浪徒 提交于 2019-12-05 07:09:52
问题 I would like to create an AJAX-based search for my webpage. So far I am able to send the form data and make the appropriate call to my Django model. What I am having a hard time with is just sending the Queryset back and having it rendered using the Django templating system. Your help/advice is greatly appreciated. Here is the code I am working with. views.py if request.is_ajax(): if request.method == 'POST': format = 'json' mimetype = 'application/json' try: q = request.POST['obj'] o =

Is django can modify variable value in template?

牧云@^-^@ 提交于 2019-12-05 06:31:09
I want to write template that render somethings only one time. My ideas is create flag variable for check it is first time or not ? My code {% with "true" as data %} {% if data == "true" %} //do somethings ** set data to "false" ** {% else %} //do somethings {% endif %} {% endwith %} I don't know How to change variable in django template ? (Is it possible ?) Or Better way for do this. Thank you NIKHIL RANE This can be done with a Django custom filter django custom filter def update_variable(value): data = value return data register.filter('update_variable', update_variable) {% with "true" as