2017-04-19 37 views
0

嘿,我期待着做這樣的問題,但我沒有看到我的代碼有什麼問題。 Something like this question。我只是想爲我的網站創建一個個人資料頁面,並且我想在個人資料頁面上添加所有用戶帖子,但我似乎無法使其正常工作。我試着用google搜索解決方案,甚至嘗試了其他的stackoverflow問題,但不能如果正確的答案。如果有人可以幫助我,我將不勝感激。謝謝。使用戶在配置文件頁面上發佈的帖子

我也知道它與我鏈接的一個問題相同,但下面是我試着解決方案中的人做了什麼,我似乎並沒有使它工作,並已經過去幾個小時傷心地說,但我能說什麼還在學習:對

models.py

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


def upload_location(posts, filename): 
    return ("%s/%s" %(posts.author, filename)) 

class Post(models.Model): 
    title = models.CharField(max_length=200) 
    image = models.ImageField(upload_to=upload_location, null=True, blank=True, height_field="height_field", width_field="width_field") 
    height_field = models.IntegerField(default=0) 
    width_field = models.IntegerField(default=0) 
    description = models.TextField() 
    pub_date = models.DateTimeField(auto_now=False) 
    author = models.ForeignKey(User) 
    votes_total = models.IntegerField(default=0) 

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

def pub_date_pretty(self): 
    return self.pub_date.strftime('%b %d %Y') 

views.py,我輸入我的Post類從不同的應用程序,所以我也認爲這可能是問題。

from posts.models import Post 

def view_profile(request): 
    logged_in_user = request.user 
    logged_in_user_posts = Post.objects.filter(user=author) 

    return render(request, 'accounts/profile.html', {'posts': logged_in_user_posts}) 

HTML文件

<div> 
{% for post in posts %} 

    Post: {{ post.author }} 

    <br> 

{% endfor %} 
</div> 

urls.py

from django.conf.urls import url 
from . import views 

app_name = 'accounts' 

urlpatterns = [ 
    url(r'^register/', views.signup, name="signup"), 
    url(r'^login/', views.loginuser, name="login"), 
    url(r'^logout/', views.logoutview, name="logout"), 
    url(r'^profile/', views.view_profile, name="viewprofile"), 
] 

添加後閱讀下面貼

看下面的帖子下面我得到這個錯誤後,第一個答案:

FileNotFoundError在/帳號/資料/ [錯誤2]沒有這樣的文件或目錄:「C:\用戶\穆罕默德\桌面\ thegallerybox \ media_cdn \圖像

我是因爲我的html文件,我收到此錯誤沒有從媒體文件夾中的用戶文件夾調用用戶圖像。我根據我在models.py顯示的作者上傳圖片,所以我想可能與在HTML文件中

def upload_location(posts, filename): return ("%s/%s" %(posts.author, filename))

回答

1

要篩選基礎上author獲取圖像做,因爲那是什麼在您的Post模型中定義,並且您要檢查當前登錄的用戶。

嘗試進行以下更改

logged_in_user_posts = Post.objects.filter(author=logged_in_user) 
+0

當我這樣做,我得到這樣的:在/帳號/資料/ [錯誤2]沒有這樣的文件或目錄FileNotFoundError:「C:\\ \\用戶穆罕默德\\ Desktop \\ thegallerybox \\ media_cdn \\ image – Mohamed

+0

您能向我們展示您的網址嗎? – name3anad

+0

按照你的要求添加了url.py文件夾 – Mohamed

相關問題