[Answered ]-Django can't open database in table

2👍

The problem is in these two lines

profile = User.objects.get(pk=request.user.id)

and

{% for item in profile.all %}

The first one will just return to you a single User object. This object won’t have an all attribute, so the second {% for %} won’t be executed! Instead of the {% for %} try {{ profile }}.

Update

First of all, I really don’t know what this line

user = models.ForeignKey(User, unique=True)

does. Please remove it from there ! Now, I suppose that the User is a normal django.contrib.auth.models.User. If yes, then you can do the following in your template:

The user {{ profile.username }} has the following name: {{ profile.first_name }} {{  profile.last_name }} 

etc etc – check the docs to see all the fields of User: https://docs.djangoproject.com/en/dev/ref/contrib/auth/

0👍

to fetch all the User objects
use:

profile = User.objects.all()
args['profile']= profile

and in html

{% if profile %}
    {% for usr in profile %}
       {{ usr }}
    {% endfor %}
{% endif %}
👤navyad

0👍

It seems like you are mistaking the code

{% for item in profile.all %}
{% endfor %}

To provide access to the fields of your model in your template.

Instead, to access user fields (data, attributes, etc.), you need to use the following in your template:

<tr> 
    <td>{{ profile.<user attribute> }}</td>
    <td>{{ profile.username }}</td>
</tr>

See: https://docs.djangoproject.com/en/dev/topics/templates/

Leave a comment