File not being written to
I am working on a website with django and am currently trying to add a way for users to upload a file to the website and just to test have it write to my pc.
But the problem is that nothing is being written to my desktop. I am not getting any errors just nothing is being written. The file I am testing with is a jpeg.
The function:
def download_file(filename):
with open(r'C:\Users\kenco\OneDrive\Desktop\file.jpg', 'wb+') as destination:
destination.write(filename)
The django view:
def file_form(request):
if request.method == 'POST':
form = file_uploader(request.POST,request.FILES)
if form.is_valid():
download_file(filename=request.FILES['file'])
return HttpResponseRedirect('/success')
else:
form = file_uploader()
return render(request, 'home_page.html', {'form' : form})
The form:
from django import forms
class file_uploader(forms.Form):
question = forms.CharField(max_length=100)
image = forms.FileField()
I tried to just write to desktop and it didnt work. I tried to do the string without a r string. And I tried to write it without the .jpg
This code is also based around the django documentation: https://docs.djangoproject.com/en/5.1/topics/http/file-uploads/
I found an answer
There was a problem with my urls
It went from:
urlpatterns= [
path('home/', TemplateView.as_view(template_name="home_page.html"), name='home_page'),
path('forms/', views.file_form, name='file_form')
]
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name="home_page.html"), name='home_page'),
path('file/', include('home.urls'))
]
To:
urlpatterns = [
path('home/', TemplateView.as_view(template_name="home_page.html"), name='home_page'),
path('forms/', views.file_form, name='file_form'),
]
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
]