我有一个问题,似乎在我一直在处理的整个网络应用程序中随机弹出。
我的应用程序将正常移动,然后,当从一个视图移动到另一个视图时,页面将开始加载,但随后继续无限期加载。我可以回去刷新,然后继续通过相同的视图,就像完全没有问题一样。当发生这种情况时,我视图中的其余代码仍然可以正常运行,但页面只是继续加载而不会产生任何类型的Http响应。
就像我说的那样,我试图删除部分项目以找出问题所在。问题开始发生在我将数据库迁移到使用South的时候,尽管我真的不确定这两者是否会交织在一起。即使你知道可能导致问题的原因,请回复我会非常感谢任何帮助,甚至是大方向。
urls.py
from django.conf.urls import patterns, url
from BucketList import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^stats/$', views.index_stats, name='index stats'),
url(r'^item/(?P<id>\w+)/comment/', views.add_item_comment, name = 'add item comments'),
url(r'^item/(?P<id>\w+)/', views.index_items, name = 'index items'),
url(r'^userstats/(?P<id>\w+)/', views.user_stats, name='user profile'),
url(r'^create/$', views.create, name='create'),
url(r'^create/(?P<id>\w+)/', views.create_specific_item, name = 'create specific item'),
url(r'^mylist/$', views.my_list, name='mylist'),
url(r'^mylist/stats/$', views.my_list_stats, name='mylist stats'),
url(r'^mylist/edit/(?P<id>\w+)/', views.edit_bucket_list_item, name = 'edit my list item'),
url(r'^mylist/view/(?P<id>\w+)/', views.view_my_list_item, name = 'view my list item'),
url(r'^mylist/uncross/(?P<id>\w+)/', views.uncross_my_list_item, name = 'uncross my list item'),
url(r'^mylist/crossoff/(?P<id>\w+)/', views.cross_off_my_list_item, name = 'cross off'),
url(r'^mylist/deleteitem/(?P<id>\w+)/', views.delete_my_list_item, name = 'delete list item'),
url(r'^mylist/recommendation/$', views.recommendation, name = 'recommendation'),
url(r'^profile/edit/$', views.edit_profile, name = 'edit profile'),
)
settings.py
"""
Django settings for Bucket project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'BucketList',
'south',
)
MIDDLEWARE_CLASSES = (
'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',
)
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
ROOT_URLCONF = 'Bucket.urls'
WSGI_APPLICATION = 'Bucket.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
LOGIN_URL = '/accounts/login/'
views.py
from django.shortcuts import render
from django.http import HttpResponse
from BucketList.models import BucketListItem, UserProfile, Comment
from django.contrib import auth
from forms import BucketListItemForm, UserProfileForm, UserProfileEditForm, BucketListItemEditForm, CustomItemEditForm, CommentForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.contrib.auth.models import User
from django.db.models import Sum
from django.contrib.auth.forms import UserCreationForm
from django.utils import timezone
from django.contrib.auth.decorators import login_required
def index(request):
#The main Bucket List Page View, sorted by pubdate so the most recent are at the top
all_list_items = BucketListItem.objects.all().order_by('-pub_date')
context = {'all_list_items': all_list_items}
return render(request, 'BucketList/index.html', context)
@login_required
def index_items(request, id):
#When a user clicks on a Bucket List Item on the index page it will take them here with a brief overview of that items information
item = BucketListItem.objects.get(pk = id)
current_user = UserProfile.objects.get(pk = request.user.id)
comments = Comment.objects.filter(item = item)
if request.POST:
form = CommentForm(request.POST)
if form.is_valid():
body = form.cleaned_data['body']
my_model = form.save(commit = False)
my_model.created = timezone.now()
my_model.author = current_user.user
my_model.item = item
my_model.body = body
my_model.save()
form = CommentForm()
else:
form = CommentForm()
context = {'item': item,
'id': id,
'comments': comments,
'form': form,
}
context.update(csrf(request))
return render(request, 'BucketList/index_items.html', context)
@login_required
def user_stats(request, id):
#A public stats/profile page that displays their basic profile information as well as their bucket list
item = User.objects.all().filter(username = id)
item_profile = item[0].userprofile
list_of_all = BucketListItem.objects.all().filter(pub_by = item)
context = {'id': id,
'item': item[0],
'list_of_all': list_of_all,
'item_profile': item_profile,
}
return render(request, 'BucketList/user_stats.html', context)
@login_required
def index_stats(request):
#This page compiles interesting statistics about all of the Bucket List Items on the main index page and displays that information
list_of_all = BucketListItem.objects.all().count()
total_cost = BucketListItem.objects.all().aggregate(Sum('cost'))
total_time = BucketListItem.objects.all().aggregate(Sum('time'))
total_crossed_off = BucketListItem.objects.all().aggregate(Sum('crossed_off'))
number_of_users = User.objects.all().count()
cost_per_user = total_cost['cost__sum']/number_of_users
time_per_user = total_time['time__sum']/number_of_users
items_per_user = list_of_all/number_of_users
crossed_off_per_user = total_crossed_off['crossed_off__sum']/number_of_users
context = {'list_of_all': list_of_all,
'total_cost': total_cost['cost__sum'],
'total_time': total_time['time__sum'],
'total_crossed_off': total_crossed_off['crossed_off__sum'],
'crossed_off_per_user': crossed_off_per_user,
'number_of_users': number_of_users,
'cost_per_user': cost_per_user,
'time_per_user': time_per_user,
'items_per_user': items_per_user,
}
return render(request, 'BucketList/index_stats.html', context)
@login_required
def my_list(request):
#The current users personal Bucket List view with links to create more list items or learn statistics about their list
personal_list = BucketListItem.objects.all().filter(pub_by = request.user.id)
context = {'personal_list': personal_list,
'user': request.user.username
}
return render(request, 'BucketList/mylist.html', context)
@login_required
def my_list_stats(request):
#General statistics about the current users Bucket List
personal_list = BucketListItem.objects.all().filter(pub_by = request.user.id)
total_cost = BucketListItem.objects.all().filter(pub_by = request.user.id).aggregate(Sum('cost'))
total_time = BucketListItem.objects.all().filter(pub_by = request.user.id).aggregate(Sum('time'))
total_crossed_off = BucketListItem.objects.all().filter(pub_by = request.user.id).aggregate(Sum('crossed_off'))
cost_per_list_item = total_cost['cost__sum']/personal_list.count()
time_per_list_item = total_time['time__sum']/personal_list.count()
context = {'personal_list': personal_list,
'user': request.user.username,
'total_cost': total_cost['cost__sum'],
'total_time': total_time['time__sum'],
'total_crossed_off': total_crossed_off['crossed_off__sum'],
'cost_per_list_item': cost_per_list_item,
'time_per_list_item': time_per_list_item,
}
return render(request, 'BucketList/my_list_stats.html', context)
@login_required
def view_my_list_item(request, id):
#View of a current users Bucket List Item with options to cross off or edit the Bucket List Item
logged_in = request.user.id
item = BucketListItem.objects.filter(pk = id)
context = {'logged_in': logged_in,
'item': item[0],
}
return render(request, 'BucketList/view_my_list_item.html', context)
@login_required
def cross_off_my_list_item(request, id):
#The view that crosses off the Bucket List Item
item = BucketListItem.objects.get(pk = id)
item.crossed_off = True
item.pub_date = timezone.now()
item.save()
context = {'item': item,
'User': User,
}
return render(request, 'BucketList/crossed_off.html', context)
@login_required
def uncross_my_list_item(request, id):
#The view that uncrosses off the Bucket List Item
item = BucketListItem.objects.get(pk = id)
item.crossed_off = False
item.pub_date = timezone.now()
item.save()
context = {'item': item,
'User': User,
}
return render(request, 'BucketList/uncross.html', context)
@login_required
def delete_my_list_item(request, id):
#The view that uncrosses off the Bucket List Item
item = BucketListItem.objects.get(pk = id)
title = item.text
item.delete()
context = {'title': title,
'User': User,
}
return render(request, 'BucketList/delete.html', context)
@login_required
def create(request):
#Creates a Bucket List Item, the user only fills out the Name and Type of the item while the rest of the fields are auto-filled: publication date, published by, crossed off, time, hours, and cost
if request.POST:
form = BucketListItemForm(request.POST)
if form.is_valid():
user = User.objects.get(id=request.user.id)
my_model = form.save(commit = False)
new_cost = form.cleaned_data['cost']
new_time = form.cleaned_data['time']
new_hours = form.cleaned_data['hours']
my_model.pub_by = user
my_model.crossed_off = False
my_model.time = new_time
my_model.hours = new_hours
my_model.cost = new_cost
my_model.save()
return HttpResponseRedirect('/bucketlist/mylist/')
else:
form = BucketListItemForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render(request, 'BucketList/create_item.html', args)
@login_required
def edit_bucket_list_item(request, id):
#This view lets the user edit their Bucket List Item and directs them to other forms necessary to make the changes needed
item = BucketListItem.objects.get(pk = id)
if request.method == "POST":
form = BucketListItemEditForm(request.POST)
if form.is_valid():
text = form.cleaned_data['text']
goal_type = form.cleaned_data['goal_type']
cost = form.cleaned_data['cost']
time = form.cleaned_data['time']
hours = form.cleaned_data['hours']
item.text = text
item.cost = cost
item.time = time
item.hours = hours
item.goal_type = goal_type
item.pub_date = timezone.now()
item.save()
return HttpResponseRedirect('/bucketlist/mylist/')
else:
form = BucketListItemEditForm({'text': item.text, 'goal_type': item.goal_type, 'cost': item.cost, 'time': item.time, 'hours': item.hours})
context = {'form': form,
'id': item.id,
}
context.update(csrf(request))
return render(request, 'BucketList/edit_bucket_list_item.html', context)
@login_required
def edit_profile(request):
#A view that allows the user to edit their current profile information
current_user = UserProfile.objects.get(pk = request.user.id)
if request.method == "POST":
form = UserProfileEditForm(request.POST)
if form.is_valid():
new_age = form.cleaned_data['new_age']
new_life_expectancy = form.cleaned_data['new_life_expectancy']
new_yearly_earnings = form.cleaned_data['new_yearly_earnings']
new_hourly_wage = form.cleaned_data['new_hourly_wage']
current_user.yearly_earnings = new_yearly_earnings
current_user.hourly_wage = new_hourly_wage
current_user.life_expectancy = new_life_expectancy
current_user.age = new_age
current_user.save()
return HttpResponseRedirect('/bucketlist/mylist/')
else:
form = UserProfileEditForm({'new_age': current_user.age, 'new_life_expectancy': current_user.life_expectancy, 'new_yearly_earnings': current_user.yearly_earnings, 'new_hourly_wage': current_user.hourly_wage})
context = {'form': form,
}
context.update(csrf(request))
return render(request, 'BucketList/edit_user_profile.html', form)
答案 0 :(得分:1)
查看您的观点,您似乎在请求中调用csrf()
,然后使用render
发送回复。渲染的重点是automatically enforces all context processors,因此您不必手动调用csrf()
。
即使它通常不重要,但根据我的经验,我注意到对csrf
的重复调用有时会在一些请求后导致奇怪和意外的行为。
正如我们在评论中所讨论的,在应用此更改后(在其他几个中),问题停止发生。我的主要嫌疑人是csrf()
电话,但很容易就是你的django安装损坏(下次,use a virtualenv)或其他一些与django无关的本地问题。