سؤال

I have been struggling to see what is wrong with this code for couple hours now. The project i m working on calls for users to register to the sites (taken care of by Django registration plugin.) Once registered, users will be able to add their company (name, address, phone etc...) to the site as a listing. So company has its own model. I am using Django profiles to show user info and company info on Django profiles page. Profiles also built on top of Django Profiles plugin.

url(r'^accounts/', include('registration.urls')),    
url(r'^admin_export/', include("admin_export.urls")),   
url(r'^profiles/edit', 'profiles.views.edit_profile'),
url(r'^profiles/create', 'profiles.views.create_profile'),
url(r'^profiles/', include('profiles.urls')),
url(r'^profiles/(?P<username>\w+)/$', 'profiles.views.profile_detail',name='UserProfileView'),
url(r'^comments/', include('django.contrib.comments.urls'))


#models.py

class UserProfile(models.Model):
    user = models.ForeignKey(User,unique=True)
    #email = models.CharField(max_length=200, blank=True, null=True)
    # Other fields here
    #company = models.ForeignKey(Company,blank=True,null=True)    
    #office = models.CharField(max_length=200, blank=True, null=True)    
    def __unicode__(self):
        return self.user.username




class Company(models.Model):
    userprofile = models.ForeignKey(UserProfile, null=True, blank=True)
    comp_name = models.CharField(max_length=200,blank=True,null=True)
    comp_address = models.CharField(max_length=200,blank=True, null=True)
    comp_email = models.CharField(max_length=200,blank=True, null=True)
    comp_zip = models.IntegerField(blank=True, null=True)
    comp_phone = models.IntegerField(blank=True, null=True)
    comp_city = models.CharField(max_length=200,blank=True, null=True)
    #comp_state = models.USStateField(blank=True, null=True
    comp_state = models.CharField(blank=True, max_length=2)
    compwebsite = models.URLField(max_length=200, blank=True, null=True)
    twitterurl = models.URLField(max_length=200, blank=True, null=True)
    facebookurl = models.URLField(max_length=200, blank=True, null=True)
    def __unicode__(self):
        return self.comp_name

class ProfileForm(ModelForm):
    class Meta:
        model=UserProfile
        exclude=('user',)

#views.py
def UserProfileView(request, username):
    context_dict = {}
    usercompany =  get_object_or_404(Company, user=userprofile)
    context_dict = {'usercompany': usercompany}
    return render_to_response('profile_detail.html', context_dict, RequestContext(request))
هل كانت مفيدة؟

المحلول

userprofile is actually not available when it's referenced in your views.py, so it should raise NameError

If I understood you right, the way to achieve this is:

#views.py
def UserProfileView(request):
    context_dict = {}
    usercompany =  get_object_or_404(Company, userprofile__user=request.user)
    context_dict = {'usercompany': usercompany}
    return render_to_response('profile_detail.html', context_dict, RequestContext(request))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top