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 views.py like

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


When I put "SEND" button,I wanna show photo.html in the browser.But now the error happens, although I wrote
registration/accounts/photo.html
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