Ich erstelle Formulare für mein Django-Projekt, und ich habe ein Problem: Ich kann Formulardaten für eines meiner Formulare nicht in der Django-Datenbank speichern. Ich kann Daten für das Wish_list-Modell hinzufügen, aber keinen Kommentar hinzufügen. Es gibt keine Fehlermeldungen, nur keine Änderungen nach dem Absenden des Formulars. Können Sie mir bitte sagen, wo ich einen Fehler habe?Formulardaten können nicht in der Django-Datenbank gespeichert werden
Dies ist meine Modelle (von 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)
Dies ist 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')
Dies ist 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'),
]
Dies ist aus 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})
Diese
ist die Vorlage für Kommentar
{% 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 %}
Und das ist eine Vorlage hinzufügen Kommentar (die, die ich habe ein Problem mit):
{% 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 %}
Sie scheinen nichts zu tun, das Ergebnis der Comment_to_wish_listForm zu beziehen zur aktuellen Wish_list. (Und bitte verwenden Sie die richtige Bezeichnung für PEP8: WishList, CommentToWishListForm.) –
Vielen Dank für Ihre Antwort, Daniel. Aber ich habe keine Ahnung, was zu tun ist, um das zu korrigieren. Ich dachte, dass 'instance = wish_list' bedeutet, dass die 'wish_list' und alle damit verbundenen Objekte nach dem Absenden des Formulars aktualisiert werden. –
Zusätzlich zur Lösung des Problems können Sie dieses Dokument lesen. Sie können dabei helfen, die Quelle zu verbessern. http://django-best-practices.readthedocs.io/en/latest/code.html –