1

I want to check that user_id exists in the profile_images table from my Django template.

My Model

class profiles(models.Model):
    profile_id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User)
    -----

class Profile_images(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to='uploads/',default = 'uploads/no-img.jpg')

My View

def view_profiles(request):
    if request.user.is_authenticated():
        view_all_profiles = profiles.objects.all()
        profile_image = Profile_images.objects.all()
        return render_to_response('profiles/all.html', {'profiles':view_all_profiles,'profile_image':profile_image}, context_instance=RequestContext(request),)
    else:
        return HttpResponseRedirect('/accounts/login/')

My Template

{% for profile in profiles %}
    <li>
        {% for image in profile_image %}
            {% ifequal image.user_id profile.user_id %}                         
                <img src="{{MEDIA_URL}}{{image.image}}" alt="image" />
            {% endifequal %}
            <!-- i want to check here if not user_id exist in profile_images table  -->
            {% if profile.user_id not in profile_image %}
                <img src="{% static 'images/no-image.jpg' %}" alt="image" />
            {% endif %}
        {% endfor %}
    </li>
{% endfor %}

{% if profile.user_id not in profile_image %} is not working. I'm new to Django & python and I'm stuck here. Please suggest better ways if my code is not correct.

2
  • Why have you externalize the profile_image in a whole Model? Why don't you save this data in profiles? Commented Jan 3, 2014 at 17:04
  • @MaximeLorant . first I'm new to Django . Second I'm saving that details not at same time & some fields value null false. So I'm worried if profile image cannot save without other values. Commented Jan 3, 2014 at 17:27

2 Answers 2

1

in your view you could get all user_ids with a profile image, something like:

user_ids_with_profile_images = Profile_images.objects.all().values_list('user_id', flat=True)

Then in your template you could check if profile.user_id not in user_ids_with_profile_images.


It might actually be a little cleaner to loop through all the users with profiles in your system and get their profile images through the foreign key, instead of looping through all the profiles and trying to get the users...

Sign up to request clarification or add additional context in comments.

Comments

1

This is really a design problem, you've got a separate model specifically for a profile image when that could just be a field on the profile model itself.

class Profile(models.Model): # convention is to use a non-plural name for models
    # No need to explicitly set the primary key, this will be added automatically
    # profile_id = models.AutoField(primary_key=True) 
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to='uploads/',default = 'uploads/no-img.jpg')
    -----

Now it would just be a case of using {{ profile.image }} with no need for any additional looking up.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.