如何解决StaticLiveServerTestCase 服务器错误 500
主 urls.py 文件:
urlpatterns = [
path(
'admin/',admin.site.urls
),path(
'',include(
'employee.urls'
)
),include(
'epos.urls'
)
),include(
'supplier.urls'
)
),]
epos.urls:
urlpatterns = [
path(
'',home_view,name='home'
),
home_view:
@login_required
def home_view(request):
# Get all categories
categories = Category.objects.all()
# Get the first category which will be selected by default
selected_category = categories.first()
# Get the order_items for the selected category (first category)
products = Product.objects.filter(
category=selected_category.id
)
user = request.user
tax = Tax.objects.latest('id')
context = {
'categories': categories,'user': user,'title': "EPOS",'products': products,'selected_category': selected_category,'tax': tax
}
return render(request,'epos/epos.html',context)
测试用例:
class LoginTests(StaticLiveServerTestCase):
fixtures = ['fixtures/employee/employee_data.json']
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.get(self.live_server_url)
self.browser.maximize_window()
def tearDown(self):
self.browser.quit()
def test_login_when_clocked_in(self):
import ipdb;ipdb.set_trace()
login_button = self.browser.find_element_by_xpath(
'//button[normalize-space()="Login"]'
)
clock_in_out_button = self.browser.find_element_by_xpath(
'//button[normalize-space()="Clock In/Out"]'
)
pin_input = self.browser.find_element_by_id(
'pin'
)
pin_code = 'some pin'
employee_name = 'some name'
pin_input.send_keys(pin_code)
clock_in_out_button.click()
time.sleep(10)
login_button.click()
settings.py
ROOT_URLconf = 'allPOS.urls'
LOGIN_URL = '/login/'
当测试登录时,我被重定向到 localhost:46497/
的主网页,但我收到的是服务器错误 500,而不是该页面。
此错误仅在测试时发生。此外,如果我添加另一个路径,例如localhost:46497/analytics
它按预期打开网页。
任何帮助将不胜感激。
解决方法
经过 4 个小时的调试,我发现问题与数据库为空有关。
我想要渲染的网页期望通过一些模型,由于数据库为空,这些模型没有通过,因此崩溃。在正常情况下( ./manage.py runserver
)如果 DEBUG=True
它会告诉你什么是错的,但由于某种原因 StaticLiveServerTestCase
没有这个选项,或者至少我不知道。如果有人知道如何为 StaticLiveServerTestCase
启用一些调试器,请随时添加到此线程。
所以我做了什么:
./manage.py dumpdata order.tax > data.json
- 我将需要的数据转储到 json 文件中,并在测试用例的开头将该数据添加到 fixture
希望这对有同样问题的人有所帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。