簡體   English   中英

無法將表單數據保存到Django數據庫

[英]Can't save form data to Django database

我正在為我的Django項目創建表單,但有一個問題:我無法將其中一個表單的表單數據保存到Django數據庫中。 我可以為Wish_list模型添加數據,但不能添加評論。 提交表單后,沒有任何錯誤消息,也沒有任何更改。 您能告訴我哪里有錯誤嗎?

這是我的模型(來自moders.py):

class Person (AbstractUser):
    phone_number = models.CharField(max_length=30)
    place_of_work_or_study = models.CharField(max_length=100)
    img = models.ImageField(upload_to='Person/', null=True, blank=True)
    class Meta:
        verbose_name = 'Person'
        verbose_name_plural = 'Users'
    def __unicode__(self):
        return self.username

class Wish_list(models.Model):
    person = models.ForeignKey(Person, null=True)
    types = (
        ('Educational', 'Educational'),
        ('Cultural', 'Cultural'),
        ('Sports', 'Sports'),
        ('Fun', 'Fun'),
        ('Other', 'Other')
    )
    type = models.CharField(max_length=50, choices=types, default='Other')
    periods = (
        ('Up to 1 hour', 'Up to 1 hour'),
        ('From 1 to 2 hours', 'From 1 to 2 hours'),
        ('From 2 to 3 hours', 'From 2 to 3 hours'),
        ('More then 3 hours', 'More then 3 hours')
    )
    time_need = models.CharField(max_length=50, choices=periods)
    place_name = models.CharField(max_length=50, blank=False, null=False)
    description = models.CharField(max_length=1000)

    def __unicode__(self):
        return unicode(self.place_name)

class Comment_to_wish_list(models.Model):
    person = models.ForeignKey(Person, null=True)
    comment_to = models.ForeignKey(Wish_list, null=True)
    text = models.CharField(max_length=500)
    def published (self):
        self.published_date = timezone.now()
        self.save()
    class Meta:
        verbose_name_plural = 'comments_to_wish_lists'
    def __unicode__(self):
        return unicode(self.comment_to)

這是forms.py:

class Wish_listForm(forms.ModelForm):
    place_name = forms.CharField(max_length=50, help_text='enter the         place_name')
    types = (
        ('Educational', 'Educational'),
        ('Cultural', 'Cultural'),
        ('Sports', 'Sports'),
        ('Fun', 'Fun'),
        ('Other', 'Other')
    )
    type = forms.ChoiceField(choices=types, help_text='choose the type')
    periods = (
        ('Up to 1 hour', 'Up to 1 hour'),
        ('From 1 to 2 hours', 'From 1 to 2 hours'),
        ('From 2 to 3 hours', 'From 2 to 3 hours'),
        ('More then 3 hours', 'More then 3 hours')
    )
    time_need = forms.ChoiceField(choices=periods, help_text='period')
    description = forms.CharField(max_length=1000, help_text='description')
    views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
    likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
    class Meta:
        model = Wish_list
        fields = ('place_name', 'type', 'time_need', 'description','likes')

class Comment_to_wish_listForm(forms.ModelForm):
    text = forms.CharField(max_length=500, help_text='enter the text')
    views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
    likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
    class Meta:
        model = Comment_to_wish_list
        fields = ('text', 'views', 'likes')

這是urls.py:

app_name = 'friends_plans'
urlpatterns = [
url(r'^$', views.index, name='index'), # start page
url(r'^users/$', views.listing, name='listing'),
url(r'^(?P<person_id>[0-9]+)/wish_list/$',views.wish_list,      name='wish_list'),
url(r'^(?P<person_id>[0-9]+)/$', views.user, name='user'),
url(r'^(?P<person_id>[0-9]+)/(?P<day_id>[0-9]+)/$', views.day, name='day'),
url(r'^add_wish_list/$', views.add_wish_list, name='add_wish_list'),
url(r'^(?P<wish_list_id>[0-9]+)/comment/$', views.comment, name='comment'),
url(r'^(?P<wish_list_id>[0-9]+)/add_comment/$', views.add_comment,   name='add_comment'),
]

這是來自views.py:

def add_comment(request, wish_list_id):
    wish_list = get_object_or_404(Wish_list, pk=wish_list_id)
    if request.method == 'POST':
        form = Comment_to_wish_listForm(request.POST, instance=wish_list)
        if form.is_valid():
            newform=form.save(commit=False)
            newform.person = request.user
            newform.save()
        else:
            print form.errors
    else:
        form = Comment_to_wish_listForm()
    return render(request, 'friends_plans/add_comment.html', {'form': form})

這是評論模板

{% extends 'friends_plans/base.html' %}
{% block title %} Comments {% endblock %}
{% block content %}
    {% for comment_to_wish_list in wish_list.comment_to_wish_list_set.all %}
        <div>{{comment_to_wish_list.text}}</div>
    {% endfor %}

<div><a href="{% url 'friends_plans:add_comment' wish_list.pk %}">Add  comment</a> </div>
</div>
{% endblock %}

這是一個添加評論的模板(我有一個問題):

{% extends 'friends_plans/base.html' %}
{% block content %}

<h1>Add a wish_list</h1>
<form method="post"  action="">
    {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Add comment" />
</form>
{% endblock %}

instance必須與表單的模型Comment_to_wish_list instance=wish_list傳遞給表單沒有意義,因為模型不匹配。

您可以在使用commit=False保存表單后,以與設置person字段相同的方式設置comment_to字段:

wish_list = get_object_or_404(Wish_list, pk=wish_list_id)
if request.method == 'POST':
    form = Comment_to_wish_listForm(request.POST)
    if form.is_valid():
        instance=form.save(commit=False)
        instance.person = request.user
        instance.comment_to = wish_list
        instance.save()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM