Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

Django The 'image' attribute has no file associated with it

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:omysitepetemplatesprofile.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:omysitepetviews.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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...