When a user registers for my app.I receive this error when he reaches the profile page.
The 'image' attribute has no file associated with it.
Exception Type: ValueError
Error during template rendering
In template C:\o\mysite\pet\templates\profile.html, error at line 6
1 <h4>My Profile</h4>
2
3 {% if person %}
4 <ul>
5 <li>Name: {{ person.name }}</li>
6 <br><img src="{{ person.image.url }}">
Traceback Switch back to interactive view
File "C:\o\mysite\pet\views.py" in Profile
71. return render(request,'profile.html',{'board':board ,'person':person})
I think this error happens because my template requires a image and seen he just registered he can't add a image unless he go to the edit page and adds a page then he can access the profile page.
My profile.html
<h4>My Profile</h4>
{% if person %}
<ul>
<li>Name: {{ person.name }}</li>
<br><img src="{{ person.image.url }}">
</ul>
{% endif %}
My Profile function at views.py
def Profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
board = Board.objects.filter(user=request.user)
person = Person.objects.get(user=request.user)
return render(request,'profile.html',{'board':board ,'person':person})
I tried this solution by creating a 2 instance of Person object and separating them at my template with a if but it didn't succeed.
<h4>My Profile</h4>
{% if person %}
<ul>
<li>Name: {{ person.name }}</li>
</ul>
{% endif %}
{% if bob %}
<ul>
<br><img src="{{ bob.image.url }}">
</ul>
My solutions to the Profile function
def Profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
board = Board.objects.filter(user=request.user)
person = Person.objects.get(user=request.user)
bob = Person.objects.get(user=request.user)
return render(request,'profile.html',{'board':board ,'person':person,'bob':bob})
I'm been reading the documentation for Built-in template tags and filters I think a solution here is to use ( and ) template tag but I can't seem to use it properly.
How can I configure this template to make picture an option. If their are no picture leave it but display the persons name.
Thank you for helping me
bob and person are the same object,
person = Person.objects.get(user=request.user)
bob = Person.objects.get(user=request.user)
So you can use just person for it.
In your template, check image exist or not first,
{% if person.image %}
<img src="{{ person.image.url }}">
{% endif %}
The better approach which would not violate DRY is to add a helper method to the model class like:
#property
def image_url(self):
if self.image and hasattr(self.image, 'url'):
return self.image.url
and use default_if_none template filter to provide default url:
<img src="{{ object.image_url|default_if_none:'#' }}" />
My dear friend, others solvings are good but not enough because If user hasn't profile picture you should show default image easily (not need migration). So you can follow below steps:
Add this method to your person model:
#property
def get_photo_url(self):
if self.photo and hasattr(self.photo, 'url'):
return self.photo.url
else:
return "/static/images/user.jpg"
You can use any path (/media, /static etc.) but don't forget putting default user photo as user.jpg to your path.
And change your code in template like below:
<img src="{{ profile.get_photo_url }}" class="img-responsive thumbnail " alt="img">
Not exactly what OP was looking for, but another possible solution would be to set a default value for ImageField:
class Profile(models.Model):
# rest of the fields here
image = models.ImageField(
upload_to='profile_pics/',
default='profile_pics/default.jpg')
you have two choices :
first one:
in the model field try to put a default image value , like this :
PRF_image = models.ImageField(upload_to='profile_img', blank=True, null=True , default='profile_img/925667.jpg')
the second one (recommended) :
add a custom method inside your class model like the following , to return PRF_image url if exist or return empty string if not :
PRF_image = models.ImageField(upload_to='profile_img', blank=True, null=True )
#property
def my_PRF_image(self):
if self.PRF_image :
return self.PRF_image.url
return ''
and inside your template you can use :
{{ your_object.my_PRF_image }}
i hope this helpful .
You can also use the Python 3 built-in function getattr to create your new property:
#property
def image_url(self):
"""
Return self.photo.url if self.photo is not None,
'url' exist and has a value, else, return None.
"""
if self.image:
return getattr(self.photo, 'url', None)
return None
and use this property in your template:
<img src="{{ my_obj.image_url|default_if_none:'#' }}" />
Many way to solve this issue
Try below code
models.py # under Person class
#property
def imageURL(self):
if self.image:
return self.image.url
else:
return 'images/placeholder.png'
html file
<img src="{% static person.imageURL %}" class="thumbnail" />
Maybe this helps but my database didn't save on of the pictures for the object displayed on the page.
As that object in models.py has blank=False and also I am looping through object, it constantly gave an error until I added a replacement picture in the admin for the database to render.
This error also arises when any one or more items doesn't have an image added and the rest items do. To fix this:
class Product(models.Model):
pname = models.CharField(max_length=30)
price = models.IntegerField()
img = models.ImageField(null = True,blank = True)
def __str__(self):
return self.pname
#property
def imageURL(self):
try:
url = self.img.url
except:
url=''
return url
I had a similar problem , but my issue was with the form in HTML template. if you don't set the enctype = "multipart/form-data" attribute then it does not upload the image hence the reason for the error
Related
The admin returns this error when trying to add an instance to one of my models. The model itself has a correct str() method and contains no instances yet. Also tried replacing the str() method with a static method or removing it altogether. No luck.
The error seems to point towards something going wrong in the history part of the admin. Stacktrace points to line 33.
Error during template rendering
In template /Users/snirp/juis/snirpdrive/glotto/venv/lib/python3.6/site-packages/django/contrib/admin/templates/admin/change_form.html, error at line 33
__str__ returned non-string (type NoneType)
23 {% endblock %}
24 {% endif %}
25
26 {% block content %}<div id="content-main">
27 {% block object-tools %}
28 {% if change %}{% if not is_popup %}
29 <ul class="object-tools">
30 {% block object-tools-items %}
31 <li>
32 {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
33 {% trans "History" %}
34 </li>
35 {% if has_absolute_url %}<li>{% trans "View on site" %}</li>{% endif %}
36 {% endblock %}
37 </ul>
38 {% endif %}{% endif %}
39 {% endblock %}
40 <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" novalidate>{% csrf_token %}{% block form_top %}{% endblock %}
41 <div>
42 {% if is_popup %}<input type="hidden" name="{{ is_popup_var }}" value="1" />{% endif %}
43 {% if to_field %}<input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}" />{% endif %}
These are the relevant parts of my models.py and admin.py
class UserContent(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, related_name='%(class)s_creator')
updated_by = models.ForeignKey(User, related_name='%(class)s_updater')
class Meta:
abstract = True
class Linetrans(UserContent):
line = models.ForeignKey(Line)
translation = models.ForeignKey(Translation)
text = models.CharField(max_length=400)
#def __str__(self):
# return self.text
class Meta:
ordering = ['line']
and
admin.site.register(Linetrans)
Other model classes are very similar and do not return an error. The error also occurs when the Linetrans is added as an inline to another admin class.
edit / update: I commented out all other str() methods in my model and sure enough the error seems to go away. Now trying to pinpoint the issue.
Turns out that there was an unexpected empty CharField in a related model. Leaving this an an answer, because it might help others.
Troubleshoot the issue by systematically commenting out the __str__() methods of your models until you find the offending model. Work from there to identify the offending record(s).
I had a similar issue. The problem was that the primary key for one row was null ( I dont know how that happened). I couldnt delete the row because of cascade issues. so I had to change the str mmethod to somthing like this.
def __str__(self):
if self.customerName==None:
return "ERROR-CUSTOMER NAME IS NULL"
return self.customerName
Check out the class that you have referred to in
models.ForeignKey
The problem lies in the
__str__()
method of one of those classes.
Then, add
or ''
in the return statement of that class like this.
def __str__(self):
return self.title or ''
def __str__(self):
return str(self.topic)
Use str to make it type string.
Then it's not not-string type.
For this error you can use two solutions
first solution: you can comment it out below code
def __str__(self):
return self.id
to
#def __str__(self):
# return self.id
secound solution: you can return the string from the object
def __str__(self):
return str(self.id)
You can also delete the instances in the model causing this problem from the django shell python manage.py shell . This helps when you are dealing with a model with lots of related models like mine.
Typically this problem is caused by altering model fields and
migrating, such that the instances are incompatible with each other
and related fields
If you are trying to use __str__() to return the value of a ForeignKey or a OneToOneField this would return error
Example:
def __str__():
return self.user # where user is a key to other model class
It should be:
def __str__():
return self.user.__str__()
# Or basically any other way that would explicitly unbox the value inside the related model class.
In my case I had the same issue and its origin was that: for the field used in the __str__ functions, has null values saved in the data base.
So I can solve the issue doing somethings like this:
update table
set field_for_STR_use = 'any value'
where field_for_STR_use is null or field_for_STR_use = ''
after that, I only refreshed my view and the problem had disappeared.
There was one of the foreign reference which was returning non string field. I changed it to string field or you can change to str(foreign-key-return-element)
name = models.CharField(max_length=200, unique=True) # name of the article
parent = models.ForeignKey('self',on_delete=models.DO_NOTHING,related_name='category_parent_item', default=None, null=True) # Store child items
created_at = models.DateTimeField(auto_now=True) # Creation date
updated_at = models.DateTimeField(auto_now=True) # Updation Date
user = models.ForeignKey(get_user_model(),
on_delete=models.DO_NOTHING,related_name='category_update_user', default=None, null=True) #User who updated the Article
def __str__(self):
return self.name ----------> this was an integer field initially in foreign reference
that's works for me:
def __str__(self):
return f"{self.category}"
Just use "f" string
In my case, I had a different model that was called by another one that had the default snippet from VSCode as:
def __str__(self):
return
This was causing the error, even when it was showing in the log another place.
Toto_tico is right, although the above error can also occur when you fail to return a str property of your django model within the str(self) method:
Class model and(models.Model):
Class Meta:
verbose_name = "something"
title = models.CharField(max_length=100)
email = models.CharField(max_length=100)
#Error instance:
def __str__(self):
self.title
#Right way
def __str__(self):
return self.title
So it is highly advised to return the str property within the
def str(self)
method to avoid errors in the Django admin panel when trying to make changes to a particular data within a model table:
def __str__(self):
return self.property
I hope this will help someone.
I have a class called Features in my models.py. In my html, I am displaying a list on the right that excludes two of these Features, one is the active feature that has been selected, the other is the most recently added since they are the main content of my page. The remaining Features in the list are displayed by date and do show what I am expecting.
Now, I want to single out the first, second and third Features (title only) in THAT list so I can place them in their own separate divs - because each has unique css styling. There are probably numerous ways of doing this, but I can't seem to figure any of them out.
This is a link to my project to give a better idea of what I want (basically trying to get the content in those colored boxes on the right.)
I'm just learning Django (and Python really), so thanks for your patience and help!
HTML
{% for f in past_features %}
{% if f.title != selected_feature.title %}
{% if f.title != latest_feature.title %}
<h1>{{ f.title }}</h1>
{% endif %}
{% endif %}
{% endfor %}
VIEWS
def feature_detail(request, pk):
selected_feature = get_object_or_404(Feature, pk=pk)
latest_feature = Feature.objects.order_by('-id')[0]
past_features = Feature.objects.order_by('-pub_date')
test = Feature.objects.last()
context = {'selected_feature': selected_feature,
'latest_feature': latest_feature,
'past_features': past_features,
'test': test}
return render(request, 'gp/feature_detail.html', context)
MODELS
class Feature(models.Model):
title = models.CharField(db_index=True, max_length=100, default='')
content = models.TextField(default='')
pub_date = models.DateTimeField(db_index=True, default=datetime.now, blank=True)
def __str__(self):
return self.title
def __iter__(self):
return [
self.id,
self.title ]
You can either store the first three Features in separate variables in your context or add checks to your template loop like {% if forloop.first %} or {% if forloop.counter == 2 %}.
If all you want is to not have the
selected_feature
latest_feature
these two records out of the past_features queryset, then you can use exclude on the past_features query and pass the id's of the selected_features and latest_feature objects.
The views.py would look like:
def feature_detail(request, pk):
selected_feature = get_object_or_404(Feature, pk=pk)
latest_feature = Feature.objects.order_by('-id')[0]
# Collect all the id's present in the latest_feature
excluded_ids = [record.pk for record in latest_feature]
excluded_ids.append(selected_feature.pk)
#This would only return the objects excluding the id present in the list
past_features = Feature.objects.order_by('-pub_date').exclude(id__in=excluded_ids)
test = Feature.objects.last()
context = {'selected_feature': selected_feature,
'latest_feature': latest_feature,
'past_features': past_features,
'test': test}
return render(request, 'gp/feature_detail.html', context)
Django provides a rich ORM and well documented, go through the Queryset options for further information.
For access to a specific object in Django templates see following example:
For access to first object you can use {{ students.0 }}
For access to second object you can use {{ students.1 }}
For access to a specific field for example firstname in object 4 you can use {{ students.3.firstname }}
For access to image field in second object you can use {{ students.1.photo.url }}
For access to id in first object you can use {{ students.0.id }}
I'm trying to use django-jfu to multiupload images, but I have a problem. I want to handle a foreign key dynamically (via url or something), but I can't think of anything.
I have the following models:
class Event(models.Model):
name = models.CharField(max_length=128)
class Picture(models.Model):
event = models.ForeignKey(Event)
image = models.ImageField(upload_to='media')
According to django-jfu, you have to specify a "upload" view to call from the template via template tag. This is my upload view:
#require_POST
def upload(request):
event = Event.objects.get(id=26)
file = upload_receive(request)
instance = Picture(image = file, event = event)
print instance
instance.save()
basename = os.path.basename(instance.image.path)
file_dict = {
'name' : basename,
'size' : file.size,
'url': settings.MEDIA_URL + basename,
'thumbnailUrl': settings.MEDIA_URL + basename,
'deleteUrl': reverse('jfu_delete', kwargs = { 'pk': instance.pk }),
'deleteType': 'POST',
}
return UploadResponse(request, file_dict)
Right now, as a test, it only saves pictures to event with id=26, but how can I handle it dynamically? This is the view and template where I'm calling the template tag:
view
def add_pictures_to_event(request, event_id):
return render(request, 'add_pictures_to_event.html')
template
{% extends 'base.html' %}
{% load staticfiles %}
{% load jfutags %}
{% block body %}
<div class="container">
<h2>Photo upload</h2>
{% jfu %}
</div>
{% endblock %}
As you can see, the view add_pictures_to_event, gets the request and the id of the event, but I cant seem to pass it to the upload view.
Any help would be appreciated.
I had the same question. I looked at different django versions of jQuery File Upload but stuck with Alem's jfu but with the changes from Thomas Willson to make it work in 1.9. My solution might not be the best but I could not find an other way.
I assume you already created an event and then add images to it.
media_upload_form.html is in my projects static directory. I used the UPLOAD_FORM_EXTRA block to add a hidden formfield with the current event_id:
{% block UPLOAD_FORM_EXTRA %}
<input type="hidden" name="currentevent" value="{{instance.pk}}">
{% endblock %}
I assume you have the view from the docs. I changed in the beginning of the uploadview:
file = upload_receive( request )
event_instance = get_object_or_404(Event, id=request.POST['currentevent'])
instance = Picture( file = file, event=event_instance)
instance.save()
It is probably against all django rules but it works. If anyone has a better solution I like to know too. FormSets maybe?
Argh. Hey all, i have a muy simple django question:
And argh, i'm sorry, i've read and read, and I am sure this is covered somewhere super-obviously, but i haven't found it!
How do i edit/update a model using django? Like, the data values? Not the model fields?
Here is my code! (I'm using a home-brew version of stuff!)
Here is the urls:
url(r'^editStory/(?P<id>\d+)$',
StoryModelView.as_view(
context_object_name='form',
template_name ='stories/editStory.html',
success_template= 'stories/editStorySuccess.html'
),
{},
'editStory'
),
Here is the view:
def get(self,request,id=None):
form = self.getForm(request,id)
return self.renderValidations(form)
def getForm(self, request,id):
if id:
return self.getModelById(request,id)
return StoryForm()
def getModelById(self,request,id):
theStory = get_object_or_404(Story, pk=id)
if theStory.user != request.user:
raise HttpResponseForbidden()
return StoryForm(theStory)
def renderValidations(self,form):
if self.context_object_name:
contextName = self.context_object_name
else:
contextName = 'form'
if self.template_name:
return render_to_response(self.template_name,{contextName:form})
else :
return render_to_response('stories/addStory.html',{contextName:form})
def getPostForm(self,request,id):
if id:
theStory = self.idHelper(request,id)
return StoryForm(request.POST,theStory)
return StoryForm(request.POST)
def processForm(self,form,request):
theStory = form.save(commit=False)
theStory.user = request.user
return theStory
Here is the template code:
{% block content %}
<h3>Edit story</h3>
<form action="" method="post">
{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<input type="submit" value="Submit"/>
</form>
{% endblock %}
try as i might, i either:
get an error
get nothing
i get an error with the code as-displayed, caused by this line
{% for field in form %}
and with the error of:
Caught AttributeError while rendering: 'Story' object has no attribute 'get'
or i get nothing - no data at all - if i change my "getModelById" method to read:
def getModelById(self,request,id):
theStory = get_object_or_404(StoryForm, pk=id)
if theStory.user != request.user:
raise HttpResponseForbidden()
return theStory
StoryForm is one of those "ModelForm" things, and its model is "Story".
SO! The question:
how do i fix this code to make it work? What have i done wrong?
You don't show what your class is inheriting from, but it seems like you're just using a standard single object display view. Instead, you should use one of the editing mixins that are provided for this purpose.
Without knowing what your model looks like, are you looking for something along the lines of
s = Story.objects.get(some criteria)
s.user = <some user>
s.save()
?
Sorry, I find your question a little vague.
Upon rereading, one thing jumped out at me:
You can't do a query (get, filter, or any variation on these) on a model-- you have to do it on an object manager, like objects.
So, as above, in your case, Story.objects.get_object_or_404 should solve your error.
I remodel my objects using ManyToMany relationship using "through" as it's guided here:
link text
class Receipt(models.Model):
name = models.CharField(max_length=128)
(...)
components = models.ManyToManyField(Product, through='ReceiptComponent')
class Admin:
pass
def __unicode__(self):
return self.name
def url(self):
return self.id
class ReceiptComponent(models.Model):
product = models.ForeignKey(Product)
receipt = models.ForeignKey(Receipt)
quantity = models.FloatField(max_length=9)
unit = models.ForeignKey(Unit)
class Admin:
pass
def __unicode__(self):
return unicode(self.quantity!=0 and self.quantity or '') + ' ' + unicode(self.unit) + ' ' + self.product.genitive
It looks ok, but I have 2 problems with it:
1) In admin management panel there's no easy connection with receipt = If I have to add a new component - I should go to components and make component connected to receipt - maybe it's the only solution - but it would be more intuitive in receipts
2) I can't print it using templates:
views.py:
(...)
def detail(request, receipt_id):
receipt = get_object_or_404(Receipt, pk=receipt_id)
components = receipt.components.all()
return render_to_response('receipt.html',{'receipt' : receipt, 'components' : components,}
(...)
receipt.html:
<h1>{{ receipt.name }}</h1>
{% for component in components.all %}
<div class='component'>{{ component }}</div>
{% endfor %}
What you did with .all is exactly what I meant -- you had it initially in 2 places, .all() in view and .all in template.
The reason of your 'error' is quite evident - components is the m2m field to Product. This is what your wrote in your code. This components is the set of products, not intermediary model ReceiptComponent.
UPD: simply leave your models as are, and use receiptcomponent_set from the Receipt
1) Have you tried inlines?
2) remove .all in your template, you don't need it.
May be you will also need
components = list(receipt.components.all())
1) looks perfect! (hint for other users: inline != inlines ;D
2) removing .all causes an exception:
Caught an exception while rendering: 'ManyRelatedManager' object is not iterable
but I understand that for good quality code I should move it from template to code:
views.py:
def detail(request, receipt_id):
receipt = get_object_or_404(Receipt, pk=receipt_id)
components = receipt.components.all()
additionals = receipt.additionals.all()
return render_to_response('drinkbook/receipts/receipt.html',{'receipt' : receipt, 'components' : components, 'additionals' : additionals, })
template:
h1>{{ receipt.name }}</h1>
{% for component in components %}
<div class='component'>{{ component }}</div>
{% endfor %}
{% if receipt.additionals %}Ponadto:
{% for additional in additionals %}
<div class='additional'>{{ additional }}</div>
{% endfor %}
{% endif %}
<p>{{ receipt.desc|safe }}</p>
Ok. It works now, but the result of component is Product.unicode not ReceiptComponent.unicode (which is a child of Product). Why?