我在/ accounts / upload_save /遇到错误,TemplateDoesNotExist
{'form':}。
我写在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>


当我按下“发送”按钮时,我想在浏览器中显示photo.html。但是现在发生了错误,尽管我在render中写了registration/accounts/photo.html。我真的不知道如何解决此问题。我该怎么办?

最佳答案

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

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


它将正常工作。

关于python -/accounts/upload_save/错误处的TemplateDoesNotExist,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46498639/

10-13 02:08