Giter Club home page Giter Club logo

Comments (20)

trco avatar trco commented on June 28, 2024

@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.

trco avatar trco commented on June 28, 2024

Closed due to inactivity.

from django-bootstrap-modal-forms.

free9ja avatar free9ja commented on June 28, 2024

Hello trco, does this mean that the plugin doesn't support file upload?

from django-bootstrap-modal-forms.

trco avatar trco commented on June 28, 2024

@free9ja Plugin supports file upload.

from django-bootstrap-modal-forms.

free9ja avatar free9ja commented on June 28, 2024

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.

trco avatar trco commented on June 28, 2024

@free9ja Please format properly and add html definition of the form.

from django-bootstrap-modal-forms.

free9ja avatar free9ja commented on June 28, 2024

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">&times;</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.

free9ja avatar free9ja commented on June 28, 2024

` 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.

trco avatar trco commented on June 28, 2024

@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.

free9ja avatar free9ja commented on June 28, 2024

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.

trco avatar trco commented on June 28, 2024

@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.

free9ja avatar free9ja commented on June 28, 2024

I got everything to work before implementing the plugin.

from django-bootstrap-modal-forms.

free9ja avatar free9ja commented on June 28, 2024

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.

trco avatar trco commented on June 28, 2024

@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.

free9ja avatar free9ja commented on June 28, 2024

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.

free9ja avatar free9ja commented on June 28, 2024

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.

trco avatar trco commented on June 28, 2024

@free9ja Great that you solved it. Staring a repository is highly appreciated.

from django-bootstrap-modal-forms.

free9ja avatar free9ja commented on June 28, 2024

done!

from django-bootstrap-modal-forms.

guillermo426 avatar guillermo426 commented on June 28, 2024

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.

waalwang avatar waalwang commented on June 28, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.