django-paging unhashable type - django

paginators.py (of django-paging package):
try:
_page = EndlessPage(list(self.object_list[bottom:top]), number, self)
this line gives a `TypeError: unhashable type error, although that object_list comes from a standard QuerySet that can be used with [bottom:top] without a problem.
template:
{% with paginate(request, my_queryset) as results %}
{{ results.paging }}
{% for result in results.objects %}
{{ result }}
{% endfor %}
{{ results.paging }}
{% endwith %}`
view:
my_objects = BetterPaginator(queryset,25)
page = 1
context = { 'my_queryset': my_objects.get_context(page) }

Related

How to pass a key of dictionary into anchor tag in django template?

I have this following dictionary:
results[calendar.month_name[date_cursor.month]] = [e,z]
My url is:
url(r'^company/(?P<pk>\d+)/purchasedelete/(?P<month>\d+)/date/(?P<pk3>\d+)/$',views.purchase_register_datewise,name='purchase_datewise'),
My view:
def purchase_register_datewise(request,month,pk,pk3):
company_details = get_object_or_404(Company, pk=pk)
selectdatefield_details = get_object_or_404(Selectdatefield, pk=pk3)
result = Purchase.objects.filter(Company=company_details.pk, date__month=month, date__gte=selectdatefield_details.Start_Date, date__lt=selectdatefield_details.End_Date)
context = {
'company_details' : company_details,
'selectdatefield_details' : selectdatefield_details,
'result' : result,
}
return render(request, 'stockkeeping/purchase/Purchase_Register_Datewise.html', context)
In my template:
{% for key, value in data %}
<tr>
<td><center>{{ key }}</center></td>
{% if value.0 == 0 %}
<th><center></center></th>
{% else %}
<td><center>{{ value.0 }}</center></td>
{% endif %}
<th><center></center></th>
{% if value.1 == 0 %}
<th><center></center></th>
{% else %}
<td><center>{{ value.1 }} Dr</center></td>
{% endif %}
</tr>
{% endfor %}
When I try to do month={{ key }}
It throws me a error like this Could not parse the remainder: '{{' from '{{'
Can anyone tell me how to pass the value of key in my template.
Thank you

create view of oneToMany object in related object view

I have a questionnaire, containing many questions with their answers. My main objective is to create a webpage where I can show the details of a questionnaire with the list of rules (question/answer) and in the bottom of the page I want to call the create rule page:
def create_rule_view(request, id, sc_id):
if request.method == "POST":
input = InputForm(request.POST)
answer = AnswerForm(request.POST)
rule = RuleForm(request.POST)
if rule.is_valid() and input.is_valid() and answer.is_valid():
r = rule.save()
i = input.save(commit=False)
a = answer.save(commit=False)
i.rule_id = r
i.save()
a.rule_id = r
a.save()
question = input.cleaned_data["patterns"]
else:
input = InputForm()
answer = AnswerForm()
rule = RuleForm()
return render(request, "rule/create_rule.html", {
'rule': rule,
'input': input,
'answer': answer
})
def detail_scenario(request, id, sc_id):
object = get_object_or_404(Scenario, id=sc_id)
# TODO : add rule in the same view
create_rule_div = create_rule_view(request, id, sc_id)
print("content", create_rule_div)
context = {
'scenario': object,
'create_rule_div': create_rule_div
}
return render(request, "scenario/detail_scenario.html", context)
This is rule_create.html:
{% block content %}
<form method="POST"> {% csrf_token %}
<h2>Create Rule</h2>
{{ rule.name}}
{{ input.patterns }}
{{ answer.text }}
<input type="submit" value="Save Rule"/>
</form>
{% endblock %}
This is detail_senario.html:
{% block content %}
<h2>Scenario {{ scenario.id }}</h2>
<p>Scenario for : {{ scenario.chatbot_id }}</p>
<p>Name: {{ scenario.name }}</p>
<p>Description: {{ scenario.description }}</p>
<p>State: {{ scenario.state }}</p>
{% for rule in scenario.rule_ids.all %}
<li>{{ rule }}</li>
{% endfor %}
<div>{% block rule %}
{{ create_rule_div.content }}{% endblock %}
</div>
{% endblock %}
when I call url of detail_scenario I got an html code in navigator like this:
How can I fix this?
Thanks.

How can I judge the object if is exists in the templates?

