所以我想在一个应用程序视图中展示两个不同应用程序的两个模型。具体来说,我试图在用户的个人资料页面上显示用户的帖子。但是我一直没能搞清楚,我什么也没找到。这是我的密码,请看一下。在
订阅源应用程序模型:
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
不确定是否需要查看,但以下是用户应用程序模型:
^{pr2}$用户应用程序视图,我希望在其中显示两个模型:
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 %}
上面的Inclusion语句指向以下文件:
{% 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 %}
在对它进行了更多的研究之后,我能够使用我问题中的代码使它正常工作。我在这里使用
user-feed
作为上下文名称:context['user-feed'] = UserPost.objects.all()
但问题是我在模板中没有正确的称呼:
{% for post in userpost_list %}
所以这不匹配。我将视图中的上下文名称更改为
user_post
,然后在模板中也更改了它{% for post in user_post %}
这就解决了这个问题。所以简而言之,我只是没有把它们连接好。在
相关问题 更多 >
编程相关推荐