如何创建指向特定文件的链接?
models.py
class Post(models.Model):
....
class Presentation(models.Model):
pres_id = models.AutoField(primary_key=True)
description = models.CharField(max_length=100)
upload = models.FileField(upload_to='presentation', null=True)
def __str__(self):
return self.description
class Participant(models.Model):
conference = models.ForeignKey(Post, related_name = 'members', on_delete=models.CASCADE, null=True, blank=True) #Post class
presentation = models.ForeignKey(Presentation, related_name = 'pres', on_delete=models.CASCADE, null=True, blank=True)
views.py
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'plan/post_detail.html', {'post': post})
post_detail.html
{% for part in post.members.all %}
<a href="{{presentation.upload.url}}">{{part.presentation}}</a>
{% endfor %}
part.presentation仅返回演示文稿说明。但是,我还需要一个文件链接。我怎么解决这个问题?
(结构:有一个帖子包含有关会议参与者,演示文稿等的信息。演示文稿描述链接应该在浏览器中打开pdf文件)
EDIT1:谢谢。这有效,但现在我有一个错误的媒体方向。
settings.py
MEDIA_URL = 'plan/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
因此,当我通过管理面板上传文件时,文件存储在“媒体/演示文稿”中(因为它是在演示模型中编写的)。但请求会发送到“http://127.0.0.1:8000/plan/1/plan/media/presentation/”。我可以更改或添加什么来解决这个问题?
答案 0 :(得分:0)
确定。解决了。刚刚改变了
public class Example {
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in);
sc1.close();
Scanner sc2 = new Scanner(System.in);
sc2.next();
}
}
到
MEDIA_URL = 'plan/media/'