![](/img/trans.png)
[英]TypeError: getattr(): attribute name must be string in Django
[英]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)`
该Recipe
模型ImageField
的height_field
和width_field
都应该是字段,你可以存储,而不是一个整数值值的名称。
删除这些属性或添加几个字段来保存值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.