I have a models.py:
class Part(models.Model):
Party_number = models.CharField(max_length=10)
Film = models.CharField(max_length=5)
viesws.py:
from django.shortcuts import render
from django.views.generic import ListView
from description.models import Part
class PartyNumView(ListView):
template_name = 'part_list.html'
model = Part
def partList(self, request):
my_var = Part.objects.all()
return render(request, 'part_list.html', {"my_var": my_var})
And HTML template part_list.html:
{% extends 'base.html' %}
{% load staticfiles %}
{% load static %}
{% block activeButton %}
<li class="active">Описание партий</li>
<li>Ic и QE</li>
{% endblock %}
{% block tableName %}
Список партий
{% endblock %}
{% block content %}
{% for object in my_var %}
{{object.Party_number}}
{% endfor %}
{% endblock content%}
My question is why part of code which consist cycle "for" does not working? i.e. objects of Party_number does not displayed on html page
UPDATE
I change object.Party_number to {{object.Party_number}}, but whatever it does not working
You have defined a custom method, partList, but it is not being called from anywhere. The method is pointless and you should delete it.
If you want to add data to the template context in a class-based view, you need to define get_context_data. However there is no reason to do that here as it would just do what ListView does for you anyway. You should use the variable that the view automatically populates, which is object_list.
{% for object in object_list %}
{{ object.Party_number }}
{% endfor %}
Related
Hello guys am building a search tool for postgres in django and am getting a lot of errors my output is in this way.
<QuerySet [<Bindll: HIV-1 protease>, <Bindll: HIV-1 protease>, <Bindll: HIV-1 protease>
my code is
#Search resuts page.
{% extends 'Search/base.html' %}
{%block content %}
<center>
<br/><br/><br/><br/>
<h1>{% if ChemSearched %}
{{ChemSearched}}
<br>
<br>
{% for Bindll in tarname %}
{{tarname}}<br>
{% endfor %}
</h1></strong>
{% else %}
<strong>No Entry</strong>
{% endif %}
Copyright (c)
</center>
{% endblock %}
My database name in Bindll.
#Views.py page
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import Bindll
def search_result(request):
if request.method == "POST":
ChemSearched = request.POST.get('ChemSearched')
tarname = Bindll.objects.filter(targetnameassignedbycuratorordatasource__contains=ChemSearched)
return render(request, 'Search/search_result.html',
{'ChemSearched':ChemSearched,'tarname':tarname})
else:
return render(request, 'Search/search_result.html',{})
using Postgresql and gitbash if related.
Thanks in advace.
In the forloop you need to access the iterated variable not the actual context
{% for Bindll in tarname %}
{{Bindll}}<br>
{% endfor %}
according to the flask-admin docs I can extend the main flask-admin dashboard by creating the file templates/admin/index.html and extending admin/master.html. The HTML would look like this:
{% extends 'admin/master.html' %}
{% block body %}
<h1>HELLO</h1>
{% endblock body %}
But i can't find any information on how to extend the model CRUD pages: List, Edit and Create. I need to extend the Create and Edit User page so i can add js code to the form template.
Is there a template where i can extend just like admin/master.html example?
Just found it in flask-admin docs. I had to create templates/edit_user.html and templates/create_user.html. For list_users is also the same, theres is an example in the docs.
In edit_user.html
{% extends 'admin/model/edit.html' %}
{% block body %}
<h1>User Edit View</h1>
{{ super() }}
{% endblock %}
In create_user.html
{% extends 'admin/model/create.html' %}
{% block body %}
<h1>Create View</h1>
{{ super() }}
{% endblock %}
and then add this to the User model View:
class UserView(ModelView):
edit_template = 'edit_user.html'
create_template = 'create_user.html'
admin.add_view(UserView(User, db.session))
As for DOC, this is the default command:
admin = Admin(app, name='microblog', template_mode='bootstrap3')
Add your own CSS here /static/css/main.css:
{% block head_css %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css', _external=True) }}" ></link>
{% endblock %}
I'm wanting to modify the options on a template according to the date.
The code I'm suspecting will look something like this:
{% for campaign in filter.qs %}
{% if campaign.event_date__gte=datetime.today() %}
<code>
{% endif %} {% endfor %}
I believe my issue is that I'm trying to write it like a view - I'm just not too sure how to edit this within the template and not the views.py
You can do so with a property method by adding it to your model class.
import datetime
today = datetime.datetime.today()
class ModelClass(models.Model):
# fields
#property
def is_future_date(self):
return self.event_date >= today
{% for campaign in filter.qs %}
{% if campaign.is_future_date %}
# <code>
{% endif %}
{% endfor %}
But in case you have multiple different dates that require comparison, or you want to know whether it's a past, present future date, writing a property method for each model is NOT THE BEST ONE. So instead of property method, I would suggest writing a tag filter, then import it to your template, so you will be able to compare dates wherever in your template for whatever instance.
Here an example:
from django import template
register = template.Library()
import datetime
#register.filter
def is_future_date(date):
return date >= datetime.datetime.today()
To use it in your template, call it that way
{% for campaign in filter.qs %}
{% if campaign.event_date|is_future_date %}
<code>
{% endif %}
{% endfor %}
Another date in your template:
{% if another_instance.date|is_future_date %}
# I am able to do it with whatever model
# no need to create multiple property methods
{% endif %}
i think sample solution for is to create model property
class YOUMODEL():
#property
def is_today_or_after(self):
return self.event_date > datetime.today()
and then use in the template:
{% for campaign in filter.qs %}
{% if campaign.is_today_or_after %}
<code>
{% endif %} {% endfor %}
I have a download page on a Django site that I want to serve for both users who are logged in and who aren't. Instead of having a user_download.html and login_download.html, I want to have a single download.html that conditionally extends the correct base.
However, I get an error when I use the following code.
{% if user.is_authenticated %}
{% extends 'user_base.html' %}
{% else %}
{% extends 'login_base.html' %}
{% endif %}
{% block content %}
<h2>Downloadable content</h2>
...
{% endblock %}
The error I receive is
TemplateSyntaxError at /download/
Invalid block tag: 'else'
What's wrong with the else? I tried
{% if user.is_authenticated %}
{% extends 'user_base.html' %}
{% else %}{% if AnonymousUser.is_authenticated %}
{% extends 'login_base.html' %}
{% endif %}{% endif %}
{% block content %}
<h2>Downloadable content</h2>
...
{% endblock %}
but this didn't work, either.
Thanks,
erip
The {% extends %} tag supports variables. See the doc for reference.
def my_view(request):
if request.user.is_authenicated
base_template_name = 'user_base.html'
else:
base_template_name = 'login_base.html'
# Pass base template name to the renderer
return render_to_response('your_template.html', {'base_template_name':base_template_name})
Template (please note that the value is not quoted):
{% extends base_template_name %}
...
You're getting an error because extends needs to be defined at the top of the template. extends controls template inheritance: you are basically creating a subclass from some parent class, which is why extends needs to be the first thing in the template.
Imagine writing a class, and in the __init__() you said something like
class DoesntKnowWhereToInheritFrom(object):
def __init__():
if something:
self.inherits_from(x)
else
self.inherits_from(y)
The compiler/interpreter would freak out.
The common way to do what you are trying to do here is to check for is_authenticated in the view, and then render the appropriate template.
I have a view in a Django 1.4 project:
def index(request):
print reverse('menus_index')
latest_menu_list = Menu.objects.all().order_by('name')
return render_to_response('menus/index.html', {'latest_menu_list': latest_menu_list})
This works as expected and prints out the reversed URL which is /menus/.
Inside of the index.html template (which is called by this view) I have:
{% url menus_index %}
Which causes a NoReverseMatch at /menus/ error. Reverse for '' with arguments '()' and keyword arguments '{}' not found.
My application's urls.py is:
urlpatterns = patterns('menus.views',
url(r'^$','index', name='menus_index'),
url(r'^(?P<menu_id>\d+)/$','detail', name='menus_detail'),
)
Which is included in my project's urls.py file.
What am I doing wrong?
Update:
Here is the full index.html template code:
{% extends "base.html" %}
{% load url from future %}
{% block title %}
Menu Index
{% endblock %}
{% block content %}
{% if latest_menu_list %}
<ul>
{% for menu in latest_menu_list %}
<li>{{ menu.name }}</li>
{% endfor %}
</ul>
{% else %}
<p>No menus are available.</p>
{% endif %}
{% endblock %}
Answer: use {% url 'menus_index' %}. That {% load url from future %} makes the quotes a requirement per https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url
Maybe this not menus_index? Paste full template code this.
You should use add variable for reverse, somethink like this: {% url "menus_index" menu.slug %}