我的观点一次又一次地将我带到同一页面

时间:2019-03-01 15:54:37

标签: django django-views

伙计们,我制作了一个可以上传一些文件的模型,然后制作了两个视图upload_list.html和upload_detail.html列表页面包含指向实际详细信息页面的链接,但是在单击链接时将我带到相同的页面再次翻页

这是模型。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 conten`t %}`

1 个答案:

答案 0 :(得分:2)

您是否尝试过将urls.py这样放置?

url(r'^upload/$',views.upload_list,name = 'upload_list'),
url(r'^upload/(?P<pk>[-\w]+)/$',views.upload_detail,name = 'upload_detail'),

url(r'^upload/(?P<pk>[-\w]+)/$',views.upload_detail,name = 'upload_detail'),
url(r'^upload/$',views.upload_list,name = 'upload_list'),

$是表示字符串结尾的正则表达式。

希望对您有帮助