簡體   English   中英

Django外鍵類未顯示

[英]Django Foreign Key class not showing

目的是讓儀表板顯示您所在區域的用戶列表。 用戶列表有效,並且顯示用戶名。 唯一的問題是我無法顯示用戶圖像(理想情況下只有第一張圖像)。 當前沒有錯誤消息。 只是什么也沒有出現。

models.py

class Images(models.Model):
    image = models.ImageField(upload_to='profile_image', null=True, default='profile_image/none/no-img.png')
    user = models.ForeignKey(User, on_delete=models.CASCADE,  null=False)

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True, null=True)
    birth_date = models.DateField(null=True, blank=True)
    ...

views.py

class DashboardView(TemplateView):
    template_name = 've/cp/dashboard.html'

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(DashboardView, self).dispatch(*args, **kwargs)

    def get(self, request, pk=None):
        users = User.objects.exclude(id=request.user.id)

        try:
            favorite = Favorite.objects.get(current_user=request.user)
            favorites = favorite.users.all()
        except Favorite.DoesNotExist:
            favorites = None

        args = {
            # 'users': users, 'favorites':favorites, 'images': images,
        }

        return render(request, self.template_name, args)

dashboard.html

        <div class="col-12">
            <h2>People near you</h2>
            {% for user in users %}
                <a href="{% url 've:view_profile_with_pk' pk=user.pk %}">

                    <!--THIS WORKS-->
                    <h4>{{ user.username }}</h4>
                    <p>{{ user.profile.bio }}</p>

                    <!--But not this... why..-->
                    <p>{{ user.images.image.url }}</p>

                ''' 
                Or this.. However it does work on view_profile page where 
                there is a pk. Seem like it's not finding images for users, 
                as this results in a "No images message on localhost 
                '''
                {% if images %}
                    {% for img in images %}
                    <a href="{{ user.img.image.url }}" target="_blank">
                    <img src="{{ user.img.image.url }}" class="" style="max-width: 300px">
                    </a>
                    {% endfor %}
                {% else %}
                <p>No images</p>
                {% endif %}
                </a>

                <!-- Favorites works great -->
                {% if not user in favorites %}
                <a href="{% url 've:change_favorites' operation='add' pk=user.pk %}">
                <button type="button" class="btn btn-success">Add Favorite</button>
                </a>
                {% endif %}
            {% endfor %}
        </div>

您可以使用images_set屬性從用戶對象直接獲取用戶的圖像。

在您的情況下,您可以執行以下操作:

{% for user in users %}
    {% with first_image=user.images_set.first %} 
        {% if first_image %} 
            <a href="{{ first_image.image.url }}" target="_blank">
            <img src="{{ first_image.image.url }}" class="" style="max-width: 300px">
        {% endif %}
    {% endwith %} 
{% endfor %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM