Error uploading files

I keep getting an error saying ‘str’ object has no attribute ‘get’ in attempt to uploading model-form… I’ve crossed check my models and forms, I can’t seem to figure out where the problem is coming from.

if request.method =="POST":
        form = GameUpload(request.POST, request.FILES)
        if form.is_valid():
            form.save(commit=False)
            form.instance.url = slugify(form.cleaned_data['title'])
            try:
                form.save() 

url is a model slugfield…upload.

Please post the complete view and the form, along with the model and the complete error message with the traceback.

Views.py

def upload_game(request):
    
    upload = False
    if request.method =="POST":
        form = GameUpload(request.POST, request.FILES)
        if form.is_valid():
            form.save(commit=False)
            form.instance.url = slugify(form.cleaned_data['title'])
            try:
                form.save() 
            except Exception as F:
                return f"Error {F}"
        else:
            error = form.errors.as_data()
            raise ValidationError(f'{error}')

        upload = True
    else:
        form = GameUpload()

        
    return render(request,
                      'home/add/upload.html',
                      {'form':form,
                       'upload':upload})

Models.py

class game(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(User,
                               related_name = 'games',
                               on_delete = models.CASCADE,
                               default = 2)
    
    description = models.TextField(max_length= 300,
                                   default = 'Enjoy the game 🎮')
    
    supports = models.CharField(max_length=14,
                                choices= Supports.choices,
                                default=Supports.Mobile_And_Pc)
    
    url = models.SlugField(max_length=90)
    Image_View = models.ImageField(upload_to='media_files/',
                                 validators = [validate_image_view])

    active = models.BooleanField(default=True)
    
    HTML_file = models.FileField(upload_to='html_files/',
                                 validators = [validate_html])
        
    JS_file = models.FileField(upload_to='js_files/',
                               validators =[validate_js])
    
    CSS_file = models.FileField(upload_to='css_files/',
                                validators = [validate_css])
    created = models.DateField(default=timezone.now)
    tweaked = models.DateField(auto_now=True)
    

Forms.py

class GameUpload(forms.ModelForm):
    class Meta:
        model = game
        fields = ['Image_View', 'title', 'supports', 'description', 'HTML_file', 'JS_file', 'CSS_file']
        
    

Traceback error

`Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/upload/

Django Version: 5.0.1
Python Version: 3.12.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'games.apps.GamesConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware']



Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\utils\deprecation.py", line 136, in __call__
    response = self.process_response(request, response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\middleware\clickjacking.py", line 27, in process_response
    if response.get("X-Frame-Options") is not None:
       ^^^^^^^^^^^^

Exception Type: AttributeError at /upload/
Exception Value: 'str' object has no attribute 'get'
`

One of the issues that is (potentially) hiding the real issue is that you have code paths that aren’t returning an HttpResponse.

Specifically:

This will return a string, which isn’t valid for a view.

There’s another error here, which is the root cause of the Exception being thrown, but this needs to be resolved before that can be identified and addressed.

All code paths through a view should return an HttpResponse.

1 Like

I did as you said and was able to trace the issue back to my foreign key default value… Already fixed the issue

Thanks for the heads up