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