Comments (20)
@akms04 Due to the security reasons the FileField is always reset to empty value, after unsuccessful form validation. That's how Django works. In case that you have defined FileField with blank=False, your form will throw required field error. Mark it with blank=True. In this case it will not be required and you will be able to post form without selected file.
from django-bootstrap-modal-forms.
Closed due to inactivity.
from django-bootstrap-modal-forms.
Hello trco, does this mean that the plugin doesn't support file upload?
from django-bootstrap-modal-forms.
@free9ja Plugin supports file upload.
from django-bootstrap-modal-forms.
request.FILES is empty`class BookCreateView(BSModalCreateView):
template_name = 'cms/new_category.html'
form_class = AddNewCategoryForm
success_message = 'Success: Book was created.'
success_url = reverse_lazy('index')
def get(self, request, *args, **kwargs):
form = self.form_class()
print('get')
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST, request.FILES)
if request.FILES:
print('Files Here!')
if form.is_valid():
print('post 2')
form.save(request)
users = User.objects.all()
return render(request,'cms/new_category.html',{'users':users})
else:
return render(request,'cms/new_category.html',{'form':form})`
from django-bootstrap-modal-forms.
@free9ja Please format properly and add html definition of the form.
from django-bootstrap-modal-forms.
forms.py`
class AddNewCategoryForm(BSModalForm):
class Meta:
model = Category
fields = ('CategoryName', 'CatgeoryDescription','Categorycover')
def __init__(self, *args, **kwargs):
self.request = kwargs.pop( None,'request')
super(AddNewCategoryForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs['class'] = 'form-control
def save(self, request):
obj = super(AddNewCategoryForm, self).save(commit=False)
obj.LastModifiedBy = request.user
obj.save()
new_category.html
`
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title">Create new Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div><div class="modal-body">
{% for field in form %}
<div class="form-group{% if field.errors %} invalid{% endif %}">
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% for error in field.errors %}
<p class="help-block">{{ error }}</p>
{% endfor %}
</div>
{% endfor %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="submit-btn btn btn-primary">Create</button>
</div>
views.py
class BookCreateView(BSModalCreateView):
template_name = 'cms/new_category.html'
form_class = AddNewCategoryForm
success_message = 'Success: Book was created.'
success_url = reverse_lazy('index')
def get(self, request, *args, **kwargs):
form = self.form_class()
print('get')
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST, request.FILES)
if request.FILES:
print('Files Here!')
if form.is_valid():
print('post 2')
form.save(request)
users = User.objects.all()
return render(request,'cms/new_category.html',{'users':users})
else:
return render(request,'cms/new_category.html',{'form':form})
from django-bootstrap-modal-forms.
` model.py
class Category(models.Model):
CategoryName = models.TextField("Category Name")
CatgeoryDescription = models.TextField("Category Description")
Categorycover = models.ImageField("Cover",upload_to='images/')
Created= models.DateTimeField("Createde Date",default=datetime.now)
LastModifiedTime = models.DateTimeField("Last Update Date",default=datetime.now)
LastModifiedBy = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.CategoryName`
from django-bootstrap-modal-forms.
@free9ja For the start your kwargs.pop
in __init__
of the form in forms.py
is wrong. Python says dictionary.pop(keyname, defaultvalue)
. Meaning you should have kwargs.pop('request', None)
. If this simple change won't work you should concentrate on all the custom code you have written. Images and files should work out of the box with this package. Unfortunately I can't study your custom code in details.
from django-bootstrap-modal-forms.
I haven't written any more custom code that what i shared. the simple change didnt work . It works without using the plugin. asides the plugin no other change is introduced.
from django-bootstrap-modal-forms.
@free9ja Have you confirmed that your form which is loaded into the modal and lives on dedicated url works? Can you upload file through it?
from django-bootstrap-modal-forms.
I got everything to work before implementing the plugin.
from django-bootstrap-modal-forms.
I adjusted the template slightly. The request>FILES is no longer empty. But i get the error 'NoneType' object has no attribute 'is_ajax'
when i post
from django-bootstrap-modal-forms.
@free9ja See CreateUpdateAjaxMixin
. Inside of this mixin self.request.is_ajax
is called. In your case request is obviously None and thus this code doesn't pass. If you want to override things in BSModalForm
be sure to understand what you're doing.
from django-bootstrap-modal-forms.
Thanks trco.
I didn't realize i had to use this. I followed the documentation on your page regarding creating models. I will and provide feedback. I'm certain it will work this time. Thanks a lot. :)
from django-bootstrap-modal-forms.
It worked perfectly. Sorry to have stressed you unnecessarily. I am new to Python. Thank you for such an amazing plugin.
from django-bootstrap-modal-forms.
@free9ja Great that you solved it. Staring a repository is highly appreciated.
from django-bootstrap-modal-forms.
done!
from django-bootstrap-modal-forms.
I adjusted the template slightly. The request>FILES is no longer empty. But i get the error
'NoneType' object has no attribute 'is_ajax'
when i post
@free9ja How do you 've adjusted the template? I still having the same problem, FILES is allways empty and validation error get "This field is required."
from django-bootstrap-modal-forms.
@free9ja, @trco, I am encountering the same issue, request.POST looks good but request.FILES is still empty even though I have been trying everything I can do, such as with enctype="multipart/form-data".
Any clues about modifying the template? my template is exactly the same as free9ja's....
from django-bootstrap-modal-forms.
Related Issues (20)
- missing "database" folder
- Allow to send extra parameters in the POST request.
- Multiple submit buttons in modal
- Maintenance of the project HOT 9
- Automate JS Minify Process
- How to make a nested modal
- Undesired GET request when using a BSModalCreateView HOT 4
- Incorrect errorClass in documentation HOT 1
- Generic views don't respond to get_success_url() since version 3.0.0 HOT 5
- Delete view success message is shown although object not deleted HOT 1
- form validation object already exist error does not shown in modal after modal form submissin with crispy form within modal HOT 3
- Django Internal server error - widget_tweaks failed to parse render_field HOT 1
- Checkbox does not render correctly. HOT 3
- btnSubmit fails detection after html minification
- Double post when submitting modal form HOT 5
- Restrict users from directly accessing the form url HOT 1
- How to remove Click listener HOT 1
- Open a new modal with the current modal form's results
- Event listeners disappear after the form is invalid. HOT 1
- FormValidationMixin shadows setting of self.object (defined in BaseCreateView)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-bootstrap-modal-forms.