django-templates

Django: Switching to Jinja2?

爱⌒轻易说出口 提交于 2019-12-02 16:05:39
I've got a couple small (500 or 600 lines of template code) Django sites, and I'd like to migrate them to using Jinja2… But I'd like to get some idea of how much work it will be. So, in general, about how much work is it to migrate a small Django site to Jinja2? And is it “worth it”? While it's just my own experience, I found converting from Django to Jinja2 to be worthwhile for the following reasons: The design and implementation of Jinja2 seemed more intuitive to me, both as a software developer and template designer; Jinja2 is more extensible (at least in the ways I've sought to extend my

Django static page?

做~自己de王妃 提交于 2019-12-02 15:36:58
I want to make a static page which will be shown to the user only if he/she clicks on a link provided in one of my models. I can do this by making a Python page alone and calling it, but I want it be called from Django. The user interface should be constructed using the Django API only. Any suggestions? David Grellscheid With the class-based views in newer Django versions, one can use this in urls.py: from django.views.generic import TemplateView url(r'^about', TemplateView.as_view(template_name='path/to/about_us.html'), name='about'), Bypassing views to render a static template, add this line

Django STATIC_URL is empty, images doesn't show

别来无恙 提交于 2019-12-02 15:22:00
问题 I know that there is plenty of questions like that, I read them all and tried everything, however nothing solved my problem. I'm writing something like blog in Django and of course I want to show pictures to users. Pictures are stored at /static/users/<username>/<image> . My setting.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')] , 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context

Django template can't see CSS files

我们两清 提交于 2019-12-02 14:56:14
I'm building a django app and I can't get the templates to see the CSS files... My settings.py file looks like: MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'media') MEDIA_URL = '/media/' I've got the CSS files in /mysite/media/css/ and the template code contains: <link rel="stylesheet" type="text/css" href="/media/css/site_base.css" />` then, in the url.py file I have: # DEVELOPMENT ONLY (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/media'}), but the development server serves the plain html (without styles). What am I doing wrong? -- OK -

how to create counter loop in django template?

爱⌒轻易说出口 提交于 2019-12-02 14:31:37
can any tell me how can I write below code of c in django for(c=0; c<5; c++) //do something i had tried below code but it gives me an error {% for(c=0; c<5; c++)%} <div class="tab-content"> <h1 class="tab" title="title for page 1">Page 1</h1> <p>This is the content of tab 1 on container 1</p> </div> {% endfor %} When you render your template, you may pass range render_to_response('template_x.html', {'range5': range(5)}) And in html template, probably like this {% for i in range5 %} <div class="tab-content"> <h1 class="tab" title="title for page {{i}}">Page {{i}}</h1> <p>This is the content of

In a Django template, how to specify a dictionary key which is itself an attribute?

放肆的年华 提交于 2019-12-02 14:26:21
问题 I'm working on a Django project with a ListView that has both a search form, known in the view's context as search_form , and a filter form, filter_form . The interface looks like this: Both the search_form and the filter_form ultimately change what is returned by the ListView 's get_queryset method. I would like to make it such that when you first apply a filter and then do a search, it will search the filtered results. At the moment, the 'reverse' functionality has already been implemented:

How to render menu with one active item with DRY?

a 夏天 提交于 2019-12-02 14:17:34
I would like to render a constructions like: <a href='/home'>Home</a> <span class='active'>Community</span> <a href='/about'>About</a> Where Community is selected menu item. I have menu with same options for several templates but I would not like to create combinations for each template: <!-- for Home template--> <span class='active'>Home</span> <a href='/comminuty'>Community</a> <a href='/about'>About</a> ... <!-- for Community template--> <a href='/home'>Home</a> <span class='active'>Community</span> <a href='/about'>About</a> ... <!-- for About template--> <a href='/home'>Home</a> <a href='

Using the slice filter with context data from a Django QuerySet

痴心易碎 提交于 2019-12-02 14:12:46
I am trying to split a list from my model across two columns, using this html code in the template: < div class ="col-md-6" > {%for value in object_list %} <ul>< ahref="/sites/{{value.url}}/">{{value.Site}}</a></ul> {% endfor %} I was planning to achieve this with the slice tag to filter the list, e.g.: {%for value in object_list|slice:"10:20" %} It does not work however, and I think it might be because I have context data i.e. {{value.Site}}, instead of just {{Site}} for example. This is the corresponding view: class homeview(ListView): template_name = 'annual_means/home.html' def get

How to add extending template for single page (need to display post in single page)

心已入冬 提交于 2019-12-02 13:14:25
i developed a blog app and i want one display one post on single page. please any body share the process for displaying posts new single page Views code: def index(request): return render(request, 'polls/home.html', {}) def Event(request): events=Events.objects.all() return render(request, 'polls/events.html', {'events':events}) template code: {% block content %} <p>Events page </p> {% for abc in events %} <div> <h2><a href="{% url 'event_detail' pk=event_title.pk % }">{{ abc.event_title }}</a></h2> <p>published: {{ abc.event_release_date }}</p> <p>{{ abc.event_description|linebreaksbr }}</p>

Django Template Language - getting url encoded values

柔情痞子 提交于 2019-12-02 12:53:48
I'm using Django Template language, using load staticfiles . However when I do the following <img src="{% static 'img/category/{{category.category|lower|slugify}}.jpg' %}"> I get the HTML as <img src="/static/img/category/%7B%7Bcategory.category%7Clower%7Cslugify%7D%7D.jpg"> which obviously is not rendering the correct image. However my expected output was <img src="/static/img/category/electronics.jpg"> where category.category = electronics I'm passing category as a ctx variable. Why is it happening like this? You can't have a variable in the static tag, that's why you see these %7B%7B in