【发布时间】:2014-09-15 19:27:33
【问题描述】:
我有一个问题似乎在我正在开发的整个网络应用程序中随机弹出。
我的应用程序会正常运行,然后,当从一个视图移动到另一个视图时,页面将开始加载,然后无限期地继续加载。我可以回去刷新,然后继续通过相同的视图,就像完全没有问题一样。当这种情况发生时,我认为其余代码仍然可以正常运行,但页面只是继续加载而不会产生任何类型的 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)
【问题讨论】:
-
“它有时不起作用,但我不知道为什么”毫无意义。如果没有至少 一些 参考点来说明您在说什么,我们将无法帮助您解决问题。请缩小问题范围,尝试找出发生了什么以及代码的哪一部分导致了它。如果您打开 chrome 控制台,您会看到错误吗?如果是某种观点,你能分享它的代码吗?如果您正在使用与众不同的东西(也许是自定义中间件)分享它。什么 django 版本,urls.py 是什么样的 - 你给我们的越多越好(在一定程度上,但仍然)
-
首先我想说谢谢您的回复。对于我的问题过于宽泛,我深表歉意。当发生冻结问题时,chrome 控制台中将看不到任何内容。没有一种特定的观点导致了这个问题。它发生在每个视图中,但不是以任何一致的间隔发生。有时我可以在我的应用程序中浏览 20 个页面,而其他时候它会在同一个视图中重复发生。没有任何错误消息,只是一个空白的加载 chrome 控制台。我没有使用任何自定义中间件。
-
快速跟进:1.您使用的是什么版本的 django 2.您确定这不是浏览器问题吗?您是否尝试过使用其他的?还是不同的电脑? 3.你能不能也分享一下你的
settings.py(记得省略个人信息——密码、密钥等...) -
我使用的是 django 1.6 版。我也尝试过使用 Firefox 和 Internet Explorer。这两个问题都会出现,尽管它在 Firefox 上确实发生得更多,在 IE 上更常见。其他网页似乎没有任何问题。我的互联网设置有问题吗?我还没有尝试过其他计算机。
-
1.铬的行为如何? 2.什么版本的IE? (这很重要) 3. 你试过不同的电脑吗?
标签: python django httpresponse django-south freeze