I've gone through quite a few django tutorials, and I'm finally getting ready to head out on my own. However, my first non-tutorial program is throwing an error, and I've been banging my head for a couple of days. I expect it to be a very noob problem, because, well, I am.
When I use this view
def todo(request):
latest_list = Item.objects.all()
return HttpResponse(latest_list)
I get
conquer djangocan I do this?learn thislearn this
which are the four items that populate the database. Not very handy since they are concatenated, and they don't appear to be handed off to the template.
When I change my view.py to try to talk to the template using
def todo(request):
latest_list = Item.objects.all()
return render_to_response,('index.html', {"latest_list", latest_list})
I get
'tuple' object has no attribute 'status_code'
Could it be that the model that's returning 'self.task' is limiting the return to only that field? Other tutorial I looked at seemed to return only one value (and returning just 'self' gets me a very similar error.
It could also be that I'm not passing in
Any help that would push me down the correct path would be greatly appreciated.
Greg
My model.py
from django.db import models
class Item(models.Model):
task = models.CharField(max_length=60)
taskNotes = models.CharField(max_length=600)
created = models.DateTimeField(auto_now_add=True)
done = models.BooleanField(default=False)
def __unicode__(self):
return self.task
My views.py
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponse
from myToDo.todo.models import Item
def todo(request):
latest_list = Item.objects.all()
return HttpResponse(latest_list)
My index.html (template)
{% if latest_list %}
<ul>
{% for task in latest_list %}
<li>{{ Item.task }}</li>
{% endfor %}
</ul>
{% else %}
<p>Looks like you're all done</p>
{% endif %}
return render_to_response,('index.html', {"latest_list", latest_list})
Remove that comma affer render_to_response and you should be ok. Reason: the comma makes the return value a tuple object, but need to return an HttpResponse object from a view.
You goofed on your return.
def todo(request):
latest_list = Item.objects.all()
return render_to_response('index.html', {"latest_list", latest_list})
Note the lack of comma after the function name.
Related
I am not able to use the Django model object in Django templates. I want to iterate using the model user in the template and then play with the ActivityPeriod(model) of that user. Please check my code for the clarity:
Here is my code:
views.py
from .models import User,ActivityPeriod
def call_response(request):
user = User.objects.all()
return render(request, "Test/list.html", {"users":user ,"activityperiod":ActivityPeriod})
Test/list.html
{% for user in users %}
'real_name': {{ user.real_name}}},
'activity_periods': {% with activity=activityperiod.objects.get(id =user) %}
{{ activity.start_time }}
{% endwith %}
{% endfor %}
But i am getting an error:
Could not parse the remainder: '(id' from 'activityperiod.objects.get(id'
What is the correct way? Can anyone please share it with me.
Django template don't understand the filter action of Model. This part shoud be in view.
activity=activityperiod.objects.get(id =user)
You should prepare your data and manipulate them before sending to template (a dictionary may help you). And remember that result of action "User.objects.all()" is a list.
views.py
def call_response(request):
user = User.objects.filter(user=request.user)
activityperiod = activityperiod.objects.get(user=user)
context={'user':user,'activityperiod':activityperiod}
return render(request, "Test/list.html",context})
Test/list.html
'real_name': {{ user.real_name}}
'activity_periods':{{ activityperiod.start_time }}
Your question suggests that you think you can a function in the templates like a normal function (ie activityperiod.objects.get(...)).
You can't, the templating system is not made like this (for security reasons amongst others).
You should do something like, in your models:
def call_response(request):
# ! first() not "all()" (if > 1 user, you'll have problem)!
user = User.objects.first()
activityperiod = activityperiod.objects.get(user=user)
return render(request, "Test/list.html",
{"users":user ,"activityperiod":activityperiod})
Below is the code for a project I'm working on. As you can see in the view, I'm running a query for objects in the Risk model that are associated with the primary key in the Url.
The problem is that I can't access the attributes of the object being queried.
Model
class Risk(models.Model):
risk_title = models.CharField(max_length=60)
root_causes = models.ManyToManyField('self',
through='Roots',
symmetrical=False,
related_name='root_cause')
class Roots(models.Model):
causal_risk = models.ForeignKey(Risk, related_name='causes', on_delete=models.CASCADE)
effected_risk = models.ForeignKey(Risk, related_name='effected_risk', on_delete=models.CASCADE)
View:
def view_risk(request, id):
try:
risk = Risk.objects.get(pk=id)
except Risk.DoesNotExist:
raise Http404("Risk does not exist")
roots = Roots.objects.filter(effected_risk=id)
args = {
'risk': risk,
'roots': roots,
}
return render(request, 'risks/risk_detail.html', args)
Template:
{% for root in roots %}
{{ root }}
{% endfor %}
Running the above code returns this:
Roots object (2) Roots object (3)
This should be expected, as these are the entries I've made to the model through the admin interface.
However, when I run:
{% for root in roots %}
{{ root.risk_title }}
{% endfor %}
The screen is left blank.
TLDR
Passing the query from the intermediary table to the template works, but the template can't access the object attributes. How can I run the above query and then access the objects in the template?
UPDATEDIncluded the render portion per JoJo's response. This problem has been solved per my other update.
Thank you in advance!
Note that the view function is missing the return statement, so there is actually no html page rendered at all.
Assuming that the template html is named 'my_template.html', the missing return should look something like:
def view_risk(request, id):
# ...
return render(request, 'my_template.html', args)
Further, as #JonHrovat mentioned, the template needs to call
{{ root.causal_risk.risk_title }}
within the for loop.
A note on efficiency:
If you call root.causal_risk.risk_title within the template with your current query for roots this leads to additional queries, one for each iteration. This can be avoided by using the select-related statement when querying for the roots in the view function:
roots = Roots.objects.select_related('causal_risk__risk_title').filter(effected_risk=id)
I forgot to query the direct attributes of the model. I got what I need with this template:
{% for root in roots %}
{{ root.causal_risk.risk_title }}
{% endfor %}
I am having some issues directing a url pattern through the following type of path: Listview --> Listview2 --> DetailView. I am running into trouble with my url patterns. Here is what I am working with:
app_name = 'ism'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<slug:client_slug>/', views.CostCenterListView.as_view(), name='cost_center_list'),
path('<slug:client_slug>/<slug:cost_center_slug>/', views.cost_center_detail, name='cost_center_detail'),
]
The home page of this app is a list of all clients. Clicking on a client will populate a new page showing a list of that client's sub-clients (cost_center).
Here is part of my template {% url %} call to my final path listed above (/slug/slug/):
{% for cost_center in cost_centers %}
<ul>
<li>{{ cost_center }}</li>
</ul>
{% endfor %}
Adding this along with its accompanying view causes an error:
NoReverseMatch at /ism/cleint_slug/
Can you confirm that my issue has to deal with my {% url %} in my template not remembering the first slug in my url path? My error message seems to indicate that it's trying to find:
.../cost_center_slug
instead of:
.../client_slug/cost_center_slug/
My assumption is that django would magically remember the first part of my url pattern (ie client_slug), but that does not appear to be happening. Do I need to bring in more context variables in my view to allow for the calling of two url variables (also is url variable the right terminology? It doesn't sound right) in my template above?
Here is my full error message ('cffd' is a slug representing a cost-center):
Reverse for 'cost_center_detail' with arguments '('cffd',)' not found.
1 pattern(s) tried:
['ism/(?P<client_slug>[-a-zA-Z0-9_]+)/(?P<cost_center_slug>[-a-zA-Z0-9_]+)/$']
Here is my views.py. I was initially trying to work with a DetailView and its get_object method, but I couldn't get that to work in addition to the function based view I shown
class IndexView(generic.ListView):
template_name = 'ism/index.html'
context_object_name = 'client_list'
def get_queryset(self):
queryset = Client.objects.all()
return queryset
class CostCenterListView(generic.ListView):
template_name = 'ism/costcenter_list.html'
context_object_name = 'cost_centers'
def get_queryset(self):
slug = self.kwargs.get('client_slug')
client = Client.objects.get(slug=slug)
queryset = client.costcenter_set.all()
return queryset
def cost_center_detail(request, client_slug, cost_center_slug):
cost_center = get_object_or_404(CostCenter, slug=cost_center_slug)
context = {'cost_center': cost_center}
return render(request, 'ism/costcenter_detail.html', context)
I think you've got a wrong url. It should be either
{% url 'ism:cost_center_list' client_slug %}
or (you don't provide enough argument to construct cost_center_detail url)
{% url 'ism:cost_center_detail' client_slug cost_center.slug %}
So I'd like to think what I'm doing is fairly common. I'm attempting to display a result set using Django. I have looked online for a while and here is what I've come up with:
models.py{
from django.db import models
from django.conf import settings
class SalesRep(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
class Sales(models.Model):
seller = models.ForeignKey(SalesRep)
sold_on = models.DateField("Sold On")
}
sales_view.py{
from salesteam.models import SalesRep, Sales
from django.views.generic import ListView
class SellerHome(List View):
model = SalesRep
template_name = 'production/sales.html'
#property
def rep_sales(self):
return Sales.objects.filter(SalesRep=self)
}
sales.html{
<div id="total-sales">
{% for sale in SellerHome.rep_sales%}
<li>{{ sale.id }}</li>
{% empty %}
<li>You don't currently have any sales</li>
{% endfor %}
</div>
}
For the sake of completeness I even tried writing just the property out as Sales.objects.all() but still no luck. Thanks for taking a look as I'm fairly new to HTML, Django, and Python. Any help on what I'm doing wrong would be much appreciated. Let me know if you have any questions :)
I'm not sure what tutorial you've followed, but there is a lot of errors in your code... I'm assuming you would like to display all Sales for the logged in user?
In this scenario, I would change the model slightly to add a related_name to Sales foreign key to SalesRep:
class SalesRep(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
class Sales(models.Model):
seller = models.ForeignKey(SalesRep, related_name='sales')
sold_on = models.DateField('Sold On')
Documentation: https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.ForeignKey.related_name
And I would change the views to use DetailView:
from django.views.generic import DetailView
class SellerDetailView(DetailView):
model = SalesRep
template_name = 'production/sales.html'
context_object_name = 'seller'
def get_object(self):
return self.request.user
Docs on DetailView — https://docs.djangoproject.com/en/1.7/ref/class-based-views/generic-display/#detailview
<div id="total-sales">
<ul>
{% for sale in seller.sales.all %}
<li>{{ sale.id }}</li>
{% empty %}
<li>You don't currently have any sales</li>
{% endfor %}
</ul>
</div>
First, I'd recommend avoiding class based views, at least as you are learning Django, since they are not really necessary and needlessly complicated for what you are doing.
Anyway, to answer your question using class-based views, your problem is in your template and your view. You'll want to put your rep_sales logic in a get_queryset, set the model to be Sales, not SalesRep, and then reference object_list in the template. More info.
However, I'd instead recommend writing a normal, function-based Django view. Something like this might get you started:
def view_all_sales(request):
my_sales_rep = SalesRep.objects.get(request.user)
all_my_sales = Sales.objects.filter(seller=my_sales_rep)
return render(request, "production/sales.html", {
"sales": all_my_sales
})
I asked in past time about how generate an url with similar caracterist starting from an ID you said me that is better use slugs to do that. On this time I wanna generate dinamics urls with slugs. My objective is obtain this result:
I have five products that I named cards in models.py (Ysera, Neltharion, Nozdormu, Alexstrasza, Malygos). I need the respective url of each of the products:
localhost:8000/card/ysera
localhost:8000/card/neltharion
localhost:8000/card/nozdormu ... etc.
I try to generate these urls but i dont know if I made a good applying of commands, either I don't know how I can specify the id card like the main name of the card (ysera, neltharion...). I was trying to follow an answer posted here in this community a little blind and this is my "reconfiguration":
Here my views.py:
from django.shortcuts import render_to_response
from django.template import RequestContext
from dracoin.apps.synopticup.models import card
from dracoin.apps.home.forms import ContactForm,LoginForm
from django.core.mail import EmailMultiAlternatives
from django.contrib.auth import login,logout,authenticate
from django.http import HttpResponseRedirect
def shop(request):
tarj = card.objects.filter(status=True)
ctx = {'tarjetas':tarj}
return render_to_response('home/shop.html',ctx,context_instance=RequestContext(request))
def singleCard(request, slug, id):
try:
tarj = card.objects.get(slug=slug, id=id_tarj)
except ObjectDoesNotExist:
tarj = get_object_or_404(card, id=id_tarj)
return render_to_response('home/singleCard.html',ctx,context_instance=RequestContext(request))
My urls.py (I have an urls.py by app and the main urls.py):
url(r'^card/(?P<slug>[-\w]+)/(?P<id_tarj>\d+)/$','dracoin.apps.home.views.singleCard',name='vista_single_card'),
My models.py:
class card(models.Model):
nombre = models.CharField(max_length=100)
descripcion = models.TextField(max_length=300)
status = models.BooleanField(default=True)
def __unicode__(self):
return self.nombre
My common template for all cards:
{% extends 'base.html' %}
{% block title %} Tarjeta {{card.nombre}} {% endblock %}
{% block content %}
<h1>{{ card.nombre }}</h1><br>
<p> {{ card.descripcion }}</p>
{% endblock %}
I don't know if my slug building in views.py was find, I'm convinced that the urls.py is bad but i don't know how build it?
please excuse me if I edit my own question to prolong it, Recently I'm trying to learn django by my side and I have many gaps in my learning
apologizeme in advance my extensive question and if I overlook something.
Thanks!!
This line:
tarj = card.objects.get(slug=slug, id=id_tarj)
tries to load a card object where the id field is set to is id_tarj and the slug field is set to slug. Your model does not have a field named slug. You will need to add one.
A good candidate for it would be a SlugField - https://docs.djangoproject.com/en/dev/ref/models/fields/#slugfield
You will need to make sure that your slug field contains a proper value in each case.