django-templates

Pass request.user to view without altering the url

本秂侑毒 提交于 2019-12-04 04:47:48
问题 I'm trying to write a view where the current logged in user's information is retrieved. My view is written as below, and it works perfectly fine as long as I pass the user to the view in the URL. def index(request, username, template="index.html"): user = get_object_or_404(User, username__iexact=username) expression_qs = Expression.objects.all().order_by('-created') album_qs = Album.objects.all().filter(head__isnull=False, is_public=True).order_by('-created') user_following = user

Django - how to get the contents of a {% block %} tag from a template

守給你的承諾、 提交于 2019-12-04 04:22:37
问题 I got this far: >>> some_template = get_template_from_string( ... load_template_source( ... 'some_template.html', ... settings.TEMPLATE_DIRS)) ... >>> blocks = some_template.nodelist.get_nodes_by_type(BlockNode) >>> blocks[0] <Block Node: another_block. Contents: [<Text Node: '\nThis one is really cool'>, <Block Node: sub_block. Contents: [<Text Node: '\nI\'m a sub-block.\n\t'>]>, <Text Node: '\n'>]> >>> # Right there is when I realized this wasn't going to be fun. You see, the contents of a

Django templates syntax error

一个人想着一个人 提交于 2019-12-04 03:48:50
问题 Is there any problem with the syntax in the following code, there is a error as Invalid block tag: 'else' {% ifequal chat_profile 1 %} {% extends "chatprofile/chat_profile1.html" %} {% else %} {% extends "chatprofile/chat_profile.html" %} {% endifequal %} 回答1: The documentation states: If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won't work, otherwise. So consider using a design where you can use {% include %} instead. 回答2:

Sorting objects in template

大城市里の小女人 提交于 2019-12-04 03:47:27
问题 Let these models: class Category(models.Model): name = models.CharField(max_length=20) class Word(models.Model): name = models.CharField(max_length=200) votes = models.IntegerField(default=1) categories = models.ManyToManyField(Category, null=True, blank=True) this view: def main_page(request): words = Word.objects.all() categories = Category.objects.all() return render(request, "main_page.html", {'words': words}) and this template: {% for category in categories %} {% for word in category

render_to_response gives TemplateDoesNotExist

浪子不回头ぞ 提交于 2019-12-04 03:46:55
I am obtaining the path of template using paymenthtml = os.path.join(os.path.dirname(__file__), 'template\\payment.html') and calling it in another application where paymenthtml gets copied to payment_template return render_to_response(self.payment_template, self.context, RequestContext(self.request)) But I get error TemplateDoesNotExist at /test-payment-url/ E:\testapp\template\payment.html Why is the error coming? Edit : I have made following change in settings.py and it is able to find the template, but i cannot hardcode the path in production, any clue? TEMPLATE_DIRS = ("E:/testapp" ) It

{% trans “string” %} not working on templates but {% trans variable %} does

送分小仙女□ 提交于 2019-12-04 03:42:32
I'm quite new to Django and I'm working on a project with i18n, the thing is that I have translated some variables using .manage.py makemessages / compilemessages on my template file, but when I use {% trans "my string" %} I got the same "my string" for all the languages. What am I doing wrong? Here's the code for the views.py and the idioma.html views.py: #some code here... def idioma(request): output = _("Mensaje en espanol") return render_to_response( 'idioma/idioma.html', { 'idioma' : output }, context_instance = RequestContext(request) ) idioma.html {% load i18n %} < form action="/i18n

Django Custom Inclusion Tags

痞子三分冷 提交于 2019-12-04 03:41:38
问题 I'm trying to build my own template tags. I have no idea why I getting these errors. I'm following the Django doc's. This is my app's file structure: pollquiz/ __init__.py show_pollquiz.html showpollquiz.py This is showpollquiz.py: from django import template from pollquiz.models import PollQuiz, Choice register = template.Library() @register.inclusion_tag('show_pollquiz.html') def show_poll(): poll = Choice.objects.all() return { 'poll' : poll } html file: <ul> {% for poll in poll <li>{{

Django Templates and MongoDB _id

你离开我真会死。 提交于 2019-12-04 03:33:04
问题 Variables and attributes may not begin with underscores: 'value._id' How does one reference the _id of an item gotten from MongoDB in Django Templates? 回答1: Custom template filter would help: from django import template register = template.Library() @register.filter(name='private') def private(obj, attribute): return getattr(obj, attribute) You can use it this way: {{ value|private:'_id' }} 来源: https://stackoverflow.com/questions/24936601/django-templates-and-mongodb-id

Could not parse the remainder Django

[亡魂溺海] 提交于 2019-12-04 03:26:05
问题 I've been trying to write a custom template tag to shorten links with bitly, I've attached the code and error I've been getting below. I've tried to look into the documentation provided by Django but cannot see what it is that I am doing wrong. I've put my templatetag in the following layout: scribbler/ models.py templatetags/ __init__.py shortenlink.py views.py the custom tag file that I've written: shortenlink.py from django import template from django.conf import settings from urllib

Django - http code 304, how to workaround in the testserver?

久未见 提交于 2019-12-04 03:08:42
问题 I have a CSS code that generates http 304: [08/Nov/2011 15:22:07] "GET /site_media/logo1.gif HTTP/1.1" 304 0 How can I get a workaround using the Django test server? Any clues? Best Regards, 回答1: The 304 code is not an error. You don't need a workaround. It simply means that the static file has not changed since your browser last accessed it For more information see the Wikipedia explanation of 3xx status codes. 回答2: If you're getting 304 with files you did change, you can force reloading in