django-templates

Customising tags in Django to filter posts in Post model

拈花ヽ惹草 提交于 2019-12-12 03:59:49
问题 Quick question - I'm not sure what would be the correct way to handle this. Essentially I wish to define a custom tag which handles some logic then returns all posts of model Post which has the is_featured field set to True. I have tried a number of avenues to get this working, but none have worked. My last coherant "guess" was the following: templatetags/blog_tags.py: @register.inclusion_tag('blog/post/featured_posts.html') def show_featured_posts(count=4): """Return 4 of the most recent

How to pass context from view to action in html file in django?

只谈情不闲聊 提交于 2019-12-12 03:42:49
问题 I am passing context from view.py file def change_password(request,pk=None): user = MyUser.objects.get(pk=pk) if request.method == "POST": form = PasswordChangeForm(user=user, data=request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return render(request, "nurse/change_password.html", {"user_id":user.id}) // passing user_id as context I can see this value in my html file <div class="form-group"> <label class="control-label allign col-sm-3" > {{user_id}}

Displaying 2 values in a record array using Django template for loop

末鹿安然 提交于 2019-12-12 03:21:41
问题 I have a numpy record array with 2 values per slot in the array.The name and offset, I want to display both of these values side by side using a Django template. Python code import numpy as np from django.template import Template, Context, loader from django.conf import settings dtype={ 'names' : ('name','offset'), 'formats' : ('U20','U20')} instance= np.zeros(3,dtype) instance[0]=('xga_control_reg','008') instance[1]=('i_cmd_REG','012') instance[2]=('i_ee_cmd_reg','016') t=Template(The

Django template slice

北战南征 提交于 2019-12-12 03:02:45
问题 {% with start=0 end=entries.number|add:"2" %} {{ paginator.page_range|slice:"start:end" }} {{ start }}, {{ end }} {{ paginator.page_range|slice:"0:3" }} {% endwith %} Why Django 1.5 template engine produces the following output for the above code: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 0, 3 [1, 2, 3] 回答1: What about using the code, Luke ? https://github.com/django/django/blob/master/django/template/defaultfilters.py @register.filter("slice", is_safe=True) def slice_filter(value, arg): """

What is wrong with my Django templating use of if's?

让人想犯罪 __ 提交于 2019-12-12 03:01:15
问题 {% for url in urls %} <a id="URL_url_{{ url.id }}" class="edit_rightclick" title="RIGHT click to edit." href="{% if ":" not in url.url %}http://{% endif %}{{ url.url }}">{{ url.url }}</a> <span class="delete" id="URL_{{ url.id }}">&#10008;</span>   {% endfor %} The heuristic is intended to prepend the value of a partial or complete URL like google.com, under the assumption that sometimes people will paste a full browser URL, and sometimes people will type out google.com and never type 'http:/

How can I save the data even if their value in a form is null?

别来无恙 提交于 2019-12-12 02:24:54
问题 If I not complete the form for "annoinizio" or "annofine" or "stagioni" or "totepisodi" django plays form.is_valid() and returns False. How can I save the data even if their value is null? Models: class Tvserie(models.Model): titolo = models.CharField(max_length = 100) titolo_originale = models.CharField(max_length = 200) annoinizio = models.IntegerField(null=True, blank=True) annofine = models.IntegerField(null=True, blank=True) stagioni = models.IntegerField(null=True, blank=True)

Django Ajax search will not work

两盒软妹~` 提交于 2019-12-12 01:58:33
问题 I've been following this tutorial: https://www.youtube.com/watch?v=jKSNciGr8kY I am just completely stuck, I have gone through my code line by line and still cannot figure out what is wrong. Whenever I search, for a document it always returns No Documents available!! instead of the documents I'm searching for. Heres the view for it: def search(request): if request.method == 'POST': search_text = request.POST['search_text'] else: search_text = '' if search_text: documents = Document.objects

Django 1.10 Templating/staticfiles not showing any images

寵の児 提交于 2019-12-12 01:56:30
问题 I am trying to enable Django templating in 1.10 and it is not behaving correctly. Anything I have referenced to be called from the static files (./manage.py collectstatic) is not showing where referenced in the html. I have it imported in my views: from django.shortcuts import render, render_to_response from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth.forms import UserCreationForm from django.template import loader from django.contrib.auth.decorators import

How do I pass Django's urlize template filter its autoescape parameter?

我们两清 提交于 2019-12-12 01:33:25
问题 Django urlize docs say: The urlize filter also takes an optional parameter autoescape . If autoescape is True , the link text and URLs will be escaped using Django's built-in escape filter. The default value for autoescape is True . I'd like to use this parameter, but the documentation says nothing about how to actually pass a keyword argument to a template filter. Is it possible, and if so, how do I do that? 回答1: Apparently urlize gets that keyword argument from current autoescape settings,

How to configure Django views and urls to render specific templates

瘦欲@ 提交于 2019-12-12 01:27:47
问题 When I bring up 127.0.0.1:8000, the current page that show up is something.html template. I would need to make it appear index.html at first launch, then when I click on other parts,it should go to 127.0.0.1:8000/something.html (or 127.0.0.1:8000/myapp/something.html). What would be the structure to achieve this? I frequently get error message : The current URL didn't match any of these. Currently, my structure is project ---myapp ---admin.py ---models.py ---url.py ---views.py ---static --