我正在使用我的第一个Django项目,现在我有一些我根本不懂的错误!昨天我创建了两个表单:其中一个表单正在工作,第二个表单中的数据没有保存到数据库中。我试图解决这个问题,但现在他们都没有工作,我甚至与我的一个旧模板有同样的问题。你能告诉我我的错误是什么以及如何解决它们吗?
这是我的views.py:
的一部分def listing(request):
users_list = Person.objects.all()
paginator = Paginator(users_list, 14)
page = request.GET.get('page')
try:
users = paginator.page(page)
except PageNotAnInteger:
users = paginator.page(1)
except EmptyPage:
users = paginator.page(paginator.num_pages)
return render(request, 'friends_plans/list.html', {"users": users})
def add_wish_list(request):
if request.method == 'POST':
form = Wish_listForm(request.POST)
if form.is_valid():
newform = form.save(commit=False)
newform.person = request.user
newform.save()
else:
print form.errors
else:
form = Wish_listForm()
return render(request, 'friends_plans/add_wish_list.html', {'form': form})
def add_comment(request, wish_list_id):
person = request.user
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():
newform=form.save(commit=False)
newform.person = request.user
newform.comment_to = wish_list
wish_list.person = person
newform.save()
else:
print form.errors
else:
form = Comment_to_wish_listForm()
context_dict = {'form': form, 'wish_list': wish_list}
return render(request, 'friends_plans/add_comment.html', {'context_dict': context_dict})
这是我的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]+)/comment/add/$', views.add_comment, name='add_comment'),
]
这是list.html:
{% extends 'friends_plans/base.html' %}
{% load staticfiles %}
{% block title %} Users {% endblock %}
{% block content %}
<div id ="left">
<div id="toptitle"> Friends' Plans members:</div>
<table class="table">
<thead>
<tr>
<th>Photo</th>
<th>Name</th>
<th>Occupation</th>
<th>Days</th>
<th>Places</th>
</tr>
</thead>
<tbody>
{% for person in users %}
<tr>
<td><span> <img class="small_cat" src={% static 'images/cat3.jpg' %} /> </span></td>
<td><a href="{% url 'friends_plans:user' user.pk %}">{{ person.username|upper }}</a></span></td>
<td><span>Student at {{ person.place_of_work_or_study}}</span></td>
<td>{{person.day_set.all.count}}</td>
<td>{{person.wish_list_set.all.count}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="pagination">
<div id="listing">
<span class="step-links">
{% if users.has_previous %}
<a href="?page={{ users.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ users.number }} of {{ users.paginator.num_pages }}.
</span>
{% if users.has_next %}
<a href="?page={{ users.next_page_number }}">next</a>
{% endif %}
</span>
</div>
</div>
</div>
{% endblock %}
这是add_wish_list.html:
{% extends 'friends_plans/base.html' %}
{% load staticfiles %}
{% block content %}
<h1>Add a wish_list</h1>
<form method="post" action="/friends_plans:add_wish_list/">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Add wish_list" />
</form>
{% endblock %}
这是add_comment.html:
{% 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 %}
我理解这个问题与网址有关,但我不明白它是什么,以及为什么一切都很好用list.html之前我没有改变它!
答案 0 :(得分:2)
在list.html中,您有def _query_data(self, sql_query: str, retries: int = 5):
cursor = self._return_cursor(self.canvas_conn)
for attempt in range(retries):
try:
cursor.execute(sql_query)
except (ppg2.ProgrammingError, ppg2.OperationalError) as e:
if 'Operation timed out' in str(e):
print('Retrying, attempt {}'.format(attempt))
sleep(3)
continue
elif self.historical_connect:
msg = 'Connected on last run, no longer available.'
self.set_for_alert(msg)
self.historical_connect = False
raise ppg2.Error from e
else:
return self._iterate_results(cursor)
,但您的变量名为{% url 'friends_plans:user' user.pk %}
,而不是person
。