注意:
urls.py
urlpatterns = [
path('',views.index),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
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})
forms.py
class Form(forms.Form):
id = forms.CharField(label="Report ID", max_length=100)
models.py
class query(models.Model):
id=models.IntegerField(primary_key=True)
repo=models.FileField(upload_to='documents/')
index.html
<!-- Search Form -->
<form id="signup-form" method="POST" action="">
{% csrf_token %}
{{form}}
<input type="submit" value="Check" />
</form>
答案 0 :(得分:0)
文件字段的URL在url
属性中,因此您需要使用ans.repo.url
。