75

ユーザーがアプリに登録すると、プロファイル ページに到達したときにこのエラーが表示されます。

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

このエラーは、テンプレートに画像が必要で、登録したばかりで、編集ページに移動してページを追加しない限り画像を追加できないために発生したと思います。その後、プロファイル ページにアクセスできます。

私のプロフィール.html

<h4>My Profile</h4>

{% if person  %}
<ul>           
    <li>Name: {{ person.name }}</li>
    <br><img src="{{ person.image.url }}">


</ul>
{% endif %}

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

Person オブジェクトの 2 つのインスタンスを作成し、テンプレートで if を使用してそれらを分離することで、このソリューションを試しましたが、成功しませんでした。

<h4>My Profile</h4>

{% if person  %}
<ul>           
    <li>Name: {{ person.name }}</li>
 </ul>
{% endif %}
{% if bob %}
<ul>           
<br><img src="{{ bob.image.url }}">
</ul>

プロファイル機能に対する私の解決策

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

組み込みのテンプレート タグとフィルターのドキュメントを読んでいます。ここでの解決策は (および) テンプレート タグを使用することだと思いますが、適切に使用できないようです。

このテンプレートを構成して画像をオプションにする方法を教えてください。写真がない場合はそのままにして、人の名前を表示します。

助けてくれてありがとう

4

8 に答える 8

103

bobpersonは同じオブジェクトであり、

person = Person.objects.get(user=request.user)
bob = Person.objects.get(user=request.user)

そのため、人だけを使用できます。

テンプレートで、image最初に存在するかどうかを確認し、

{% if person.image %}
    <img src="{{ person.image.url }}">
{% endif %}
于 2013-03-10T12:54:27.510 に答える
87

DRY に違反しないより良いアプローチは、次のようなヘルパー メソッドをモデル クラスに追加することです。

@property
def image_url(self):
    if self.image and hasattr(self.image, 'url'):
        return self.image.url

default_if_none テンプレート フィルターを使用して、デフォルトの URL を提供します。

<img src="{{ object.image_url|default_if_none:'#' }}" />
于 2013-05-20T07:26:59.097 に答える
10

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">
于 2019-11-09T01:54:50.340 に答える