django-templates

Put a <a> hyperlink in a django message [duplicate]

怎甘沉沦 提交于 2019-12-21 03:35:36
问题 This question already has answers here : How do I output HTML in a message in the new Django messages framework? (7 answers) Closed 3 years ago . I'm using django messages and i want to put an hyperlink in it. view.py: from django.contrib import messages def my_view(request): messages.info(request,"My message with an <a href='/url'>hyperlink</a>") Obviously, in my page, i see the html code and no hyperlink. How to treat the message as an htlml code ? Hope this is clear. 回答1: Strings in Django

Django - template context processors - breaking my app

瘦欲@ 提交于 2019-12-21 03:32:09
问题 I was trying to set up a template context processor like this article mentions so that I could provide information to every template. I wrote this function in views.py: def items_in_cart(request): """Used by settings.TEMPLATE_CONTEXT_PROCESSORS to provide an item count to every template""" cart, lines = get_users_cart_and_lines(request) return {'items_in_cart': lines.count()} And then I added this line to settings.py: TEMPLATE_CONTEXT_PROCESSORS = ('Store.views.items_in_cart',) But now

Django, global template variables

隐身守侯 提交于 2019-12-21 03:20:49
问题 I have a base template file (base.html) and every other template extends to it and generates content using its blocks. Certain variables, such as nav_obj, are used in the base template file. View: nav_obj = NavigationObject.objects.all() Base template: {% for object in nav_obj %} <a href="{{ object.link }}">{{ object.title }}</a> {% endfor %} At the moment, I need to pass nav_obj in every view. Is there any way to have this sent automatically? 回答1: Write your own context processor. 回答2:

Calling Python function in Django template

自古美人都是妖i 提交于 2019-12-21 02:21:27
问题 Inside a django template I'm trying to call the split function on one of the template variables and then get the last element, so I did something like this: {{ newsletter.NewsletterPath.split('/').-1 }} Unfortunately, it doesn't like the split. Some might suggest that I do the split in the view, but I'm not sure how to do that because I need to do it for all of the records. It would be much easier if I could do it in the template. Is there a way to do this? 回答1: What do you mean by "it doesn

Django template, how to make a dropdown box with the predefined value selected?

浪尽此生 提交于 2019-12-21 01:24:49
问题 I am trying to create a drop down list box with the selected value equal to a value passed from the template values, but with no success. Can anyone take a look and show me what I am doing wrong. <select name="movie"> {% for movie in movies %} {% ifequal movie.id selected_movie.id %} <option value="{{movie.key}}" selected="true">Movie {{movie.id}}: {{movie.name}}</option> {% endifequal %} {% ifnotequal movie.id selected_movie.id %} <option value="{{movie.key}}">Movie {{movie.id}}: {{movie

Django template, how to make a dropdown box with the predefined value selected?

Deadly 提交于 2019-12-21 01:24:26
问题 I am trying to create a drop down list box with the selected value equal to a value passed from the template values, but with no success. Can anyone take a look and show me what I am doing wrong. <select name="movie"> {% for movie in movies %} {% ifequal movie.id selected_movie.id %} <option value="{{movie.key}}" selected="true">Movie {{movie.id}}: {{movie.name}}</option> {% endifequal %} {% ifnotequal movie.id selected_movie.id %} <option value="{{movie.key}}">Movie {{movie.id}}: {{movie

MVC and django fundamentals

て烟熏妆下的殇ゞ 提交于 2019-12-21 01:12:47
问题 Pretty new to this scene and trying to find some documentation to adopt best practices. We're building a fairly large content site which will consist of various media catalogs and I'm trying to find some comparable data / architectural models so that we can get a better idea of the approach we should use using a framework we've never made use of before. Any insight / help would be greatly appreciated! 回答1: "data / architectural models so that we can get a better idea of the approach we should

How to concatenate a string to a number inside a template tag in Django

杀马特。学长 韩版系。学妹 提交于 2019-12-20 21:58:10
问题 I've found a similar question on StackOverflow, but the solution doesn't seem to work for me, unless I'm doing it wrong. I have a ID number, which I'd like to append to a string in a template tag. Here's my attempt: {% with "image-"|add:vid.the_id as image_id %} {# custom template tag to generate image #} {% image vid.teaser_thumbnail alt=vid.title id=image_id %} {% endwith %} But image_id is coming out as empty. What am I doing wrong here? My desired output of image_id would be something

increment a variable in django templates

纵饮孤独 提交于 2019-12-20 19:05:14
问题 All, How Can we increment a value like the following in django templates, {{ flag =0 }} {% for op in options %} {{op.choices}}<input type="radio" name="template" id="template" value="template{{flag++}}"/> {% endfor %} thanks.. 回答1: I don't think it's intended you should alter data in your templates. For in your specific case, you could instead use the forloop.counter variable. For example: {% for op in options %} {{op.choices}}<input type="radio" name="template" id="template{{forloop.counter}

Django pagination and “current page”

二次信任 提交于 2019-12-20 18:29:31
问题 I'm currently developing a Django application which will make use of the infamous "pagination" technique. I'm trying to figure out how the django.core.paginator module works. I have an application with a Question model. I will be listing all of the questions using this paginator. There will be 20 questions per page. def show_question(question_pk): questions = Question.objects.all() paginator = Paginator(questions, 20) page = ... # Somehow figure out which page the question is on return render