首先,我將搜索用戶,然后將我帶到用戶串列的頁面以單擊。之后,當我選擇一個我想點擊查看他們的個人資料的用戶時,它應該帶我到我想查看的用戶頁面,但這就是我遇到問題的地方。URL 模式應該是,例如/user/MatthewRond/,但相反,我得到/user/MatthewRond Profile/,這會阻止我看到 Matthew 的頁面,因為 Profile 從未打算在 URL 中。如果我能弄清楚如何擺脫 Profile,我的問題應該會得到解決。
我閱讀了 是什么以及從中得到了什么:它與使用和處理字母數字字符有關。無論如何,它都沒有幫助我弄清楚如何洗掉它。至于它的代碼部分,我從資料庫中獲取默認用戶名,然后從該默認用戶那里獲取自定義組態檔。之后,我將用戶名設定為我制作的自定義組態檔模型。
視圖.py
def profile_view(request, username, *args, **kwargs,):
context = {}
try:
user = User.objects.get(username=username)
profile = user.profile
img = profile.uploads_set.all().order_by("-id")
context['username'] = user.username
except:
return HttpResponse("Something went wrong.")
if profile and img:
context = {"profile": profile, "img": img}
return render(request, "main/profile_visit.html", context)
搜索結果.py
<a class="profile-link" href="{% url 'profile_view' username=profile.0 %}">
網址.py
path("user/<str:username>/", views.profile_view, name = "profile_view"),
模型.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, null = False, blank = True)
first_name = models.CharField(max_length = 50, null = True, blank = True)
last_name = models.CharField(max_length = 50, null = True, blank = True)
phone = models.CharField(max_length = 50, null = True, blank = True)
email = models.EmailField(max_length = 50, null = True, blank = True)
bio = models.TextField(max_length = 300, null = True, blank = True)
profile_picture = models.ImageField(default = 'default.png', upload_to = "img/%y", null = True, blank = True)
banner_picture = models.ImageField(default = 'bg_image.png', upload_to = "img/%y", null = True, blank = True)
def __str__(self):
return f'{self.user.username} Profile'
uj5u.com熱心網友回復:
在 Rapheal 的幫助下,錯誤將出現在 Profile 類中。改變這個
def __str__(self):
return f'{self.user.username} Profile'
對此。
def __str__(self):
return f'{self.user.username}'
洗掉空間和組態檔
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436212.html
