为什么这个CreateView不起作用?

时间:2013-11-09 08:25:21

标签: django forms django-class-based-views

models.py

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

views.py

class addCity(CreateView):
    model = City
    fields = ['name', 'image', 'country']


    def get_success_url(self):
        return '/register/'

city_form.html

<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领域吗?如果是,那么为什么我只能使用Cityadmin创建name, image, country?有什么帮助吗?

谢谢。

0 个答案:

没有答案