Image is not uploading to the specific destination - python

I tried several ways to upload images to a specific destination. Showing no error but the image is not uploading to the destination.
views.py
from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['name', 'weblink', 'image']
success_url = reverse_lazy('posts')
template_name = 'base/post.html'
def form_valid(self, form):
# form.instance.post_id = self.kwargs['pk']
form.instance.author = self.request.user
return super().form_valid(form)
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
name = models.CharField(max_length=200)
weblink = models.CharField(max_length=200)
image = models.ImageField(default='default.png', upload_to='uploads/%Y/%m/%d/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return f'Added {self.name} Profile'
urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
.....
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
template
{% extends 'base/base.html' %}
{% block content %}
<div class="row justify-content-md-center home">
<div class="col-md-6 create-form">
← Back
</div>
</div>
<div class="row justify-content-md-center">
<div class="col-md-6 submit-form">
<form action="" method="POST">
{% csrf_token %}
{{form.as_p}}
<button class="btn btn-info" type="submit">Submit</button>
</form>
</div>
</div>
{% endblock content %}
I added also the media root and URL in the settings file. I didn't any clue but unable to upload the file to the specific location.

When your form is supposed to submit files you must set the enctype attribute on the form tag to multipart/form-data otherwise files won't be submitted:
{% extends 'base/base.html' %}
{% block content %}
<div class="row justify-content-md-center home">
<div class="col-md-6 create-form">
← Back
</div>
</div>
<div class="row justify-content-md-center">
<div class="col-md-6 submit-form">
<!-- Set it here ↓ -->
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<button class="btn btn-info" type="submit">Submit</button>
</form>
</div>
</div>
{% endblock content %}

Related

How to write an image attribute file in django (The 'image' attribute has no file associated with it)

I am building a django blog website and I am trying to upload an image on the website. I can access the photos on the admin page but whenever I try to access it on the page I get an enroll. The trace back says
"
The 'image' attribute has no file associated with it.
This my code for the model
class article(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField()
image = models.ImageField(null=True, blank=True, upload_to='static/media/article')
date = models.DateTimeField(default=timezone.now)
def __str__ (self):
return self.title
def get_absolute_url(self):
return reverse('article-detail', kwargs= {'pk':self.pk})
This is my views code
class ArticleCreateView(LoginRequiredMixin, CreateView):
model = article
fields= ['title', 'content', 'image']
def ArticleCreateView(request):
if request.method == "POST":
form = ArticleForm(request.POST, request.FILES)
if form.is_valid():
article = form.save(commit=False)
article.author = request.user.id
article.save()
return HttpResponse
this is the blog template code
{% extends "agri_tourism/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<section class="flex-container">
<div>
{% for article in articles %}
<article class="media content-section">
{% load static %}
<div class="media-body">
<div style="text-decoration:none; display: inline-block;" class="article-metadata">
<img class="rounded-circle article-img" src="{{user.profile.image.url}}">
<a style="text-decoration:none;" class="mr-2" href="#">{{ article.author }}</a>
<small class="article-date">{{ article.date|date:"F d, Y" }}</small>
</div>
<h2><a class="article-title" href="">{{ article.title }}</a></h2>
<p class="article-content">{{ article.content }}</p>
<img class="image-content" id="image-el" src = "{{article.image.url}}">
<div class="like-share-btns">
</div>
</article>
{% endfor %}
The form page code is below
{% extends "agri_tourism/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class = "content-section">
<div class = "container">
<div class = "row md-8">
<form method = "POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset class = "form-group">
<legend class ="border-bottom mb-4">Blog article</legend>
{{form|crispy }}
{{form.media}}
</fieldset>
<div class= "form-group">
<button class="btn btn-outline-info" type="submit" style= "margin-top:4px; ">Submit the Article</button>
</div>
</form>
</div>
</div>
</div>
I want to access photos on the blog page
check the image with if condition like this...
<div class="media-body">
<div style="text-decoration:none; display: inline-block;" class="article-metadata">
{% if user.profile.image.url %}
<img class="rounded-circle article-img" src="{{user.profile.image.url}}">
{% endif %}
<a style="text-decoration:none;" class="mr-2" href="#">{{ article.author }}</a>
<small class="article-date">{{ article.date|date:"F d, Y" }}</small>
</div>
and also check those settings
urls.py (in project directory)
from django.conf import settings # --------> this
from django.conf.urls.static import static # --------> this
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # --------> this
settings.py (in project directory)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Using urls path with slug returns Page not found (404) No profile found matching the query

I'm trying to create user Profile for my django project, I'm using UpdateView to allow user to edit Profile model when they want to create profile for their account but it return an error every time I click on create profile url in the profile template.
the error message:
Page not found (404)
No profile found matching the query
Request Method: GET
Request URL: http://127.0.0.1:8000//6/edit
Raised by: answer.views.EditProfileView
Profile Template:
<div class="container">
<div class="row justify-content-center">
{% for profile in profiles %}
<div class="col">
{{profile.website}}
{{profile.website}}
</div>
{% endfor %}
</div>
</div>
<br>
<div class="container">
<div class="row">
Create Profile
</div>
</div>
My model:
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to="avatars/")
stories = models.TextField(max_length=500,blank=True, null=True)
website = models.URLField(max_length=250, blank=True, null=True)
twitter = models.URLField(max_length=250, blank=True, null=True)
location = models.CharField(max_length=50, blank=True, null=True)
slug = models.SlugField(blank=True, null=True)
my urls:
path('editprofile/<slug:slug>/edit', views.EditProfileView.as_view(), name='editProfile'),
my views:
#login_required(login_url='login')
def profile(request, pk):
profiles = Profile.objects.filter(user=request.user)
questions = Question.objects.filter(user=request.user)
context = {'questions':questions, 'profiles':profiles}
return render(request, 'profile.html', context)
class EditProfileView(UpdateView):
model = Profile
fields = ['profile_image', 'stories', 'website', 'twitter', 'location']
template_name = 'edit_profile.html'
success_url = reverse_lazy('index')
def save(self, *args, **kwargs):
self.slug = slugify(self.user)
super(Creator, self).save(*args, **kwargs)
my index template:
<div class="container">
<div class="row justify-content-center">
<div class="row justify-content-center">
<div class="col-md-6">
Ask Question
<a href="{% url 'notification' %}" class="btn btn-primary">Notifications
{% if unread_notifications %}
<span class="badge bg-secondary">{{unread_notifications}}</span>
{% endif %}
</a>
FeedBack
Profile
Log Out
</div>
</div>
</div>
</div>
You have made user a OneToOneField in your Profile model, that means you should not use filter() in profile view, you should use get_object_or_404 for getting single user's profile, as it has OneToOneRelation.
Try this:
from django.shortcuts import get_object_or_404
#login_required(login_url='login')
def profile(request, pk):
profile = get_object_or_404(Profile,user=request.user)
questions = Question.objects.filter(user=request.user)
context = {'questions':questions, 'profile':profile}
return render(request, 'profile.html', context)
class EditProfileView(UpdateView):
model = Profile
fields = ['profile_image', 'stories', 'website', 'twitter', 'location']
template_name = 'edit_profile.html'
success_url = reverse_lazy('index')
def index(request):
return render(request, 'index.html')
profile.html:
<div class="container">
<div class="row justify-content-center">
{% comment %} {% for profile in profiles %} {% endcomment %}
<div class="col">
{{profile.website}}
{{profile.website}}
</div>
{% comment %} {% endfor %} {% endcomment %}
</div>
</div>
<br>
<div class="container">
<div class="row">
Create Profile
</div>
</div>
Note: I have passed profile.slug since the EditProfileView also requires slug to come in route.
Note: You should not run loop while displaying data with single object.
index.html (success template):
<body>
<h3>Profile updated successfully.</h3>
</body>
edit_profile.html
<body>
<h2>You can edit your profile </h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
</body>
urls.py
urlpatterns = [
path('profile-updated/', views.index, name='index'),
path('profile/<int:pk>/', views.profile, name='profile'),
path('editprofile/<slug:slug>/edit/',
views.EditProfileView.as_view(), name='editProfile')
]
That will successfully update your profile.
Firstly fix
def save(self, *args, **kwargs):
self.slug = slugify(self.user.field) #field is what you want to slugfiy
super(Creator, self).save(*args, **kwargs)
secondly
You are sending ID but url requires slug
#old
Create Profile
#should be
Create Profile

How can I restrict users to delete other's posts in django using class based views?

my views.py file:
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import (
LoginRequiredMixin,
UserPassesTestMixin,
)
from .models import Post
# Create your views here.
class PostListView(ListView):
model = Post
template_name = "blog/index.html"
context_object_name = "posts"
ordering = ["-date_posted"]
class PostDetailView(DetailView):
model = Post
class PostCreateView(CreateView, LoginRequiredMixin, UserPassesTestMixin):
model = Post
fields = ['title', 'genere', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class PostUpdateView(UpdateView, LoginRequiredMixin, UserPassesTestMixin):
model = Post
success_url = "blog-home"
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
class PostDeleteView(DeleteView, LoginRequiredMixin, UserPassesTestMixin):
model = Post
success_url = "/"
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def about(request):
return render(request, 'blog/about.html')
My models.py:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
genere = models.CharField(max_length=50, default='')
def __str__(self):
return f'{self.title} by {self.author}'
def get_absolute_url(self):
return reverse('blog-home')
my urls.py url:
from django.urls import path
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeleteView
from . import views
urlpatterns = [
path("", PostListView.as_view(), name="blog-home"),
path("about", views.about, name="blog-about"),
path("post/<int:pk>", PostDetailView.as_view(), name="blog-detail"),
path("post/new", PostCreateView.as_view(), name="blog-create"),
path("post/<int:pk>/update", PostUpdateView.as_view(), name="blog-update"),
path("post/<int:pk>/delete", PostDeleteView.as_view(), name="blog-delete"),
]
index.html
{% extends "blog/base.html" %}
{% load static %}
{% block content %}
<div class="row tm-row">
{% for post in posts %}
<article class="col-12 col-md-6 tm-post">
<hr class="tm-hr-primary">
<a href="{% url 'blog-detail' post.id %}" class="effect-lily tm-post-link tm-pt-60">
<div class="tm-post-link-inner">
<img src="{% static 'img/img-01.jpg' %}" alt="Image" class="img-fluid">
</div>
<span class="position-absolute tm-new-badge">New</span>
<h2 class="tm-pt-30 tm-color-primary tm-post-title">{{ post.title }}</h2>
</a>
<p class="tm-pt-30">
{{ post.content|safe|truncatewords:"30"|linebreaks }}
</p>
<div class="d-flex justify-content-between tm-pt-45">
<span class="tm-color-primary">{{ post.genere }}</span>
<span class="tm-color-primary">{{ post.date_posted|date:'N j,Y' }}</span>
</div>
<hr>
<div class="d-flex justify-content-between">
<span>36 comments</span>
<span>by {{ post.author }}</span>
</div>
</article>
{% endfor %}
</div>
{% endblock %}
post_detail.html:
{% extends 'blog/base.html' %}
{% load crispy_forms_tags %}
{% load static %}
{% block content %}
<div class="container">
<article class="col-12 col-md-6 tm-post">
<hr class="tm-hr-primary">
<a href="" class="effect-lily tm-post-link tm-pt-60">
<div class="tm-post-link-inner">
<img src="{% static 'img/img-01.jpg' %}" alt="Image" class="img-fluid">
</div>
<span class="position-absolute tm-new-badge">New</span>
<h2 class="tm-pt-30 tm-color-primary tm-post-title">{{ object.title }}</h2>
{% if object.author == user %}
<a class="btn btn-outline-danger" href="{% url 'blog-delete' object.id %}">Delete</a>
<a class="btn btn-outline-secondary" href="{% url 'blog-update' object.id %}">Update</a>
{% endif %}
</a>
<p class="tm-pt-30">
{{ object.content }}
</p>
<div class="d-flex justify-content-between tm-pt-45">
<span class="tm-color-primary">{{ object.genere }}</span>
<span class="tm-color-primary">{{ object.date_posted|date:'N j,Y' }}</span>
</div>
<hr>
<div class="d-flex justify-content-between">
<span>36 comments</span>
<span>by {{ object.author }}</span>
</div>
</article>
</div>
{% endblock %}
post_confirm_delete.html:
{% extends 'blog/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<form method="POST">
{% csrf_token %}
<h2>Are You Sure You Want To Delete "{{ object.title }}"</h2>
<button class="btn btn-outline-danger">Yes, I'm Sure</button>
<a class="btn btn-outline-secondary" href="{% url 'blog-detail' object.id %}">Cancel</a>
</form>
</div>
{% endblock %}
So, what I'm getting is that suppose 2 person jeff and ram are users so ram cannot update the posts of jeff and vice versa.
And if jeff views the post of ram, so he does not get the update and delete, so he cannot edit the post of ram but if jeff goes to "127.0.0.1:8000/post/9/delete" from "127.0.0.1:800/post/9",
So he get the confirm delete page and he can even delete his post.
How can I fix this bug in my project??????
you can use get_queryset() to restrict query in database
class PostUpdateView(UpdateView, LoginRequiredMixin, UserPassesTestMixin):
model = Post
success_url = "blog-home"
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def get_queryset(self):
pk = self.kwargs.get(self.pk_url_kwarg)
return self.model.objects.filter(pk=pk,user=self.request.user)

cannot connect list page to detail page

I have been working on to make the website which people can post their review on restaurants. I finished creating urls.py, views.py, models.py and html file.
I tried to connect the list page with the detailed page on each restaurants. Therefore, I used str:pk tag to connect list page with detail page.
However it does't work and however times I check, I can't find why the error happens.
settings and other settings are already done. I only have to adjust app files.
My Goal:
List of restaurants are already created. I want user to be able to go to the detail page by clicking the button below the "{{ list.outline}}"
models.py
from django.db import models
from django.utils import timezone
stars = [
(1,"☆"),
(2,"☆☆"),
(3,"☆☆☆"),
(4,"☆☆☆☆"),
(5,"☆☆☆☆☆")
]
# Create your models here.
class Tabelog(models.Model):
store_name = models.CharField("店名",max_length = 124,primary_key=True)
title = models.CharField("タイトル",max_length = 124,null=True,blank=True)
evaluation = models.IntegerField("評価",choices = stars)
comment = models.TextField("口コミ")
create_date = models.DateField("口コミ投稿日",default=timezone.now)
price = models.PositiveIntegerField("値段",help_text='円',default=0)
def outline(self):
return self.comment[:10]
def __str__(self):
return ("{},{},{}".format(self.store_name,self.evaluation,self.comment[:10]))
urls.py
from django.urls import path,include
from Tabelog import views
from Tabelog.views import ReviewList,ReviewDetail,ReviewForm,ReviewFix,ReviewDelete,ReviewContact,ReviewContactComplete
app_name = "Tabelog"
urlpatterns = [
path("lp/", views.lp,name="lp"),
path("list/", ReviewList.as_view(),name="list"),
path("detail/<str:pk>/",ReviewDetail.as_view(),name="detail"),
path("form/",ReviewForm.as_view(),name="form"),
path("form/fix/<str:pk>/",ReviewFix.as_view(),name="form_fix"),
path("form/delete/<str:pk>/",ReviewDelete.as_view(),name="delete"),
path("contact/",ReviewContact.as_view(),name="contact"),
path("contact/complete/",ReviewContactComplete.as_view(),name="ContactComplete")
]
forms.py
from django.shortcuts import render,redirect,get_object_or_404
from Tabelog.models import Tabelog
from Tabelog.forms import CreateTabelogForm
from django.views import generic
from Tabelog.forms import CreateTabelogForm,ContactForm
from django.urls import reverse_lazy
from django.core.mail import send_mail
from django.template.loader import render_to_string
# Create your views here.
def lp(request):
return render(request,"Tabelog/lp.html")
class ReviewList(generic.ListView):
model = Tabelog
class ReviewDetail(generic.DetailView):
model = Tabelog
class ReviewForm(generic.CreateView):
model = Tabelog
form_class = CreateTabelogForm
success_url = reverse_lazy("Tabelog:list")
class ReviewFix(generic.UpdateView):
model = Tabelog
fields = "__all__"
success_url = reverse_lazy("Tabelog:list")
class ReviewDelete(generic.DeleteView):
model = Tabelog
success_url = reverse_lazy("Tabelog:list")
class ReviewContact(generic.FormView):
template_name = "Tabelog/tabelog_contact.html"
form_class = ContactForm
success_url = reverse_lazy("Tabelog:ContactComplete")
def form_valid(self,form):
subject = "お問い合わせがありました"
message = render_to_string('Tabelog/mail.txt',form.cleaned_data,self.request)
from_email = "toiawase#gmail.com"
recipient_list = ["yutotennisnowboard#gmail.com"]
send_mail(subject,message,from_email,recipient_list)
return redirect('Tabelog:list')
class ReviewContactComplete(generic.TemplateView):
template_name = "Tabelog/tabelog_contact_complete.html"
tabelog_list.html
<!DOCTYPE html>
{% extends 'diary/base.html' %}
{% block title %}お店リスト{% endblock %}
{% block content %}
{% for list in object_list %}
<div class="card border-primary mb-3">
<div class="card-body text-primary">
<h1 class="card-title">{{list.store_name}}</h1>
<h2 class="card-text">{{ list.get_evaluation_display}}</h2>
<span class="card-text">{{ list.outline}}</span><br>
<button type="button" class="btn btn-light"> See More Detail! </button>
</div>
</div>
{% endfor %}
{% endblock %}
tabelog_detail.html
<!DOCTYPE html>
{% extends 'diary/base.html' %}
{% load static %}
{% block title %}Detail Page{% endblock %}
{% block design %}
<link rel="stylesheet" href="{% static 'css/detail.css' %}">
{% endblock %}
{% block content %}
<div class="container">
<div class="card border-info mb-3">
<div class="card-body">
<h1 class="card-title">{{object.title}}</h1>
<p class="card-text">投稿者:{{ object.writer }}</p>
<p class="card-text">作成日:{{ object.created_at}}</p>
<p class="card-text">更新日:{{ object.last_modefied}}</p>
<p class="card-text">カテゴリ:{{ object.category}}</p>
<p class="card-text">タグ:{% for tag in object.tag.all %}{{tag}},{% endfor %}</p>
<div class="card-text">
{{object.text| linebreaks | urlize }}
</div><br>
<button type="button" class="btn btn-light"> Change the post </button>
</div>
</div>
</div>
<div class="container">
<button type="button" class="btn btn-light"> コメントする</button>
</div>
<div class="container">
<article class="card border-info mb-3" id="comment">
<div class="card-body">
{% for comment in article.comment_set.all %}
<p class="card-text">{{comment | linebreaks | urlize}}</p>
{% endfor %}
</div>
</article>
</div>
{% endblock %}
The problem was caused by the detail page connected from list page. I realized that the error message doesn't necessarily show the specific cause.

Problem in calling items from the database django

I am trying to create a music streaming website.I have created a base template and extended it to two other templates.One is song_list.html and musicplayer.html.
I have coded the views and urls to get absolute url.
my song_list.html:
{% extends "home.html" %}
{% block content %}
<div class="container">
{% for post in posts %}
{{post.song_title}} by {{post.artist}} from {{post.album}}<br>
{% endfor %}
</div>
<div class="container">
{% include "pagination.html" with page=page_obj %}
</div>
{% endblock %}
I want to render songs from the database in the musicplayer page after clicking the anchor tag.
my msicplayer.html:
{% extends "home.html" %}
{% load i18n %}
{% block content %}
<div class="container">
<form class="" action="mediaplayer.html" method="get">
<div class="data-content">
<div class="container">
<div id="img-1" class="tabcontent">
<div class="blog-content">
<div class="row">
<div class="col-sm">
<div class="card" style="width: 18rem;">
<div class="img">
<img class="img-thumbnail" src="{{ post.img.url }}" alt="">
</div>
<div class="card-body">
<div class="title">
<p>{% trans 'Artist:' %} {{post.artist}} </p><br>
<p>{% trans 'Title:' %} {{post.song_title}}</p><br>
<p>{% trans 'Album:' %} {{post.album}}</p><br>
<p>{% trans 'Duration' %} {{post.song_duration}}</p><br>
</div>
<audio controls>
<source src='{{ post.song.url }}' type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
My app views:
from django.shortcuts import render,get_object_or_404
from .models import song_thumb
from django.core.paginator import Paginator, EmptyPage,PageNotAnInteger
from django.views.generic import ListView
# Create your views here.
class SongListView(ListView):
model=song_thumb
context_object_name='posts'
paginate_by=20
template_name='songlist.html'
def song_detail(request, year, month, day, post):
post=song_thumb.objects.all()
return render(request,'musicplayer.html',{'post': post})
my app models:
from django.db import models
from django.utils import timezone
from django.urls import reverse
# Create your models here.
class song_thumb(models.Model):
artist=models.CharField(max_length=100,null=True)
uploaded_by=models.CharField(max_length=100,null=True)
song_title=models.CharField(max_length=100,null=True)
slug=models.SlugField(max_length=250,null=True)
album=models.CharField(max_length=100,null=True)
song_duration=models.FloatField(null=True)
img=models.ImageField(upload_to='pics',null=True)
song=models.FileField(upload_to='media',null=True)
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
return reverse('song_detail',args=[self.publish.year,
self.publish.month,
self.publish.day, self.slug])
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.song_title
my app urls:
urlpatterns = [
path('',views.SongListView.as_view(),name='songs'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',views.song_detail,
name='song_detail'),
Like i said,I want a new page with the songdetails and mediaplayer to be rendered after i click the anchor tag in songlist.html.But when I click it i get a blank page.
here, you need to get the post as per the id but you are rendering everything here in you detail. So you can do this
view.py
from django.shortcuts import get_object_or_404
def song_detail(request, post_id):
post=get_object_or_404(Course, pk=post_id)
return render(request,'musicplayer.html',{'post': post})
urls.py
path('<int:post_id>/',views.song_detail,
name='song_detail'),
in you acnhor tag you can link to the particular post
<a href="{% url 'song_detail' post.id %}">

Categories