I'm writing a program that enables my Django application to consume a REST API. My problem is that Django rejects the program if I run it as a .py
file, but accepts it via shell. The program is divided into two files: one that interacts with the API, and another that interacts with Django. The main folder structure is as follows:
我正在编写一个程序,使我的Django应用程序能够使用REST API。我的问题是Django拒绝该程序,如果我将其作为.py文件运行,但通过shell接受它。该程序分为两个文件:一个与API交互,另一个与Django交互。主文件夹结构如下:
/root folder
[rest of Django project]
-__init__.py
-test.py
-mid_side.py
-teamup_api/
-__init__.py
-teamup.py
where test.py
is the file that starts the program, mid_side.py
is the Django-side part, and teamup.py
is the API-side part. Below is the specific code I'm having trouble with.
其中test.py是启动程序的文件,mid_side.py是Django端部分,teamup.py是API端部分。以下是我遇到问题的具体代码。
test.py
import django
from mid_side import dates
from datetime import datetime
start = datetime(2017,7,28)
end = datetime(2017,8,19)
test = dates(start,end)
print(test)
Where dates
is a method from mid_side
that accepts two datetime
objects as parameters.
其中date是mid_side中接受两个日期时间对象作为参数的方法。
According to Django, the problem emerges in mid_side.py
, as it imports models from an app in the main Django project. The specific line Django has issues with is from events.models import Service
, where events
is an app Django uses and Services
is a model. The traceback is
根据Django的说法,问题出现在mid_side.py中,因为它从主Django项目中的应用程序导入模型。 Django遇到的具体问题是来自events.models import Service,其中事件是Django使用的应用程序,而Services是模型。追溯是
Traceback (most recent call last):
File "test.py", line 2, in <module>
from mid_side import dates
File "/home/brian/Github/nydkc11/nydkcd11/mid_side.py", line 3, in <module>
from events.models import Service
File "/home/brian/Github/nydkc11/nydkcd11/events/models.py", line 1, in <module>
from embed_video.fields import EmbedVideoField
File "/home/brian/anaconda3/envs/server/lib/python3.6/site-packages/embed_video/fields.py", line 6, in <module>
from .backends import detect_backend, UnknownIdException, \
File "/home/brian/anaconda3/envs/server/lib/python3.6/site-packages/embed_video/backends.py", line 17, in <module>
from .settings import EMBED_VIDEO_BACKENDS, EMBED_VIDEO_TIMEOUT, \
File "/home/brian/anaconda3/envs/server/lib/python3.6/site-packages/embed_video/settings.py", line 7, in <module>
'embed_video.backends.SoundCloudBackend',
File "/home/brian/anaconda3/envs/server/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/brian/anaconda3/envs/server/lib/python3.6/site-packages/django/conf/__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting EMBED_VIDEO_BACKENDS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I know for a fact the traceback concerns an app my project uses, django-embed-video. What I don't understand is why Django is indicating it as a problem considering a) Service
doesn't use fields from django-embed-video
, b) I successfully deployed the application on my project earlier. My questions are thus as follows:
我知道跟踪涉及我的项目使用的应用程序,django-embed-video。我不明白为什么Django表示这是一个问题考虑到a)服务不使用来自django-embed-video的字段,b)我之前成功地在我的项目上部署了应用程序。我的问题如下:
1) Why does this problem occur in the .py
file and not in shell?
1)为什么在.py文件中而不是在shell中出现此问题?
2) What is causing Django to output the traceback?
2)是什么导致Django输出回溯?
Below is relevant code from the program. Please notify me if additional clarifications are required. Thank you!
以下是该计划的相关代码。如果需要进一步说明,请通知我。谢谢!
*I decided not to include teamup.py
as this is more of a Django-side problem, but I will include it if requested.
*我决定不包括teamup.py,因为这更像是一个Django方面的问题,但如果请求,我会包含它。
mid_side.py
from dateutil.parser import parse
from events.models import Service
import datetime
from teamup_api.teamup import *
def dates(start,end):
event_list = day_query(start.year, start.month, start.day, end.year, end.month, end.day)['events']
service_objects = []
for event in event_list:
service_objects.append(Service(
title=event['title'],
school=none_parse(event['who']),
location=none_parse(event['location']),
start_time=parse(event['start_dt']),
end_time=parse(event['end_dt']),
all_day=event['all_day'],
description=none_parse(event['notes'])
))
return service_objects
models.py
class Service(models.Model):
title = models.CharField(max_length=100)
school = models.CharField(max_length=100)
location = models.CharField(max_length=100)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
all_day = models.BooleanField()
description = models.TextField()
def __str__(self):
return self.title
UPDATE: I've been running 'shell' through manage.py shell
, and 'console' via natively running a python file (e.g. python [file].py
)
更新:我一直在通过manage.py shell运行'shell',并通过本地运行python文件来运行'console'(例如python [file] .py)
1 个解决方案
#1
0
My problem was probably that I was using Django resources in the native python
environment. As Chris indicates in the comments, creating a custom manage.py
command instead of a .py
start file should suffice.
我的问题可能是我在本机python环境中使用Django资源。正如Chris在评论中指出的那样,创建自定义manage.py命令而不是.py启动文件就足够了。
#1
0
My problem was probably that I was using Django resources in the native python
environment. As Chris indicates in the comments, creating a custom manage.py
command instead of a .py
start file should suffice.
我的问题可能是我在本机python环境中使用Django资源。正如Chris在评论中指出的那样,创建自定义manage.py命令而不是.py启动文件就足够了。