简体   繁体   中英

{{ user.get_profile.foo }} not working in Django

I am trying to display user profile information in the users profile (original, I know,) but I can't get it to work unless it's the form view of the profile. I call user and foo in my user profile edit form with {{form.instance.user}} and it works fine.

I've implemented {{ user.get_profile.foo }} in my static user profile template ( loggedin.html ) but nothing happens.

My static user profile ( loggedin.html ) template looks like this:

{% extends "base.html" %}

{% block content %}

    <title>{% block title %} | {{ username }}{% endblock %}</title>

    <h2>Hi, {{ username }}!</h2>
    <h3>{{ user.get_profile.foo }}</h3>

    <a href="/accounts/profile/">Edit Profile</a>

{% endblock content %}

The view for loggedin.html :

def loggedin(request):
    return render_to_response('loggedin.html', {'username':request.user.username, 'user':request.user})

My user profile edit form( profile.html ) template:

{% extends "base.html" %}

{% block content %}

    <title>{% block title %} | Edit Profile{% endblock %}</title>

    <h2>Edit {{ username }}</h2>

    {% for field in form %}
        {{ field.error }}
    {% endfor %}

   <form action="/accounts/profile/" method="post">{% csrf_token %}
       <label for="id_user">Username:</label>
       <br>
       <input id="id_user" name="user" type="text" value="{{form.instance.user}}">
       <br><br>
       <label for="id_foo">Foo:</label>
       <br>
       <input id="id_foo" name="foo" type="text" value="{{form.instance.foo}}">
       <br><br>
       <input type="submit" value="Update">

   </form>

{% endblock content %}

My user profile (form) model:

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    foo = models.CharField(max_length=30)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

My user profile (form) view:

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from forms import UserProfileForm
from django.contrib.auth.decorators import login_required
from userprofile.models import UserProfile

@login_required
def user_profile(request):
    user = request.user
    username = request.user.username

    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=request.user.profile)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/loggedin')
    else:
        profile = user.profile
        form = UserProfileForm(instance=profile)

    args = {}
    args.update(csrf(request))
    args['form'] = form
    args['user'] = user
    args['username'] = username

    return render_to_response('profile.html', args)

I can get the username and foo value to display fine in the user profile form, but when I try to get the same information to display in the static profile, it doesn't work.

Full disclosure: my static profile is in an app called polls which is different than the userprofile app where my edit form model and template are located, but I'm not sure if that has anything to do with this issue.

If you need to see my urls, settings, or admin code, let me know; I figured it wasn't relevant, and would just add clutter, but I'm happy to add it if needed.

Your property is User.profile . Therefore you should try

{{ user.profile.foo }}

in your template, instead of {{ user.get_profile.foo }} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM