Right now i am trying to pull all of the objects out of my database and display them onto a web page using django and PostgreSQL, the syntax is slightly different due to the use of the Django Mako Plus framework, but the underlying code is the same. The only major difference is that in the template the syntax is changed from {{code here}} to ${code here}.
Here is my view:
from django.conf import settings
from django_mako_plus import view_function
from datetime import datetime
from django.http import HttpResponse, HttpResponseRedirect
from django import forms
from homepage import models as cmod
from django.contrib.auth import authenticate, login
from django.shortcuts import render
from homepage.models import User
# from homepage.models import
from homepage.models import DReferForm
import random, string
#view_function
def process_request(request):
dReferral = DReferForm.objects.all()
context = {
'dReferral':dReferral,
}
# The DMP Equivalent when rendering the page
return request.dmp.render('submissions.html', context)
Here is my template:
<%inherit file="base.htm" />
<%block name="content">
<h1>Testing</h1>
%for i in dRefferal:
<p>Doctor Name: ${i.Fname}</p>
%endfor
</%block>
Whenever I try to load the webpage, I get an object not iterable error.
If I use this exact same code on any other model it works just fine and pulls all the data.
Thank you for all your help.
You have a typo in the template - dRefferal for dReferral.
Related
I checked all the questions and answers in this site, but could not find a solution. I am new in Django, trying to develop a storage model and stucked already in the ListView.
part of my view.py:
from django.shortcuts import render, redirect
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from Storage.models import Storage
from django.http import HttpResponse
class StorageListView(LoginRequiredMixin, ListView):
model = Storage
print(Storage.objects.order_by('-barcode'))
template_name = 'Storage/Storagelist.html'
def get_queryset(self):
return Storage.objects.order_by('-barcode')
I add print statement to check whether I reach the model and
everyting seems normal:
<QuerySet [<Storage: 123>, <Storage: 122>, <Storage: 121>]>
However, I can not reach the context from the template file 'Storagelist.html':
<h3>trial</h3>
{% if storage_list %} <h3>trial</h3> {% endif %}
Just to check access via url configurations, I added 'trial' at the beginning of the template and it also seems normal. I also tried to use context_object_name, but did not help either. Somehow, I can not reach to ListView context data from the template.
My versions of applications are as follows:
Django 2.0.3
Python 3.6.1
Can somebody please help me?
In a ListView I believe the objects will populate into the template as object_list. You might consider using this method to make the template context variable human readable (and also match what you are expecting). In other words, try adding context_object_name = storage_list to your view.
So, I have PyCharm 2017.2.3, and Python 3.6, and Django 1.11. while practicing on the test project, I tried to render to my index.html in view.py under my app. Below is the piece of code that I am talking about:
def index(request):
db_Conn = Album.objects.all()
template = loader.get_template('/music/index.html')
context = {
'db_Conn': db_Conn,
}
return HttpResponse(template.re)
Inside return HttpResponse, I can get until template, but when I use a period after template to use the render() sub-function, I do not get any suggestion from PyCharm for render() instead, I could see two other functions which not relevant to my post.
Can sombody help me on this. My learning is halted due to this.
Try this. I assume music is app name, so your template file should be in, music/templates/index.html
from django.shortcuts import render
from django.utils import timezone
def index(request):
return render(request, 'index.html', {'start_date':timezone.now()})
If not understood refer to this repo
I'm working on a site that has some mostly static content that I still want to use Django template tags in. I don't want to have to write a view and add a urlconf entry for each URL, I just want to add templates to a specific folder and have them be rendered and accessible on the web. Is there already a project out there that does this?
Write a catch all view and resolve template dynamically:
from django.shortcuts import render_to_response
import django.template
import django.http
def view_template(request, template_name):
try:
return render_to_response(template_name)
except django.template.TemplateDoesNotExist:
raise django.http.Http404('No such file: %s' % template_name)
And in your url-conf add, towards the end:
url(r'^/(?P<template_name>[\w-]+\.html)$', view_template),
I have a question regarding page_processors.py. I've made a contact page with a form through the mezzanine admin and I have some logic that I'd like executed upon completion of that form. After reading the documentation, I found that creating a page_processors.py module in my app should help take care of that, but the file isn't being touched (ie, I can't get to the debugger inside it). Can anyone here help?
Here's my page_processors.py file:
from django import forms
from django.http import HttpResponseRedirect
from mezzanine.pages.page_processors import processor_for
from mezzanine.forms.models import Form
import requests
#do some testing to see that this code is correct
import pdb;pdb.set_trace()
#processor_for(slug="contact")
def form_view(request):
pass
Turns out you shouldn't name your app "site", interferes with an existing module named site (facepalm).
i am trying to do a simple Django application where employee list is read from database and displayed. for that i defined the models and entered the values to database through Django admin. But while trying to display data from database i am stuck with an error, "ViewDoesNotExist at /employeeProfile/ : Could not import task.employeeDetails.views. Error was: cannot import name emp_profile ".I am relatively new to django,so please help me to solve this. i will paste the code here.enter code here
VIEWS.PY
from django.shortcuts import render_to_response
from django.contrib.auth.models import*
from task.employeeDetails.models import *
from django.conf import settings
from django.http import HttpResponse
from task.employeeDetails import emp_profile
def employeeList(request):
tableList = EmployeeDetails.objects.all()
return render_to_response('employeeList.html', {'emp_list': tableList})
def employeeProfile(request):
profile = EmployeeDetails.objects.all()
return render_to_response('employeeProfile.html',{'emp_profile':emp_profile})
URLS.PY
(r'^employeeProfile/$','task.employeeDetails.views.employeeProfile'),
TEMPLATE
<html>
<body>
{%for emp in emp_profile%}
<tr> <td>{{ emp.userName }} {{ emp.designation }} {{ emp.employeeID }}</td> </tr><td>
{%endfor%}
</table></h4>
</body>
</html>
def employeeProfile(request):
profile = EmployeeDetails.objects.all()
return render_to_response('employeeProfile.html',{'emp_profile':emp_profile})
You named it profile on line 2, and then you tried to put it in the dictionary as emp_profile on line 3.
from task.employeeDetails import emp_profile
What is emp_profile and where exactly is it? from the looks of it, employeeDetails is the name of your directory, so unless emp_profile is a file in employeeDetails/, is defined in employeeDetails/__init__.py (or otherwise imported there), it will throw an import error.
I assume you want:
def employeeProfile(request): profile = EmployeeDetails.objects.all()
return render_to_response('employeeProfile.html',{'emp_profile':profile})
As Yuji pointed out, it looks like emp_profile isn't defined anywhere