Я довольно новичок в django, а также python, в моем приложении у меня есть две модели, одна - MyProfile
, а одна - MyPost
, пользователи будут иметь профиль, и пользователи смогут создавать сообщения, это все работает, но я хотел показать сообщения, созданные пользователем в своих профилях. для этого я попытался создать get_context_data
внутри моего generi c Detailview
. Но это дает мне эту ошибку Cannot query "ahmy": Must be "MyProfile" instance
. ahmy - это мое имя пользователя.
Мои модели
from django.db import models
from django.contrib.auth.models import User
from django.db.models.deletion import CASCADE
from django.core.validators import MinValueValidator, RegexValidator
# Create your models here.
class MyProfile(models.Model):
name = models.CharField(max_length = 500)
user = models.OneToOneField(to=User, on_delete=CASCADE)
address = models.TextField(null=True, blank=True)
gender = models.CharField(max_length=20, default="Male", choices=(("Male", 'Male'), ("Female", "Female"), ("LGBTQ", "LGBTQ")))
phone_no = models.CharField(validators=[RegexValidator("^0?[5-9]{1}\d{9}$")], max_length=15, null=True, blank=True)
description = models.CharField(max_length = 240, null=True, blank=True)
pic = models.ImageField(upload_to = "image\\", null=True)
def __str__(self):
return "%s" % self.user
class MyPost(models.Model):
main_pic = models.ImageField(upload_to = "image\\", null=True)
amount_spend = models.IntegerField(null=True, blank=True)
total_donars = models.IntegerField(null=True, blank=True)
title = models.CharField(max_length = 200)
body = models.TextField(null=False, blank=False)
cr_date = models.DateTimeField(auto_now_add=True)
uploaded_by = models.ForeignKey(to=MyProfile, on_delete=CASCADE, null=True, blank=True)
def __str__(self):
return "%s" % self.title
My View @method_decorator (login_required, name = "dispatch")
class MyProfileDetailView(DetailView):
model = MyProfile
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the user posts
user_posts = MyPost.objects.filter(uploaded_by=self.request.user).order_by('-cr_date')
context['user_posts'] = user_posts
context['user'] = self.request.user
return context
Мой Html файл
{% extends 'base.html' %}
{% block content %}
<div class="p-5">
<img src="/media/{{myprofile.pic}}" />
<h1 class="myhead2">{{myprofile.name}}</h1>
<p><strong>Address: {{myprofile.address}}</strong></p>
<p><strong>Phone Number: {{myprofile.phone_no}}</strong></p>
<p><strong>Email: {{myprofile.user.email}}</strong></p>
<p><strong>About:</strong> {{myprofile.purpose}}</p>
<p><strong> Total Donation Recived: {{myprofile.donation_recived}}</strong></p>
<hr>
<table class="table my-3">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
{% for MyPost in user_posts %}
<tr>
<td>{{MyPost.title}}</td>
<td>{{MyPost.cr_date | date:"d/m/y"}}</td>
<td>
<a class="btn btn-dark btn-sm" href='/covid/mypost/{{n1.id}}'>Read More</a>
<a class="btn btn-dark btn-sm" href='/covid/mypost/delete/{{n1.id}}'>Delete</a>
</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
URLS
from django.contrib import admin
from django.urls import path
from django.urls.conf import include
from covid import views
from django.views.generic.base import RedirectView
urlpatterns = [
# Normal pages
path('home/', views.HomeView.as_view()),
path('tips/', views.TipsViews.as_view()),
path('info/', views.InfoView.as_view()),
path('dashboard', views.DashboardView.as_view()),
# Choose State URL
path('chose_state', views.chose_state, name='chose_state'),
# Orginisations Profiles
path('profile/edit/<int:pk>', views.MyProfileUpdateView.as_view(success_url="/covid/home")),
path('myprofile/', views.MyProfileListView.as_view()),
path('myprofile/<int:pk>', views.MyProfileDetailView.as_view()),
# Post URL
path('mypost/create/', views.MyPostCreate.as_view(success_url="/covid/mypost")),
path('mypost/delete/<int:pk>', views.MyPostDeleteView.as_view(success_url="/covid/mypost")),
path('mypost/', views.MyPostListView.as_view()),
path('mypost/<int:pk>', views.MyPostDetailView.as_view()),
path('profile/edit/<int:pk>', views.MyProfileUpdateView.as_view(success_url="/covid/home")),
# Root URL
path('', RedirectView.as_view(url="home/")),
]