django-templates

Override existing Django Template Tags

こ雲淡風輕ζ 提交于 2019-12-18 03:56:20
问题 Is it possible to override an existing Django Template Tag or is it necessary to customize the template file and create a new Template Tag? 回答1: Yes. As django is basically a python library (as with everything in python) you can over-write anything you'd like. It's not clear exactly what you'd like to do but it really is pretty easy to roll-your-own templatetag, the docs are pretty clear: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-tags This is

Filtering a model in a CreateView with get_queryset

徘徊边缘 提交于 2019-12-18 03:43:41
问题 I'm trying to filter a model with get_queryset() and it seems to work in the view but not in the template. My view : class FolderCreate(CreateView): fields = ['name', 'parent'] template_name = 'Form/folder_create.html' def get_queryset(self): folders = Folder.objects.filter(owner=self.request.user) print folders # ==> return [<Folder: Folder>, <Folder: Another folder>] return folders def form_valid(self, form): self.object = form.save(commit=False) self.object.owner = self.request.user return

Defining “global variable” in Django templates

痞子三分冷 提交于 2019-12-18 03:43:31
问题 I'm doing something like: {% extends 'base.html' %} {% url myapp.views.dashboard object as object_url %} {% block sidebar %} ... {{ object_url }} ... {% endblock %} {% block content %} ... {{ object_url }} ... {% endblock %} Django documentation says url templatetag can define a variable in context, but I don't get any value for object_url in the following blocks. If I put the url templatetag at the beginning of each block, it works, but I don't want to "repeat myself". Anyone knows a better

why is logged_out.html not overriding in django registration?

旧城冷巷雨未停 提交于 2019-12-18 03:16:10
问题 I am using built-in django login and logout. In my Project/urls.py i have added url's for both login and logout. from django.conf.urls import include, url from account import views from django.contrib.auth import views as auth_views from django.contrib import admin urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^$',views.index,name='Index'), url(r'^accounts/login/$',auth_views.login,name='login'), url(r'^accounts/logout/$',auth_views.logout,name='logout'), url(r'^accounts

Can we append to a {% block %} rather than overwrite?

。_饼干妹妹 提交于 2019-12-17 22:04:47
问题 In my core.html I have a block labeled javascript. It would be great if I can append more lines to this block without overwriting everything in it. 回答1: {% block javascript %} {{ block.super }} ... more content ... {% endblock %} See: Django documentation - Template inheritance 回答2: Using block.super works fine when extending a template but not as well when including one, ie: {% extends "base.html" %} vs. {% include "partial.html" %} Say you want to include a template in the middle of your

Django template - Is there a built-in way to get current date as type 'date' instead of type 'str'?

旧时模样 提交于 2019-12-17 21:35:50
问题 I know I can get the current date as a str in a Django template (using the template tag now), like this: {% now "Y-m-d" as today_str %} <p>{{ today_str }}</p> But I cannot use that for comparissons: {% now "Y-m-d" as today_str %} {% for elem in object_list %} {% if elem.date < today_str %} {# WRONG: this compares 'date' and 'str' #} <p>{{ elem.pk }} before today</p> {# do some other rendering #} {% endif %} {% endfor %} Possible solutions: I know I can pass a context variable to the template,

display templates value in datatable (django)

独自空忆成欢 提交于 2019-12-17 21:23:29
问题 t = Template(" my name is {{ my_name }}") c = Context({ "my_name": patient.name }) //(like patient.age,patient.height.......... i want to display 8 fields of form in my datatable.) d = t.render(c) I want to display the template value in datatable. Here is my HTML code, where I'm trying but could getting exactly. Please help. {% for patient in PatientInfo %} <tr><td>{{patient.name }}</td> <td>{{patient.uhid }}</td> <td>{{patient.age }}</td> <td>{{patient.gender }}</td> <td>{{patient.height }}<

Django url template with query parameters

故事扮演 提交于 2019-12-17 20:48:32
问题 I'm trying to pass query parameters via my view into a link, but it escapes me on how to actually achieve this in a good way. My template is as following: <a class="link-button" href="{% url 'videos:index' %}?tag={{ tag }}&page={{ next }}">Next</a> This returns what I want: http://127.0.0.1:8000/videos/?tag=1&page=2 While this works, it's quite fragile, does not handle None values and there must be a better way of doing this. I tried to pass this via the url template tag but it did not seem

How to pass extra_context when redirecting in Django

为君一笑 提交于 2019-12-17 20:42:35
问题 My views.py: @login_required def some_views(request): if request.method == 'POST': form = AddressCreateFrom(request.POST) if form.is_valid(): name = form.cleaned_data['Address'] ip_value = form.cleaned_data['value'] user_list = get_username(name) address_create = form.save() extra_context = { 'user_list': user_list } return redirect_to(request, url=address_create.get_absolute_url()) else: form = AddressCreateFrom() extra_context = { 'form':AddressCreateFrom(initial={'user': request.user.pk})

render_to_response or redirect changes the template elements in Django 1.8

可紊 提交于 2019-12-17 20:39:17
问题 I'm trying to check if email id entered by user is existing in the database table, if existing - I would like to route to 'prof.html' template otherwise just show a message in the login.html template. Both the conditions are working fine. However, the problem is when I use redirect() or render_to_response() - the destination template elements like div, input etc., are being changed automatically (prof.html in this case) ? Can we also send the context information to destination template ?