In the template:
<h4>
{% if data.wine_one %}
{{ data.wine_one.title }}
{% elif data.news_one %}
{{ data.news_one.title }}
{% endif %}
</h4>
I promise the data.wine_one is exists, because in the views.py I have print out it.
But in the templates it do not shows up the data.wine_one.title, and I use the data.wine_one != None can not judge it too.
EDIT
In the views.py:
def getData():
banner = models.Banner.objects.filter(fk_info=1)
info = models.Info.objects.all().first()
aboutus = models.AboutUs.objects.all().first()
winery = models.Winery.objects.all()[:3]
winery_consult = models.WineryConsult.objects.all()[:4]
data = {
'banner': banner,
'info': info,
'aboutus': aboutus,
'winery': winery,
'winery_consult': winery_consult,
}
return data
def productdetails(request, nid):
data = getData()
wine_one = models.Winery.objects.filter(id=nid).first()
data['wine_one'] = wine_one
print (data['wine_one'].title) # there ouput the "gaoliangjiu"
return render(request, 'article_list_content.html', data)
You've misunderstood how the template context works.
There's no element called data in the template; that's just the local variable you're using in the view to build up the context. In the template, you just reference the keys of that object directly. So it should be:
{% if wine_one %}
{{ wine_one.title }}
{% elif news_one %}
{{ news_one.title }}
{% endif %}

How can I use a variable to lookup in a tuple? (Django)

I have a tuple, such that
{{VISIBILITY_CHOICES.1.1}}
Outputs "hello".
I have model "task" with a attribute "visibility_status", and in a loop, say all the iterations of task.visibility_status outputs 1
{{task.visibility_status}}
Outputs 1.
How do I use this task.visibility_status inside the lookup of the tuple? Something like VISIBILITY_CHOICES[task.visibility_status][1] in a different language.
I'm very new to django... Thanks a lot.
edit:
The code I was running:
{% for task in tasks %}
<div class="post">
<h1>{{ task.subject }}</h1>
<div class="date">
<p>Due: {{ task.due_date }}</p>
<p>Assigned to: {{task.assigned_to}}</p>
</div>
<p>{{ task.text_area|linebreaks }}</p>
{% with args=""|add:task.visibility_status|add:",1" %}
<p>Visibility Status: {{VISIBILITY_CHOICES|get_index:args}}({{ task.visibility_status }})</p>
{% endwith %}
<p>Case Status: {{ task.case_status }}</p>
<div class="date">
<p>Created: {{ task.created_date }} by {{ task.author }}</p>
</div>
</div>
{% endfor %}
Although the builtin name tuple may not have any syntactic meaning in a template, I'll use my_tuple in its place in the code below.
I've used with to create a context that builds the args for indexing my_tuple (i.e task.visibility_status and 1): :
{% with x=task.visibility_status|stringformat:"s" %}
{% with args=x|add:",1" %}
{{ my_tuple|get_index:args }}
{% endwith %}
{% endwith %}
And in the custom template filter, I've splitted and recreated the arguments and used indexing in plain python to return the item at the index:
from django import template
register = template.Library()
#register.filter
def get_index(my_tuple, args):
arg1, arg2 = args.split(',') # split on the comma separator
try:
i = int(arg1)
j = int(arg2)
return my_tuple[i][j] # same as my_tuple[task.status][1]
except:
return None

Could not parse the remainder django template

I have a condition within a loop in my template like this:
{% for message in message_trash %}
<td><a href="#">
{% if request.session.user_email == message.message_user_reciever.user_email %}
{{ message.message_user_reciever.user_firstName }} {{ message.message_user_reciever.user_lastName }}
{% elif request.session.user_email == message.message_user_sender.user_email %}
{{ message.message_user_sender.user_firstName }} {{ message.message_user_sender.user_lastName }}
{% endif %}
</a><small>Friends</small></td>
{% endfor %}
but i don't know why i get this error when applying the url?
TemplateSyntaxError: Could not parse the remainder: '==message.message_user_reciever.user_email' from 'request.session.user_email==message.message_user_reciever.user_email'
Update:
this is the view and variables that i render to the template:
def trashMessages(request, userId):
if isMessageOwner(request, userId):
user = Users.objects.get(user_id=userId)
message_trash = Messages.objects.filter(Q(message_user_reciever= user, message_sender_type='TRASH') | Q(message_user_sender=user, message_reciever_type='TRASH'))
return render(request, 'navigation_messages.html', {'user': user, 'message_trash': message_trash, 'type': 'trash'})
On testing your code out, I can only replicate your issue is by swapping:
{% if request.session.user_email == message.message_user_reciever.user_email %}
for
{% if request.session.user_email ==message.message_user_reciever.user_email %}
Note the missing space. Is the snippet in your question exactly as it is in your template?