django-templates

template tag inside object TextField

喜欢而已 提交于 2019-12-20 06:28:25
问题 I need custom template tag inside model.TextField value. Value from the object text field have something like "lorem ipsum dolor {% mytag %}" but "mytag" is not rendered as template tag. It is registered in the library as tag and loaded on the page and I have {{ object.textfield|safe }} filter. Is it possible at all? 回答1: As Django's template engine can easily be used anywhere in your code you should be able to do something like this: from django.template import Context, Template rendered =

Django Ajax Form submit wrongly redirect to another page

半城伤御伤魂 提交于 2019-12-20 06:28:23
问题 When I use ajax to submit a comment form in Django,the page will redirect to a blank page shows me the success data: {"status":"success", "msg":"添加成功"} ,but not stay in current page.I want the page stay in current page and show me the new comment. Here is my update_comment view: def update_comment(request, news_pk): news = get_object_or_404(News, id=news_pk) comment_form = CommentForm(request.POST or None) if request.method == 'POST' and comment_form.is_valid(): if not request.user.is

ajax with django forms

萝らか妹 提交于 2019-12-20 06:26:26
问题 can i add the ajax code with django? i have created a simple registraion form that have 5 fields . i wish to disply the each fields in different pages but in a single window . it means by using next button 5 pages want to disply in a single window. same time all content of each page i want add to my database. is this possible in django with ajax.. my codes are as follows : #view from django.shortcuts import render_to_response from registration.models import UserDetails from forms import

Django QuerySet returns nothing

偶尔善良 提交于 2019-12-20 06:12:13
问题 I have a list of countries, they all have there own url www.example.com/al/ for example. There is a list of cities for every country but the object_list is empty My View: class CityOverview(generic.ListView): template_name = 'shisha/pages/country_index.html' model = City def get_queryset(self, *args, **kwargs): country_id = self.kwargs.get('country_id') return City.objects.filter(country__name=country_id) My Model: class Country(models.Model): COUNTRY_CHOICES = ( ('al', 'Albania'), ('ad',

Ajax request not working properly in django

試著忘記壹切 提交于 2019-12-20 05:56:07
问题 JS code for ajax request <script type="text/javascript"> $(document).ready(function(){ console.log("IN") var cartUpdateForm = $('.js_cart_update_form') cartUpdateForm.submit(function(event){ event.preventDefault() var thisform = $(this); var method = thisform.attr("method"); var api = thisform.attr("api"); var data_ = thisform.serialize(); var current = window.location.href console.log(method,api,current,data_) $.ajax({ url:api, data:data_, type:method, success:function(data){ console.log(

How to create link for one of the category in Django

做~自己de王妃 提交于 2019-12-20 05:50:43
问题 I'm coding a news website.I have 'category' in News model. I want to get all the news in one of the categories named 'opinion' in my index.html. And create detail page link for each of them. I can the title ,author, etc of the news mentioned above .But my brain really junks,I don't know how to create a link points to opinion_new.html or news_detail.htlm for each of them.I have a link for regular news to point to news_detail.htlm. If you don't quite understand what I'm asking, please also read

How to access the dynamic key in Django template? [duplicate]

岁酱吖の 提交于 2019-12-20 04:53:22
问题 This question already has answers here : Django template how to look up a dictionary value with a variable (7 answers) Closed last year . Please see the following the code: {% for row in df_src.iterrows %} <tr > <td><input type="checkbox"></td> {% for col in columns %} <td class="redrow">{{row.1.col}}</td> {% endfor %} </tr> {% endfor %} Here in {{row.1.col}} where col can be any value like NAME , PHONE , etc. When I access it like {{row.1.PHONE}} I get the value in html, however when I

Django: Get the form name or id from template

醉酒当歌 提交于 2019-12-20 04:09:31
问题 If you have multiple forms from django import forms class NameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100) class SecondNameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100) is there a way that, in your template, you can figure out if form in inside context belongs to NameForm or SecondNameForm I have a custom widget that in its html, it uses the id="" identifier (which should be unique in the whole html). I want to assign the

How to iterate a queryset list in a django template

荒凉一梦 提交于 2019-12-20 03:05:26
问题 Is there a way to iterate this list of querysets in the template? [<Director: Roman Polanski>, <Director: Alfred Hitchcock>, <Director: Steven Spielberg>, <Director: David Lynch>] I tried using a list syntax, but the django template language doesn't seem to accept lists, also. Thank you all. 回答1: Django's template language does of course accept lists! Here is what the code in your template should look like: {% for director in director_list %} {{ director }} {% endfor %} By the way: What you

including css in Django

一曲冷凌霜 提交于 2019-12-20 02:12:07
问题 I'm new to Django, and i'm having hard time including css styles in a template. I read this and tried to do the same but it's not working for me. my Template: {% load static %}<html><head><link href="{% get_static_prefix %}/style.css" rel='stylesheet' type='text/css' /></head><body> the HTML i get: <head><link href="C:/Users/Nayish/workspace/am/src/am/static/style.css"rel='stylesheet'type='text/css' /></head> Note that this is the folder containing my css. Thanks, Boris. 回答1: I am guessing