python - Django. Unbound method in urls -
i trying accept localhost:8000/upload file , run problem
unbound method upload_file() must called upload instance first argument (got wsgirequest instance instead)
urls.py
from django.conf.urls import include, url django.contrib import admin upload.views import upload urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^upload/$', upload.upload_file), url(r'^thanks/$', upload.thanks), ]
views.py
from django.http import httpresponse django.template import requestcontext django.shortcuts import render_to_response upload.forms import fileform upload.models import upfile class upload(): def upload_file(request): if request.method == "post": form = fileform(request.post, request.files) if form.is_valid(): form.save() return httpresponseredirect('thanks') else: form = fileform() return render_to_response('temp1.html', {'form': form}, context_instance = requestcontext(request)) def thanks(request): return render_to_response('temp2.html')
your view not inheriting django class based view: https://docs.djangoproject.com/en/1.8/topics/class-based-views/intro/
from django.views.generic import view class upload(view): def upload_file(self, request): if request.method == "post": form = fileform(request.post, request.files) if form.is_valid(): form.save() return httpresponseredirect('thanks') else: form = fileform() return render_to_response('temp1.html', {'form': form}, context_instance = requestcontext(request)) def thanks(self, request): return render_to_response('temp2.html')
you need inherit form 1 of these view classes otherwise django treat view normal class why getting error.
you need adjust urls.py:
urlpatterns = [ url(r'^upload/', upload.as_view()), ]
after looking through docs , @ structure of vie i'm not sure structuring views class based views. may want read through docs more , adjust structure accordingly.
i believe want somethign looks more this:
class upload(view): def get(self, request): form = fileform() return render_to_response('temp1.html', {'form': form}, context_instance = requestcontext(request)) def post(self, request): form = fileform(request.post, request.files) if form.is_valid(): form.save() return httpresponseredirect('thanks') else: return render_to_response('temp2.html')
Comments
Post a Comment