2017-09-08 2 views
1

だから、私は2つの異なるモデルから2つのモデルを表示したいと思っています。具体的には、プロファイルページにユーザーの投稿フィードを表示しようとしています。しかし、私はそれを把握することができず、私が見つけたものは何も見つからなかった。ここで私のコードは、見てください。Django、複数のモデルを表示しています

フィードアプリモデル:

from django.db import models 
from django.core.urlresolvers import reverse 
from django.conf import settings 

from django.contrib.auth import get_user_model 
User = get_user_model() 

# Create your models here. 
class UserPost(models.Model): 
    author = models.ForeignKey(User,related_name='userpost',null=True) 
    post_date = models.DateTimeField(auto_now_add=True) 
    title = models.CharField(max_length=150,blank=False) 
    post_body = models.TextField(max_length=1000,blank=False) 

    def publish(self): 
     self.save() 

    def get_absolute_url(self): 
     return reverse('index') 

    def __str__(self): 
     return self.title 

あなたがこれを確認する必要があるだろうかどうかわからしかし、ここではありませんユーザーアプリモデル:

from django.db import models 
from django.utils import timezone 
from django.contrib.auth.models import User 
from users.choices import * 

# Create your models here. 
class UserProfileInfo(models.Model): 
    user = models.OneToOneField(User) 

    join_date = models.DateTimeField(default=timezone.now) 
    profile_pic = models.ImageField(upload_to='profile_pics',blank=True) 
    location = models.CharField(max_length=150) 
    title = models.CharField(max_length=250) 
    user_type = models.IntegerField(choices=USER_TYPE_CHOICES,default=1) 
    website = models.URLField(max_length=100,blank=True) 
    about = models.TextField(max_length=500,default='about') 
    twitter = models.CharField(max_length=50,blank=True) 
    dribbble = models.CharField(max_length=50,blank=True) 
    github = models.CharField(max_length=50,blank=True) 

    def __str__(self): 
     return self.user.username 

ユーザーのアプリの表示私は2つのモデルにしたいです表示:

from django.shortcuts import render,get_object_or_404 
from users.forms import UserForm,UserProfileForm 
from users.models import UserProfileInfo 
from feed.models import UserPost 

from django.contrib.auth import authenticate,login,logout 
from django.http import HttpResponseRedirect, HttpResponse 
from django.core.urlresolvers import reverse 
from django.contrib.auth.decorators import login_required 

from django.contrib.auth.mixins import LoginRequiredMixin 
from django.views.generic import (TemplateView,ListView, 
            DetailView,CreateView, 
            UpdateView,DeleteView) 

# Create your views here. 
class UserProfileView(DetailView): 
    model = UserProfileInfo 
    template = 'users/userprofile.html' 

    def get_context_data(self, **kwargs): 
     context = super(UserProfileView, self).get_context_data(**kwargs) 
     context['user-feed'] = UserPost.objects.all() 
     return context 

ユーザーのプロファイルテンプレート(私はこれを使用するテンプレート):

{% extends "base.html" %} 

{% block content %} 

    <div class="sidebar-userinfo"> 
     <img class="profile-pic" src="{{ userprofileinfo.profile_pic.url }}"> 
     <h2>{{ userprofileinfo.username }}</h2> 
     <p class="accent">Score:</p> 
     <p>Score goes here</p> 
     <form> 
      <button class="btn" type="submit">Follow</button> 
     </form> 

     <p class="accent">Title:</p> 
     <p class="profile-info">{{ userprofileinfo.title }}</p> 

     <p class="accent">Website:</p> 
     <p class="profile-info">{{ userprofileinfo.website }}</p> 

     <p class="accent">I'm a:</p> 
     {% if request.user.userprofileinfo.user_type == 1 %} 
      <p class="profile-info">Designer</p> 
     {% elif request.user.userprofileinfo.user_type == 2 %} 
      <p class="profile-info">Designer</p> 
     {% else %} 
      <p class="profile-info">Both</p> 
     {% endif %} 

     {% if userprofileinfo.about %} 
      <p class="accent">About Me:</p> 
      <p class="profile-info">{{ userprofileinfo.about }}</p> 
     {% endif %} 

     <p class="accent">Member Since:</p> 
     <p class="profile-info">{{ userprofileinfo.join_date }}</p> 

     {% if userprofileinfo.location %} 
      <p class="accent">Location:</p> 
      <p class="profile-info">{{ userprofileinfo.location }}</p> 
     {% endif %} 

     {% if userprofileinfo.twitter %} 
      <p class="accent">Twitter:</p> 
      <p class="profile-info">{{ userprofileinfo.twitter }}</p> 
     {% endif %} 

     {% if userprofileinfo.dribbble %} 
      <p class="accent">Dribbble:</p> 
      <p class="profile-info">{{ userprofileinfo.dribbble }}</p> 
     {% endif %} 

     {% if userprofileinfo.github %} 
      <p class="accent">Git Hub:</p> 
      <p class="profile-info">{{ userprofileinfo.github }}</p> 
     {% endif %} 
    </div> 

    <div class="content-right"> 
     {% include 'feed/userpost_list_inner.html' %} 
    </div> 

{% endblock %} 

上記封入文は、このファイルにつながる:もう少し、私はそれは私の質問のコードを使用して仕事を得ることができた、それで遊んで後

{% for post in userpost_list %} 
    <div class="post"> 
     <h2 class="post-title">{{ userpost.post.title }}</h2> 
     <p class="accent">{{ post.author }}</p> 
     <p class="accent">{{ post.post_date }}</p> 
     <p class="body">{{ post.post_body }}</p> 
    </div> 
{% endfor %} 

答えて

0

。私はここに私のコンテキスト名としてuser-feedを使用していました:

context['user-feed'] = UserPost.objects.all()

そして問題は、私は私のテンプレートに右の名前を呼んなかったことでした。

{% for post in userpost_list %}

だからそれがありませんでしたマッチングアップ。私は、この問題を修正

{% for post in user_post %}

user_postにビューのコンテキスト名を変更して、テンプレートでそれを変更するだけでなく

。要するに、私はちょうどそれらを適切に接続していませんでした。

関連する問題