class City(models.Model):
name = models.CharField("Name of City", max_length=100, unique = True)
image = ImageField(upload_to=image_path)
slug = AutoSlugField(populate_from='name')
country = models.ForeignKey(Country, related_name = 'cities')
def __unicode__(self):
return self.name
class addCity(CreateView):
model = City
fields = ['name', 'image', 'country']
def get_success_url(self):
return '/register/'
<form action="" method="post" enctype="multi-part/formdata">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Create" />
</form>
我可以从管理员创建城市,但不能从CreateView创建城市。
当我在city_form.html
上填写详细信息并按Create
时,会在名称值This field is required
下面。它是slu field领域吗?如果是,那么为什么我只能使用City
从admin
创建name, image, country
?有什么帮助吗?
谢谢。