大家好,我制作了一个可以上传一些文件的模型,然后制作了两个视图upload_list.html
和upload_detail.html
,列表页面包含指向实际详细信息页面的链接,但是在单击链接时需要我又回到同一页面
models.py
class Upload(models.Model):
image = models.ImageField(upload_to = 'images',)
file = models.FileField(upload_to = 'images/%Y/%M/%d/')
name = models.CharField(max_length = 200)
def __str__(self):
return self.name
def get_absolute_url(self):
return self.pk{}
这是views.py
def upload_list(request):
upload_list = Upload.objects.all()
return render(request,'app/upload_list.html',{'upload_list':upload_list})
def upload_detail(request,pk):
upload_detail = get_object_or_404(Upload,pk = pk)
return render(request,'app/upload_detail.html',{'upload_detail':upload_detail})
这是urls.py
url(r'^upload/',views.upload_list,name = 'upload_list'),
url(r'^upload/(?P<pk>[-\w]+)/$',views.upload_detail,name = 'upload_detail'),
这是upload_list.html
{% extends 'app/base.html' %}
{% block content %}
{% load static %}
{% for i in upload_list %}
<div class="jumbotron">
<a href="{% url 'upload_detail' i.pk %}">{{i.name}}</a>
<br>
</div>
{% endfor %}
{% include 'app/index_js.html' %}
{% endblock content %}
这是upload_detail.html
{% extends 'app/base.html' %}
{% block content %}
{% load static %}
<div class="jumbotron">
<h1>{{upload_detail.name}}</h1>
<img src="{{upload_detail.name}}" alt="'Image for you,r betterment "></img>
{{upload_detail.file}}
</div>
{% include 'app/index_js.html' %}
{% endblock content %}