django-templates

How do I add multiple arguments to my custom template filter in a django template?

我只是一个虾纸丫 提交于 2019-12-17 04:47:34
问题 Here's my custom filter: from django import template register = template.Library() @register.filter def replace(value, cherche, remplacement): return value.replace(cherche, remplacement) and here are the ways I tried using it in my template file that resulted in an error: {{ attr.name|replace:"_"," " }} {{ attr.name|replace:"_" " " }} {{ attr.name|replace:"_":" " }} {{ attr.name|replace:"cherche='_', remplacement=' '" }} I looked into django's docs and book but only found example using a

Use variable as dictionary key in Django template

落花浮王杯 提交于 2019-12-17 04:27:55
问题 I'd like to use a variable as an key in a dictionary in a Django template. I can't for the life of me figure out how to do it. If I have a product with a name or ID field, and ratings dictionary with indices of the product IDs, I'd like to be able to say: {% for product in product_list %} <h1>{{ ratings.product.id }}</h1> {% endfor %} In python this would be accomplished with a simple ratings[product.id] But I can't make it work in the templates. I've tried using with... no dice. Ideas? 回答1:

How can I get the domain name of my site within a Django template?

↘锁芯ラ 提交于 2019-12-17 04:13:08
问题 How do I get the domain name of my current site from within a Django template? I've tried looking in the tag and filters but nothing there. 回答1: I think what you want is to have access to the request context, see RequestContext. 回答2: If you want the actual HTTP Host header, see Daniel Roseman's comment on @Phsiao's answer. The other alternative is if you're using the contrib.sites framework, you can set a canonical domain name for a Site in the database (mapping the request domain to a

Error 500 in production just when debug is set to False

你说的曾经没有我的故事 提交于 2019-12-14 04:26:20
问题 I am using Django 1.11 in my application. I've implemented the authentication using django-registration and created a profile model to store some user infos: class Profile(models.Model): ... user = models.OneToOneField(User) nick = models.CharField(max_length=50) level = models.PositiveIntegerField(null=True) avatar = models.CharField(max_length=500, null=True, blank=True) ... This model is being created/saved with signals: def create_user_profile(sender, instance, created, **kwargs): if

How to change the layout for templates in a Django Form-Wizard?

╄→尐↘猪︶ㄣ 提交于 2019-12-14 04:04:15
问题 I am using Django's Form Wizard package to create a 4-step form. I have created forms for each of the four steps. It's working fine. In my case I have created 4 separate templates and each step of the form wizard uses its own template. But each of these templates that I have created is substantially the same. Below is the code that is in each one. I cut and pasted this from elsewhere. It works, but I don't fully understand how or why: <form action="" method="post">{% csrf_token %} <table> {{

How to make an external database query iterable?

回眸只為那壹抹淺笑 提交于 2019-12-14 03:45:55
问题 I have the following code: settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'tectcom', 'USER': 'test', 'PASSWORD': '***146***', 'HOST': '', 'PORT': '', }, 'cdr': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'ast', 'USER': '123', 'PASSWORD': '654', 'HOST': '', 'PORT': '', } views.py def cdr_user(request): cursor = connections['cdr'].cursor() calls = cursor.execute('SELECT * FROM cdr') return render_to_response("cdr_user.html", {'result':calls }, context

Django can't find template dir?

心已入冬 提交于 2019-12-14 03:22:12
问题 Originally I had just one app in my Django project, the templates consisted of an index.html a detail.html and a layout.html ... the index and detail files extended the layout. They all lived in the same directory ( [app1]/templates/[app1]/ ) and it could find all the files fine. Now I've got a second app, and I want to re-use the layout.html ... I decided to make a templates dir off the base of the django project, and put it in there. Here's what my directory structure looks like now:

Django test project TemplateDoesNotExist at /

£可爱£侵袭症+ 提交于 2019-12-14 03:17:39
问题 I know this question has been asked many times but even after resolving all the things I am still getting this error. My setup is as follow - settings.py """ Django settings for my_notebook project. Generated by 'django-admin startproject' using Django 1.8.3. For more information on this file, see https://docs.djangoproject.com/en/1.8/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.8/ref/settings/ """ # Build paths inside the project

Manually Rendering Django Form with Validation

China☆狼群 提交于 2019-12-14 03:16:05
问题 I've started to create a form. Basically the form is a "test" of twenty words. The form consists of twenty text fields which I want to contain the definition of a word. The user would input the word. Once complete, the form should validate the data and mark what is correct and what isn't. I've done plenty of modelforms in django, but this one is different. All the data in this form has to be passed through as context. views.py def get_test(request, username='default'): template_name = 'main

Range not working in for loop

坚强是说给别人听的谎言 提交于 2019-12-14 03:06:49
问题 My for loop is not working and I'm not sure why. This is the loop: {% for i in range({{text|length}} + {{images|length}}) %} text and images are querysets I have passed to the template from the view. The error I am getting is: django.template.exceptions.TemplateSyntaxError: 'for' statements should use the format 'for x in y': for i in range({{text|length}} + {{images|length}}) This doesn't make sense to me, as it looks to me as if this does follow the format suggested by the error. 回答1: {%