1๐
โ
I would advise to rename your model to GroupDetail
, since not it will generate a lot of confusion since there are two models used: the Group
from the django.contrib.auth.models
, and the one you defined yourself.
Likely it is better to implement this as:
class GroupDetail(models.Model):
group = models.OneToOneField(Group, on_delete=models.CASCADE, related_name='groups')
# โฆ
Then in the view, we can fetch the GroupDetail
s for a primary key with:
group = get_object_or_404(GroupDetail, pk=pk)
or if we want to work with the primary key of the referenced Group
model, then it is:
group = get_object_or_404(GroupDetail, group__pk=pk)
This thus means that the view should look like:
def group_main(request, pk):
context= {
'groupdetail': get_object_or_404(GroupDetail, group__pk=pk)
}
return render(request,'group/grouptimeline.html',context)
Furthermore, you are rendering the GroupDetail
object with variables that do not exist on the Group
object. You should render these with:
<img src="{{ groupdetail.cover.url }}" alt="">
<!-- โฆ -->
<h1> {{ groupdetail.group_name }} </h1>
Source:stackexchange.com