Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django,在视图中显示多个模型_Python_Django_Django Templates_Django Views - Fatal编程技术网

Python Django,在视图中显示多个模型

Python Django,在视图中显示多个模型,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,因此,我想在其中一个应用程序视图中显示来自两个不同应用程序的两个模型。具体来说,我试图在用户的个人资料页面上显示用户的帖子提要。然而,我一直没能弄明白,我发现什么都不管用。这是我的密码,请看一下 订阅源应用程序型号: from django.db import models from django.core.urlresolvers import reverse from django.conf import settings from django.contrib.auth import g

因此,我想在其中一个应用程序视图中显示来自两个不同应用程序的两个模型。具体来说,我试图在用户的个人资料页面上显示用户的帖子提要。然而,我一直没能弄明白,我发现什么都不管用。这是我的密码,请看一下

订阅源应用程序型号:

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
不确定您是否需要查看此内容,但以下是用户应用程序型号:

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
用户应用程序视图,我希望在其中显示两个模型:

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%}
{{userprofileinfo.username}

分数:

分数在这里

跟随

标题:

{{userprofileinfo.title}

网站:

{{userprofileinfo.website}

我是:

{%if request.user.userprofileinfo.user_type==1%} 设计师

{%elif request.user.userprofileinfo.user_type==2%} 设计师

{%else%}

两者

{%endif%} {%if userprofileinfo.about%}

关于我:

{{userprofileinfo.about}

{%endif%}

成员自:

{{userprofileinfo.join\u date}

{%if userprofileinfo.location%} 位置:

{{userprofileinfo.location}

{%endif%} {%if userprofileinfo.twitter%} 推特:

{{userprofileinfo.twitter}

{%endif%} {%if userprofileinfo.dribbble%}

带球:

{{userprofileinfo.dribbble}

{%endif%} {%if userprofileinfo.github%} Git Hub:

{{userprofileinfo.github}

{%endif%} {%include'feed/userpost\u list\u inner.html%} {%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 %}
{userpost\u list%中的post%
{{userpost.post.title}

{{post.author}

{{post.post_date}

{{post.post_body}

{%endfor%}
在反复使用它之后,我能够使用问题中的代码让它工作。我在这里使用
user feed
作为我的上下文名称:

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

问题是我没有在模板中使用正确的名称:

{%forpost in userpost\u list%}

所以这是不匹配的。我将视图中的上下文名称更改为
user\u post
,然后在模板中也更改了它

{%for post in user_post%}


这解决了问题。所以简而言之,我只是没有正确地连接它们。

在对它进行了更多的尝试后,我能够使用我问题中的代码使它工作。我在这里使用
user feed
作为我的上下文名称:

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

问题是我没有在模板中使用正确的名称:

{%forpost in userpost\u list%}

所以这是不匹配的。我将视图中的上下文名称更改为
user\u post
,然后在模板中也更改了它

{%for post in user_post%}

这解决了问题。简言之,我只是没有正确地连接它们