我正在尝试使用用户输入表单和显示先前输入数据的区域来创建视图。表单正确显示,但不是数据库中的数据。我不确定它是否首先保存,如果没有正确查询,没有正确传递到上下文,或者没有显示在模板中。
models.py
class Channels(models.Model):
name = models.CharField(max_length=100, unique=True)
total_views = models.IntegerField(null=True)
subscribers = models.IntegerField()
def __unicode__(self):
return self.title
forms.py
class ChannelNameForm(forms.Form):
channel_name = forms.CharField(label='Add Channel', max_length=500)
views.py
def add_channels(request):
queryset = Channels.objects.all()
print queryset
if request.method == 'POST':
form = ChannelNameForm(request.POST)
if form.is_valid():
try:
name = name_config(form.cleaned_data['channel_name'])
yturl = get_id_url(name)
sub_count = get_sub_count(yturl)
c = Channels(name=name, subscribers = sub_count)
c.save()
except:
error = "No results were found for YouTube user %s." % name
else:
form = ChannelNameForm()
return render(request, 'channel_tracker/addchannel.html', {'form': form,'queryset' : queryset})
addchannel.html
<form action="." method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
<form method='get'>
{% for item in queryset %}
{{ item.name }}
{{ itm.subscribers}}
{% endfor %}
</form>
答案 0 :(得分:0)
我认为你必须指定动作网址,你可以把:
<form action="{% url 'add_channels' %}" method="post">
答案 1 :(得分:0)
问题似乎是我试图将一个str插入到IntegerField中,并且由于愚蠢的尝试而没有引发错误:except:statements。