Save .docx file django for specific user - python

I'm creating a docx file with user input in django. For now when the submit button is clicked the download link generates and users can download the file. However I want to save the file in file store and create a template where the user can see all the docx files (or a hyperlink) that he has created in the past. "Somewhat like a database"
models.py
class Timetables(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
default=1, related_name='timetables_files', null=True)
timetable_files = models.FileField(
null=True, blank=True, upload_to='word_documents')
views.py
def school_view(request):
if request.method == 'POST':
worddocument = docx.Document()
documenttitle = worddocument.add_heading(
school_name_view.title(), 0)
file = io.BytesIO()
worddocument.save(file)
response = HttpResponse(
content_type='application/vnd.openxmlformats-
officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename =quicktimetable.docx'
worddocument.save(response)
path = join(settings.MEDIA_ROOT, 'word_documents',
'quicktimetable.
file = open(path, 'w+b')
return response
class TimetablesView(ListView):
model = Timetables
template_name = 'quickmain/timetables_list.html'
timetables_list.html
{% if user.is_authenticated %}
{% for f in files %}
{{ f.files.url }}
{% endfor %}
{% for timetable_files in user.timetables_files.all %}
<embed src = "{{timetable_files.timetable_files.url}}" target="__blank">
{% endfor %}
{% else %}
How can I save the files which are generated and show all the files related to that user on a different a template.
I have a FileField in my models.py but no idea how to make use of it. I got a suggestion to regenerate file whenever users ask for it but I have no idea about that as well.

You have to create the relation between the file and user model.
Example
in your model
class DocxFile(models.Model):
user = models.ForiegnKey(User) #your user model name
file = models.FileField('upload_to='/')
then in your views
from yourappname.models import DocxFile
def docs_file(request):
files = DocxFile.object.filter(user=request.user)
return render('your template path', {'files':files})
in your template
{% for f in files%}
{{f.file.url}}
{% endfor %}

Related

unable to upload files in django through web forms

I am trying to create a django website similar to that of Udemy or Coursera. I am trying to implement a feature that lets users add courses.
Here is my view for adding the course:
def create_course(request):
form = CreateCourseForm()
if request.method == 'POST':
form = CreateCourseForm(request.POST)
if form.is_valid():
new_course = form.save(commit=False)
new_course.instructor = request.user
new_course.save()
return redirect('home')
return render(request, 'courses/create.html',{'form':form})
Here is my form:
class CreateCourseForm(forms.ModelForm):
class Meta:
model = Course
fields = ('name','video','description','category',)
My course model:
class Course(models.Model):
name = models.CharField(max_length=200)
instructor = models.ForeignKey(User, on_delete=models.CASCADE)
video = models.FileField(upload_to='videos/')
description = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
def __str__(self):
return self.name
And finally my web form:
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<h1>Create a Course</h1>
<hr>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-sm btn-outline-primary" type="submit">Create Course</button>
</form>
{% endblock %}
The issue i am facing is that when i try to create a course, the video field shows an error "This field is required" even when I have uploaded a video.
I tried researching a bit and found out that adding enctype="multipart/form-data" to the form was required, but even adding that didn't solve my problem
Can anyone help me here?
You need to pass request.POST and request.FILES to the form, so:
def create_course(request):
form = CreateCourseForm()
if request.method == 'POST':
# add request.FILES &downarrow;
form = CreateCourseForm(request.POST, request.FILES)
# …
return render(request, 'courses/create.html',{'form':form})
If you only pass request.POST, you are only passing the items of the form that are not file uploads. This then will indeed add an error to the form fields of these files, saying that the form requires data for these fields.
as willem told you have to request file in you form like this
def create_course(request):
form = CreateCourseForm()
if request.method == 'POST':
form = CreateCourseForm(request.POST, request.FILES)
if form.is_valid():
new_course = form.save(commit=False)
new_course.instructor = request.user
new_course.save()
return redirect('home')
return render(request, 'courses/create.html',{'form':form})
but i think you have to know one more you don't have to pass / in while telling django where to upload that file
class Course(models.Model):
name = models.CharField(max_length=200)
instructor = models.ForeignKey(User, on_delete=models.CASCADE)
video = models.FileField(upload_to='videos')
description = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
def __str__(self):
return self.name
offcourse willem answer will solve your problem but you will see in your media folder a new folder name videos/ got created where you file is storing instead of your actual folder becuase you are telling django to upload that file in folder name vedios/ and if django doesn't find that folder it will create that folder with that name and then stat uploading it

Django Image Field not Updating on the Front

I am trying to get my app to validate the image upload field in my app's template and save any new image that is uploaded. Everything works fine on the admin side of things but when I make an image change on the front, it doesn't get saved.
Here is my model
def imageupload(instance, filename):
return os.path.join('static/petition-photos/', filename)
# Create your models here.
class Petition(models.Model):
title = models.CharField(max_length= 90, default="Enter petition title here")
created_on = models.DateTimeField(auto_now_add=True)
image = models.ImageField(null=False, upload_to=imageupload)
video = models.CharField(max_length=600, default="Enter an external video link")
petition = models.TextField(null=False, default="Type your petition here")
created_by = models.ForeignKey(User)
Here is my view class:
class NewPetitionView(generic.edit.CreateView):
model = Petition
template_name = 'petition/petition_form.html'
fields= ['title','petition', 'image', 'video']
success_url = '/dashboard/'
def form_valid(self, form):
form.instance.created_by = self.request.user
return super(NewPetitionView, self).form_valid(form)
And this is my form template:
{% include 'layout/header.html' %}
{% block content %}
<form action="" method="post">
{{form.as_p}}
<button type="submit">Submit</button>
{% csrf_token %}
</form>
{% endblock %}
When I upload an image to a new post or try to edit the image field of one of the posted items, I get a "This field is required" notification.
What am I doing wrong?
You need in your <form> add enctype="multipart/form-data" in order to let file upload work.
<form action="" method="post" enctype="multipart/form-data">
django doc.

Multiple file uploads using formset?

models.py
class Album(models.Model):
file_upload = models.FileField(upload_to=content_file_name)
name_content = models.CharField(max_length=100)
And Function is here
def content_file_name(instance, filename):
upload_dir = os.path.join('uploads', 'resource')
return os.path.join(upload_dir, filename)
forms.py
class Albumform(forms.Form):
name_content = forms.CharField(max_length=100)
file_upload = forms.FileField( 'please select' )
This is my views.py
from django.forms.formsets import formset_factory
def viewalbum(request):
AlbumFormSet = formset_factory(Albumform, extra=3, max_num=10,)
if request.method == 'POST':
formset = AlbumFormSet(request.POST, request.FILES)
if formset.is_valid():
albumvalue = Album()
albumvalue.name_content = request.POST.get('name_content', None)
albumvalue.file_upload = request.FILES['file_upload']
albumvalue.save()
return HttpResponseRedirect(reverse('views.viewalbum'))
else:
formset = AlbumFormSet()
return render_to_response('audio/test.html',{'formset': formset}, context_instance=RequestContext(request))
and the html looks like this
<form action="" method="POST" enctype="multipart/form-data">
{{ formset.management_form }}
{% csrf_token %}
<table>
{% for form in formset.forms %}
{{ form }}
{% endfor %}
</table>
The problem is i can only upload one file at the time. Give me solution to upload multiple file in the same time and saved into resource folder. Thanks
If you want to upload multiple files, then you need multiple filefields. That is your option A. If you choose to go for it, create formset and use it to provide multiple file upload.
your option B is to use some kind of javascript widgets that make multiple file uploads possible.

Attaching a current User object to Django form

I am working on an app that has a section with with a file upload form for .txt fiels. I would like for the current user that is uploading the file to be added along with the file and the file name. Currently, I can do this successfully in the admin section but I just cant get it to save via the form itself. Any Ideas?
Here are the models:
class UploadedTextFile(models.Model):
file = models.FileField(upload_to="textfiles")
filename = models.CharField(max_length = 50)
username = models.ForeignKey(User, blank=True, null=True)
class UploadedTextFileForm(ModelForm):
class Meta:
model = UploadedTextFile
fields = ['file', 'filename']
Here is my view:
def inputtest(request):
#response for file being submited
if request.method == "POST":
form = UploadedTextFileForm(request.POST)
if form.is_valid():
new_form = form.save(commit=False)
new_form.username = request.user
new_form.save()
return render(request, 'about.html')
inputtest = UploadedTextFileForm()
return render(request, 'failed.html', {'inputtest': inputtest})
else:
inputtest = UploadedTextFileForm()
return render(request, 'inputtest.html', {'inputtest': inputtest})
Here is my html:
{% extends 'base.html' %}
{% block content %}
<form method="post">{% csrf_token %}
{{ inputtest.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock content %}
Doing it in the view (as you've shown) is the right way to do this. Most likely you're having problems because you've left username as a field on the form, and because the FK model field doesn't have blank=True set the form requires the field to be provided. You should explicitly declare just the subset fields that you want to accept user input for in the form's Meta class.
class UploadedTextFileForm(ModelForm):
class Meta:
model = UploadedTextFile
fields = ['file', 'filename']
I am not sure why you're rendering a different template when the form is not valid, but no matter what you're not providing the form object in the context. This means that you'll never see any errors the form detects, which is probably what's happening with this code - you're not seeing the error that username is not provided.

FileField not uploading the file

I am relatively new to python and Django.
The form is displaying itself, but when i choose a file and hit save, the form just refreshes itself and the selected file in the FormField disappears and the forms says that this filed is required.
The code is :
models.py
class ScribbleMedia(models.Model):
media = models.FileField(upload_to='photos')
def __unicode__(self):
return self.media
class Scribble(models.Model):
body = models.TextField()
user = models.ForeignKey(User)
media = models.ForeignKey(ScribbleMedia)
def __unicode__(self):
return u'%s, %s' % (self.user.username, self.media)
class Tag(models.Model):
name = models.CharField(max_length=64, unique= True)
scribbles = models.ManyToManyField(Scribble)
def __unicode__(self):
return self.name
views.py
def scribbler_save_page(request):
if request.method == 'POST':
form = ScribbleSaveForm(request.POST)
if form.is_valid():
#Create or get media.
media, dummy = ScribbleMedia.objects.get_or_create(
media=form.cleaned_data['media']
)
#Create or get media.
scribble, created=Scribble.objects.get_or_create(
user=request.user,
media=media
)
#update scribble body
scribble.body=form.cleaned_data['body']
# If the scibble is being updated, clear old tag list.
if not created:
scribble.tag_set.clear()
# Create new tag list.
tag_names = form.cleaned_data['tags'].split()
for tag_name in tag_names:
tag, dummy = Tag.objects.get_or_create(name=tag_name)
scribble.tag_set.add(tag)
# Save scribble to database.
scribble.save()
return HttpResponseRedirect ( '/user/%s/' % request.user.username
)
else:
form = ScribbleSaveForm()
variables = RequestContext (request, {
'form': form
})
return render_to_response ('scribble_save.html', variables)
forms.py
class ScribbleSaveForm(forms.Form):
media=forms.FileField(
label=u'add file',
widget=forms.FileInput()
)
text=forms.CharField(
label=u'description',
widget=forms.Textarea()
)
tags=forms.CharField(
label=u'Tags',
required=False,
widget=forms.TextInput(attrs={'size':64})
)
urls.py
(r'^save/$', scribbler_save_page),
scribble_save.html
{% extends "base.html" %}
{% block title %}Save Scribble {% endblock %}
{% block head %}Save Scribble { % endblock % }
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="save" />
{% csrf_token %}
</form>
{% endblock %}
Two things you need to do specially for file uploads:
(1) Your HTML form element must include the attribute enctype with the value multipart/form-data.
<form enctype="multipart/form-data" method="post" action=".">
(2) In your view, you'll find uploaded files in request.FILES instead of request.POST. To bind uploaded files to your form, pass in request.FILES as the second argument to the form constructor.
form = ScribbleSaveForm(request.POST, request.FILES)
Source: Django docs - The Forms API - Binding uploaded files to a form

Categories

Resources