user8681386 user8681386 - 3 years ago 159
Python Question

TemplateDoesNotExist at /accounts/upload_save/ error

I got an error,TemplateDoesNotExist at /accounts/upload_save/
{'form': } .
I wrote in like

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

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']
form = UploadForm()

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

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

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/$',, 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 is

<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">
<input type="file" style="display:none" name="files[]" multiple>
<input type="text" class="form-control" readonly="">

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

When I put "SEND" button,I wanna show photo.html in the browser.But now the error happens, although I wrote
in render.I really cannot understand how to fix this.What should I do?

Answer Source

In render pass request as first argument.

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

It will work fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download