2

I am trying to load a pdf file in django which is already present in the database. I can access the pdf file using the url, "localhost:8000/documents/file.pdf" but when I perform the query and return the response containing this file, url it redirects to "localhost:8000/ans/documents/file.pdf" which doesn't exist. The html code is:

<form id="signup-form" method="POST" action="ans/">
        {% csrf_token %}
            <input type="text" name="id" id="id" placeholder="Report id" />
            <input type="submit" value="Check" />
        </form>

The path in urls.py is

path('ans/',views.func),

The view is:

def func(request):
if request.method=="POST":
    id=request.POST.get("id")
    ans = query.objects.get(id=id)
    response=ans.repo
    if ans is None:
        return render(request,"index.html",{})
    else:
        return redirect(response)

The bottomline is, I wan't to get rid of the "/ans/" in the url.

4

2 回答 2

1

当你把ans/它添加到 url 和文件位置后添加它。要解决此问题,请删除ans/并命名。

path('',views.func, name='func'),

此外,你在观点和形式上存在逻辑错误。您应该使用普通表单或 ModelForm 来获取 id 以形成表单。

def func(request):
    if request.method=="POST":
        id=request.POST.get("id") # do not get id from url in post method
        ans = query.objects.get(id=id)
        response=ans.repo
        if ans is None:
            return render(request,"index.html",{})# do not render index.html just use HttpResponseRedirect. rendering is used for forms.
        else:
            return redirect(response) # redirecting to the file directly is a bad pattern.

而不是这些糟糕的设计,您应该FileField在您的 models.py 类中使​​用,然后您将有资格在任何地方使用 file.url 调用获取 url 而无需重定向它,您可以轻松地将其分配给任何按钮。

注意:要跳过你应该使用我上面提到的动作属性form并在视图方法中呈现它。

编辑: 假设您想从特定对象查看文件(您只需要传递可以使用 url 模板标签完成的对象的 pk)

<a href="{% url 'specific_request' pk=query.pk %}">Specific request</a>

网址.py

path('response/request/<int:pk>/', views.specific_request, name='specific_request')

视图.py

def specific_request(request, pk):

    query = get_object_or_404(Request,
                              pk=pk)

    context = {
        'query': query
    }

    return render(request,
                  'view-request-specific.html',
                  context=context)

现在是时候点击 template.py查看上传的文件了

<label for="id_file">Uploaded file:   </label>
<a id="id_file" href="{{ query.file.url}}">  {{ query.file.url}}</a>

file.url是检索文件的确切 url 的魔法。所以当它被点击时,文件将被打开。

于 2018-11-24T20:42:24.290 回答
0

从模板文件中的表单标记中删除 action 属性,并将索引函数更改为接近以下内容。

def index(request):
    if request.method == "POST":
        form = Form(request.POST)
        if form.is_valid():
            id=request.POST.get("id")
            ans = query.objects.get(id=id)
            response=ans.repo
            if ans is None:
                return redirect("index.html")
            else:
                #return render (request,"ans.html",{'ans':response})
                return redirect(response)
    else:
        form = Form()
    return render(request,"index.html",{'form':form})

我没有测试上面的代码,所以请提供您看到的任何错误的反馈。

于 2018-11-25T08:42:05.337 回答