0
我对Django的观点如何工作有点困惑。我想它的工作是这样的:Django Views - 它是如何工作的?
- 用户按下一个HTML页面上的一个按钮
- 动作被链接到一个视图,如此这般的功能在url.py定义,然后它一些逻辑。
不过下面我有一个index.html页面,将用户重定向到登录页面,如果用户没有登录:
def index(request):
if not request.user.is_authenticated():
return redirect('/login.html')
else:
result = Hello_World.delay()
somethingDownByCelery = result.get(timeout=2)
context = {'somethingDownByCelery': somethingDownByCelery, 'userName': request.user.username}
return render(request, 'webapp/index.html', context)
然后,我有一个login.html的,我有一个记录器记录用户在每个网页上的行为。
def loginUser(request):
logger = logging.getLogger('views.logger.login')
try:
username = request.POST['username'];
logger.info("User:" + username + " in Login Page");
except:
logger.error("Cannot Identify User");
type = ""
try:
type = request.POST['submit']
logger.info("User:" + username + " requests:" + type);
except MultiValueDictKeyError:
logger.error("Cannot Identify User's Request");
try:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('index.html')
else:
return redirect('disabled.html')
else:
condition = "Invalid Login"
context = {'condition': condition}
return render(request, 'webapp/login.html', context)
except MultiValueDictKeyError:
context = None
return render(request, 'webapp/login.html', context)
的问题是,网页刷新,或重定向到的时候,它会得到当我试图请求POST使用用户名,并提交两个例外2个logger.error,因为我认为行为是1(按下网页中的按钮),然后是2(在视图中运行该功能)。
然而,不知怎的,它首先经历了整个功能,然后生成一个网页,这是一个3步骤的过程?
“越来越logger.error”?那有什么意思?你在这段代码中有多个'error()'调用,你知道... – 2014-09-02 21:17:42
对不起,补充说明。它给我两个错误,分别是“无法识别用户”和“无法识别用户的请求”。 – user1157751 2014-09-02 21:20:16
...所以你打两个除了:子句。这不应该是一个惊喜,因为这个重定向不会发布到/ login。 – 2014-09-02 21:22:08