我无法弄清楚这里发生了什么。我想检索一个对象,然后将用户添加到myobject.followers。但我无法让它发挥作用。我是Django的新手。这里发生了什么?
models.py
class SubNetwork(models.Model):
sub_name = models.CharField(max_length=40)
followers = models.ManyToManyField(User)
def __str__(self):
return self.sub_name
def number_of_followers(self):
self.number = self.followers.count()
return self.number
def number_of_posts(self):
self.total_posts = self.fishingreport_set.count()
return self.total_posts
views.py
def sub(request, sub_name):
subnetwork = SubNetwork.objects.get(sub_name=sub_name)
fishing_report = FishingReport.objects.filter(sub_network=subnetwork.id).order_by('-pub_date')
paginator = Paginator(fishing_report, 10)
page = request.GET.get('page')
try:
reports = paginator.page(page)
except PageNotAnInteger:
reports = paginator.page(1)
except EmptyPage:
reports = paginator.page(paginator.num_pages)
return render(request, 'subnetwork/sub.html', {'subnetwork': subnetwork,
'reports': reports})
def follow(request, pk):
if request.method == 'POST':
report = SubNetwork.objects.get(id=pk)
return redirect('sub/subs/')
urls.py
urlpatterns = [
url(r'^subs/', views.all_subs, name="all_subs"),
url(r'^(?P<sub_name>\w+)', views.sub, name='sub_name'),
url(r'^follow/(?P<pk>[0-9]+)', views.follow, name='follow'),
]
HTML
<form method="POST" action="{% url 'subnetworks:follow' sub.id %}">
{% csrf_token %}
<input type="submit" value="UP">
</form>
错误 环境:
请求方法:POST
Request URL: http://127.0.0.1:8000/sub/follow/1
Django Version: 1.10.5
Python Version: 3.6.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'TheFishNetwork',
'accounts',
'subnetwork',
'posts',
'markdownify']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/swapnil/python_envs/fishenv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/swapnil/python_envs/fishenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/swapnil/python_envs/fishenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/swapnil/PycharmProjects/fishnetwork/subnetwork/views.py" in sub
13. subnetwork = SubNetwork.objects.get(sub_name=sub_name)
File "/home/swapnil/python_envs/fishenv/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/swapnil/python_envs/fishenv/lib/python3.6/site-packages/django/db/models/query.py" in get
385. self.model._meta.object_name
Exception Type: DoesNotExist at /sub/follow/1
Exception Value: SubNetwork matching query does not exist.
main urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
url(r'^reports/', include('posts.urls')),
url(r'^sub/', include('subnetwork.urls'),),
url(r'^^(?P<pk>[0-9]+)/$$', home_page, name='user_front'),
]