TemplateDoesNotExist位于/​​ accounts / upload_save / error

时间:2017-09-30 01:13:12

标签: python html django

我收到错误,在/ accounts / upload_save /上的TemplateDoesNotExist {'表格':}。 我在views.py 中写了,如

def upload(request, p_id):
    form = UserImageForm(request.POST or None)
    d = {
        'p_id': p_id,
        'form':form,
    }
    return render(request, 'registration/accounts/photo.html', d)

@csrf_exempt
def upload_save(request):

    if request.method == "POST":
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            data = Post()
            data.image = request.FILES['image']
            data.save()
    else:
        form = UploadForm()

    return render('registration/accounts/photo.html', {'form':form})

class UploadForm(forms.Form):
    image = forms.FileField()

urls.py

urlpatterns = [
    url(r'^regist/$', views.regist,name='regist' ),
    url(r'^regist_save/$', views.regist_save, name='regist_save'),
    url(r'^profile/$', views.profile, name='profile'),
    url(r'^photo/$', views.photo, name='photo'),
    url(r'^upload/(?P<p_id>\d+)/$', views.upload, name='upload'),
    url(r'^upload_save/$', views.upload_save, name='upload_save'),
]

profile.html

<div class="container">
      <form action="{% url 'accounts:upload_save' %}" method="POST" enctype="multipart/form-data">
        {% csrf_token %}

        <div class="input-group">
          <label class="input-group-btn">
            <span class="btn btn-primary btn-lg">
              SELECT FILE
            <input type="file" style="display:none" name="files[]" multiple>
            </span>
          </label>
          <input type="text" class="form-control" readonly="">
        </div>

        <div class="form-group">
        <input type="hidden" value="{{ p_id }}" name="p_id" class="form-control">
        </div>
        <div class="form-group">
        <input type="submit" value="SEND" class="form-control">
        </div>
      </form>
    </div>

当我把&#34; SEND&#34;按钮,我想在浏览器中显示photo.html。但现在错误发生了,虽然我在渲染中写了registration/accounts/photo.html。我真的无法理解如何解决这个问题。我该怎么办?

1 个答案:

答案 0 :(得分:1)

render传递请求作为第一个参数。

return render(request, 'registration/accounts/photo.html', {'form':form})

它会正常工作。