简体   繁体   中英

getattr(): attribute name must be string

Very beginner here i am not getting whats the error here
#Models.py file

`from django.db import models

    class User(models.Model):
        name = models.CharField(max_length=20)
        email = models.CharField(max_length=100)
        date_created = models.DateTimeField(auto_now_add=True)

        def __str__(self):
            return self.name

    class Recipe(models.Model):
        name = models.CharField(max_length=50,null=True)
        steps = models.CharField(max_length=2000,null=True)
        image = models.ImageField(height_field=200,width_field=200,null = True, blank = True)
        ingredients = models.CharField(max_length=100, null=True)
        description = models.CharField(max_length=1000,null=True)
        user = models.ForeignKey(User, on_delete=models.CASCADE)`

#Form file

    `from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Recipe

class SignupForm(UserCreationForm):
    username = forms.CharField(max_length=30)
    email = forms.EmailField(max_length=200)

    class Meta:
        model = User
        fields = ['username','email','password1','password2']

class RecipeForm(ModelForm):
    class Meta:
        model = Recipe
        fields = ['name','steps','image','ingredients','description']`
#views.py file

    `from django.shortcuts import render, redirect
from .forms import SignupForm,RecipeForm
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
from django.contrib import messages

# views here
 @login_required(login_url='blog-login')
    def recipes(request):
        form = RecipeForm()
        if request.method == "POST":
            form = RecipeForm(request.POST)
            if form.is_valid():
                form.save()
                return redirect('blog-home')
        context = {'form':form}
        return render(request,'food_app/Add_recipe.html',context)`

The Recipe models ImageField 's height_field and width_field are both supposed to be the names of fields that you can store the value in, not an integer value.

Either remove these properties or add a couple fields to hold the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM