本教學課程將探討如何使用 Bing Ads Python SDK、Visual Studio Code IDE 和 Django Web 架構,開始執行 Microsoft Advertising Web 應用程式。
本教學不探討 Django 本身的各種細節,例如使用資料模型和建立管理介面。 有關這些方面的指導,請參閱 Django 文件。 如需有關如何在 VS Code 終端機、編輯器和偵錯工具中使用 Django 的詳細資訊,請參閱在 Visual Studio Code 中使用 Django。 本教學課程大量借鑒了在 Visual Studio Code 中使用 Django 中的設定說明。
範例 Web 應用程式概觀
完成本教學課程後,您將擁有正在執行 http://localhost 的 Web 應用程式,以驗證您的 Microsoft Advertising 使用者認證,並顯示您的使用者和帳戶資訊。 然後,您可以新增多個 Web 應用程式使用者,這些使用者可以為您的應用程式啟用存取權以使用其 Microsoft Advertising 認證。 此 Web 應用程式提供 Web 應用程式使用者的一對一對應,例如 ContosoUser 與 Microsoft Advertising 使用者。 有關如何修改資料模型的資訊,請參閱 Django 文件以 取得更多資訊。 如果您的網頁應用程式使用者使用 Microsoft 帳戶來啟用其 Microsoft Advertising 帳戶的存取權,則重新整理權杖會儲存在網頁伺服器上的 SQL Lite 資料庫中。
必要條件
您必須安裝 Visual Studio Code 才能遵循本教學課程。 若要執行 Django Web 應用程式,您可以使用 Visual Studio Community 或 Visual Studio Professional;不過,設定步驟會與本教學課程中的步驟有所不同。
您必須從 python.org 安裝 Python 3;通常會使用頁面上第一個出現的 [ 下載 Python 3.7.0 ] 按鈕, (或任何最新版本) 。 在 Windows 中,請確定 Python 解釋器的位置包含在 PATH 環境變數中。 您可以在命令提示字元執行來 path 檢查。 如果不包含 Python 解釋器的資料夾,請開啟 Windows [設定],搜尋「環境」,選取您 帳戶的 [編輯環境變數],然後編輯 Path 變數以包含該資料夾。
您必須 安裝Bing Ads Python SDK,本教學課程將逐步引導您完成安裝。
您需要安裝 Django Web 框架 才能在本地部署應用程序,本教程將引導您完成安裝。
您至少需要一名具有 Microsoft Advertising 認證和 開發人員權杖的使用者。
您必須註冊應用程式,並記下用戶端識別碼 (已註冊的應用程式識別碼) 和用戶端密碼 (登錄的密碼) 。 在此範例中,您需要註冊 Web 應用程式 (非原生) 。 系統會要求您註冊一個或多個重新導向 URL,對於本教學課程,您應該註冊 http://localhost/callback。 當部署到生產伺服器時,您應該改為使用 https 。 如需註冊應用程式和授權碼授與流程的詳細資訊,請參閱 使用 OAuth 驗證。
本教學課程是在 Windows 上開發。 雖然執行範例不需要 Windows,但如果您使用其他作業系統,例如 Linux 或 MacOS,以下某些步驟會有所不同。
建立 Django 的專案環境
在本節中,您將建立安裝 Django 的虛擬環境。 使用虛擬環境可以避免將 Django 安裝到全域 Python 環境中,並讓您精確控制應用程式中使用的函式庫。
在您的檔案系統上,建立本教學課程的專案資料夾,例如
hello_django。在資料夾中
hello_django,開啟 Powershell 或您最愛的指令碼殼層,並使用下列命令建立根據您目前的口譯器命名env的虛擬環境:py -3 -m venv env在 VS Code 中開啟專案資料夾,
hello_django方法是執行code .,或執行 VS Code 並使用 檔案>開啟資料夾 命令。
在 VS Code 中,開啟命令面板 (檢視>命令面板或
Ctrl+Shift+P) 。 然後選取 Python: 選取解譯器 命令。此命令會顯示 VS Code 可以自動找到的可用解釋器清單。 您的清單會有所不同;如果您沒有看到所需的解釋器,請參閱 設定 Python 環境。 從清單中,選取專案資料夾中以 或 開
.\env頭./env的虛擬環境:
執行 終端機:從 命令板 (
Ctrl+Shift+ `) 的新終端機,它會建立終端機,並透過執行其啟動指令碼來自動啟動虛擬環境。注意事項
在 Windows 上,如果您的預設終端機類型為 PowerShell,您可能會看到它無法執行 activate.ps1 錯誤,因為系統上已停用執行指令碼。 該錯誤應該提供有關如何允許指令碼的連結。 否則,請使用 終端機: 選取 [預設殼層 ] 以設定您偏好的預設值。
選取的環境會顯示在 VS Code 狀態列的左下角。 請注意 (venv) 指示器,它告訴您正在使用虛擬環境:
在虛擬環境中透過 VS Code 終端中的 pip 安裝 Django:
python -m pip install django透過 VS Code 終端機中的 pip 在虛擬環境中安裝 Bing Ads Python SDK:
python -m pip install bingads
您現在擁有獨立的虛擬環境,可撰寫 Django 和 Microsoft Advertising 程式碼。
建立及執行 Django 應用程式
在 Django 術語中,「Django 專案」由數個網站層級設定檔以及部署到 Web 主機的一個或多個「應用程式」組成,以建立完整的 Web 應用程式。 一個 Django 專案可以包含多個應用程式,每個應用程式通常在專案中具有獨立的功能,並且相同的應用程式可以存在於多個 Django 專案中。 就應用程式而言,它只是一個遵循 Django 期望的某些約定的 Python 包。
因此,要建立 Django 應用程式,必須先建立 Django 專案作為應用程式的容器,然後建立應用程式本身。 出於這兩個目的,您可以使用 Django 管理實用程序, django-admin該實用程序是在您安裝 Django 軟件包時安裝的。
建立 Django 專案
在虛擬環境已啟用的 VS Code 終端機中,執行下列命令:
django-admin startproject web_project .此
startproject命令會假定 (藉由在結尾) 的 ,.目前的資料夾是您的專案資料夾,並在其中建立下列項目:manage.py:專案的 Django 命令列系統管理公用程式。 您可以使用 執行python manage.py <command> [options]專案的系統管理命令。名為
web_project的子資料夾,其中包含下列檔案:-
__init__.py:告訴 Python 此資料夾是 Python 套件的空白檔案。 -
wsgi.py:WSGI 相容網頁伺服器為您的專案提供服務的進入點。 您通常會讓此檔案保持原樣,因為它為生產 Web 伺服器提供了掛勾。 -
settings.py: 包含 Django 專案的設定,您可以在開發 Web 應用程式的過程中對其進行修改。 -
urls.py: 包含 Django 專案的目錄,您也會在開發過程中對其進行修改。
-
要驗證 Django 項目,請確保您的虛擬環境已激活,然後使用命令
python manage.py runserver啟動 Django 的開發服務器。 伺服器在預設連接埠 8000 上執行,您會在 VS Code 終端機輸出視窗中看到如下所示的輸出:Performing system checks... System check identified no issues (0 silenced). You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. October 18, 2018 - 05:38:23 Django version 2.1.2, using settings 'web_project.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.當您第一次運行伺服器時,它會在檔案
db.sqlite3中建立預設的 SQLite 資料庫,該資料庫通常用於開發目的,但可用於低容量 Web 應用程式的生產環境。 此外,Django 的內建 Web 伺服器 僅 用於本地開發目的。 但是,當您部署到 Web 主機時,Django 會改用主機的 Web 伺服器。wsgi.pyDjango 專案中的模組負責連接到生產伺服器。如果您想使用與預設 8000 不同的連接埠,只需在命令列上指定連接埠號碼即可,例如
python manage.py runserver 5000。Ctrl+clickhttp://127.0.0.1:8000/VS Code 終端機輸出視窗中的 URL,以開啟預設瀏覽器至該位址。 如果 Django 安裝正確且專案有效,您會看到如下所示的預設頁面。 [VS 程式碼輸出] 視窗也會顯示伺服器記錄檔。
完成後,請關閉瀏覽器視窗,並依照 VS Code 終端機輸出視窗中的指示方式停止
Ctrl+CVS Code 中的伺服器。
為 Microsoft Advertising 建立 Django 應用程式
在已啟動虛擬環境的 VS Code 終端機中,在專案資料夾 (所在的位置
manage.py執行管理公用程式的startapp命令,) :python manage.py startapp app此命令會建立一個名為
app資料夾的資料夾,其中包含許多程式碼檔和一個子資料夾。 其中,您經常使用的views.py(包含定義 Web 應用程式) 中頁面的函式,以及models.py包含定義資料物件) 類別的 (。 Django 的管理公用程式使用該migrations資料夾來管理資料庫版本,如本教學課程稍後所討論。 還有 (應用程式設定) 的檔案apps.py、admin.py用於建立系統管理介面) 的 (,以及tests.py用於單元測試) 的 (,此處未涵蓋這些。在其中
app/settings.py新增下列程式碼,並設定您自己的 CLIENT_ID、 CLIENT_SECRET、 DEVELOPER_TOKEN 和 ENVIRONMENT 值。""" Bing Ads API settings Edit with your credentials. """ REDIRECTION_URI = "http://localhost:8000/callback" CLIENT_ID = "ClientIdGoesHere" # Your registered App ID CLIENT_SECRET="ClientSecretGoesHere" # Your registered App Password DEVELOPER_TOKEN = "DeveloperTokenGoesHere" # Your production developer token ENVIRONMENT = 'production' API_VERSION=13在 [新增至
app已安裝應用程式清單] 中app/settings.py。INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app', ]在 VS Code 終端機中,
app/static/app建立 和app/templates/app資料夾:(env) PS C:\dev\hello_django> mkdir app/static/app (env) PS C:\dev\hello_django> mkdir app/templates/app在資料夾中
app/static/app,建立名為 site.css 的新檔案,並新增下列內容。.message { font-weight: 600; color: blue; } .message_list th,td { text-align: left; padding-right: 15px; } .navbar { background-color: lightslategray; font-size: 1em; font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; color: white; padding: 8px 5px 8px 5px; } .navbar a { text-decoration: none; color: inherit; } .navbar-brand { font-size: 1.2em; font-weight: 600; } .navbar-item { font-variant: small-caps; margin-left: 30px; } .body-content { padding: 5px; font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } input[name=message] { width: 80%; }在資料夾中
app/templates/app建立檔案,index.html其中包含下列內容。{% extends "app/layout.html" %} {% block content %} {% if errors %} <div class="jumbotron"> <section id="errors"> <h1>Errors occurred in your last request to Bing Ads API.</h1> <table class="message_list"> <tr> <th>Code</th> <th>ErrorCode</th> <th>Message</th> </tr> {% for error in errors %} <tr> <td>{{ error.Code }}</td> <td>{{ error.ErrorCode }}</td> <td>{{ error.Message }}</td> </tr> {% endfor %} </table> </section> </div> {% endif %} {% if user.is_authenticated %} {% if bingadsuser %} <div class="jumbotron"> <section id="enabled"> <h1>Your credentials have access to Microsoft Advertising.</h1> <table class="message_list"> <tr> <th>Id</th> <th>UserName</th> <th>First Name</th> <th>Last Name</th> </tr> <tr> <td>{{ bingadsuser.Id }}</td> <td>{{ bingadsuser.UserName }}</td> <td>{{ bingadsuser.Name.FirstName }}</td> <td>{{ bingadsuser.Name.LastName }}</td> </tr> </table> </section> </div> <div class="jumbotron"> <section id="revoke"> <p class="lead">Click here to revoke access for this app to your Microsoft Advertising accounts. You will then be able to login with a different Microsoft Advertising user. </p> <form id="revokeForm" action="/revoke" method="post" class="navbar-left"> {% csrf_token %} <p><a href="javascript:document.getElementById('revokeForm').submit()" class="btn btn-primary btn-large">Delete Refresh Token</a></p> </form> </section> </div> <div class="jumbotron"> <section id="accounts"> <h1>Account Details</h1> <table class="message_list"> <thead> <tr> <th>Id</th> <th>Name</th> </tr> </thead> <tbody> {% for account in accounts %} <tr> <td>{{ account.Id }}</td> <td>{{ account.Name }}</td> </tr> {% endfor %} </tbody> </table> </section> </div> {% else %} <div class="jumbotron"> <section id="enable"> <h1>Enable Microsoft Advertising Access</h1> <p class="lead"> You are logged into the Django web application, but not yet signed in with your Microsoft Advertising credentials. You can sign in with Microsoft Advertising credentials below. </p> </section> </div> <div> <div class="col-md-6"> <section id="socialLoginForm"> <h1>Microsoft Account Login</h1> <p class="lead"> Click here to authenticate your Microsoft Account. If you don't have Microsoft Advertising credentials, you can go to the <a href="https://ads.microsoft.com/customer/Signup.aspx">Microsoft Advertising Sign Up</a> page. </p> <p><a href="/callback" class="btn btn-primary btn-large">Authenticate Microsoft Account »</a></p> </section> </div> </div> {% endif %} {% else %} <div class="jumbotron"> <div class="col-md-6"> <section id="socialLoginForm"> <h1>Microsoft Advertising Example Web Application</h1> <p class="lead"> Before you can provide your Microsoft Advertising user credentials and access Microsoft Advertising data, you must <a href="{% url 'login' %}">login</a> to the Django web application. </p> <p class="lead">Use your site's Django admin portal to add web app users.</p> <p><a href="/admin" class="btn btn-primary btn-large">Django Admin »</a></p> </section> </div> </div> {% endif %} <div> <div class="col-md-4"> <h2>Get Started Using Python with Bing Ads API</h2> <p>The Bing Ads Python Software Development Kit (SDK) simplifies workflows such as OAuth authentication and report file parsing.</p> <p><a class="btn btn-default" href="https://learn.microsoft.com/advertising/guides/get-started-python">Learn more »</a></p> </div> <div class="col-md-4"> <h2>Django</h2> <p>Django is a free web framework for building Web sites and Web applications using HTML, CSS and JavaScript.</p> <p><a class="btn btn-default" href="https://www.djangoproject.com/">Learn more »</a></p> </div> <div class="col-md-4"> <h2>Microsoft Azure</h2> <p>You can publish your web app to Microsoft Azure. Find out how you can host your application with a free trial today.</p> <p><a class="btn btn-default" href="https://azure.microsoft.com">Learn more »</a></p> </div> </div> {% endblock %} {% block scripts %} {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'app/site.css' %}"/> {% endblock %}在資料夾中
app/templates/app建立檔案,layout.html其中包含下列內容。<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{ title }} - My Django Application</title> {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'app/site.css' %}"/> <script src="{% static 'app/scripts/modernizr-2.6.2.js' %}"></script> </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a href="/" class="navbar-brand">Microsoft Advertising App via Django</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a href="{% url 'home' %}">Home</a></li> </ul> {% include 'app/loginpartial.html' %} </div> </div> </div> <div class="container body-content"> {% block content %}{% endblock %} <hr/> <footer> <p>© {{ year }} - My Django Application</p> </footer> </div> {% block scripts %}{% endblock %} </body> </html>在資料夾中
app/templates/app建立檔案,login.html其中包含下列內容。{% extends "app/layout.html" %} {% block content %} <h2>{{ title }}</h2> <div class="row"> <div class="col-md-8"> <section id="loginForm"> <form action="." method="post" class="form-horizontal"> {% csrf_token %} <h4>Use a local account to log in.</h4> <hr /> <div class="form-group"> <label for="id_username" class="col-md-2 control-label">User name</label> <div class="col-md-10"> {{ form.username }} </div> </div> <div class="form-group"> <label for="id_password" class="col-md-2 control-label">Password</label> <div class="col-md-10"> {{ form.password }} </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="hidden" name="next" value="/" /> <input type="submit" value="Log in" class="btn btn-default" /> </div> </div> {% if form.errors %} <p class="validation-summary-errors">Please enter a correct user name and password.</p> {% endif %} </form> </section> </div> </div> {% endblock %} {% block scripts %} {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'app/site.css' %}"/> {% endblock %}在資料夾中
app/templates/app建立檔案,loginpartial.html其中包含下列內容。{% if user.is_authenticated %} <form id="logoutForm" action="/applogout" method="post" class="navbar-right"> {% csrf_token %} <ul class="nav navbar-nav navbar-right"> <li><span class="navbar-brand">Hello {{ user.username }}!</span></li> <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li> </ul> </form> {% else %} <ul class="nav navbar-nav navbar-right"> <li><a href="{% url 'login' %}">Log in</a></li> </ul> {% endif %}在資料夾中
app建立檔案,forms.py其中包含下列內容。from django import forms from django.contrib.auth.forms import AuthenticationForm from django.utils.translation import ugettext_lazy as _ class BootstrapAuthenticationForm(AuthenticationForm): """Authentication form which uses boostrap CSS.""" username = forms.CharField(max_length=254, widget=forms.TextInput({ 'class': 'form-control', 'placeholder': 'User name'})) password = forms.CharField(label=_("Password"), widget=forms.PasswordInput({ 'class': 'form-control', 'placeholder':'Password'}))修改以
app/models.py符合下列程式碼。from django.db import models from django.contrib.auth.models import User # In this web app a Microsoft Advertising user maps a Django web user to a refresh token. class BingAdsUser(models.Model): user = models.OneToOneField(User, on_delete=models.PROTECT) refresh_token = models.CharField(max_length=200) # def __unicode__(self): # __unicode__ on Python 2 # return self.refresh_token def __str__(self): # __str__ on Python 3 return self.refresh_token修改以
app/views.py符合下列程式碼。from django.http import HttpRequest, HttpResponse from django.shortcuts import render from django.template.loader import get_template, render_to_string from web_project import settings from datetime import datetime from django.shortcuts import redirect from django.contrib.auth import authenticate, login, logout, get_user_model from django.contrib.auth.models import User from app.models import BingAdsUser from bingads import * # import logging # logging.basicConfig(level=logging.INFO) # logging.getLogger('suds.client').setLevel(logging.DEBUG) # logging.getLogger('suds.transport').setLevel(logging.DEBUG) authorization_data = AuthorizationData( account_id=None, customer_id=None, developer_token=None, authentication=None) customer_service=None def home(request): """ If an authenticated user returns to this page after logging in, the appropriate context is provided to index.html for rendering the page. """ assert isinstance(request, HttpRequest) # If the Django user has a refresh token stored, # try to use it to get Microsoft Advertising data. if user_has_refresh_token(request.user.username): return redirect('/callback') else: return render( request, 'app/index.html' ) def callback(request): """Handles OAuth authorization, either via callback or direct refresh request.""" assert isinstance(request, HttpRequest) authentication = OAuthWebAuthCodeGrant( client_id=settings.CLIENT_ID, client_secret=settings.CLIENT_SECRET, redirection_uri=settings.REDIRECTION_URI, env=settings.ENVIRONMENT) return authorize_bing_ads_user(request, authentication) def authorize_bing_ads_user(request, authentication): assert isinstance(request, HttpRequest) global customer_service bingadsuser = None try: Users = get_user_model() user = User.objects.get(username=request.user.username) except User.DoesNotExist: return render( request, 'app/index.html' ) try: bingadsuser = user.bingadsuser except BingAdsUser.DoesNotExist: bingadsuser = BingAdsUser() bingadsuser.user = user pass try: # If we have a refresh token let's refresh the access token. if(bingadsuser is not None and bingadsuser.refresh_token != ""): authentication.request_oauth_tokens_by_refresh_token(bingadsuser.refresh_token) bingadsuser.refresh_token = authentication.oauth_tokens.refresh_token # If the current HTTP request is a callback from the Microsoft Account authorization server, # use the current request url containing authorization code to request new access and refresh tokens. elif (request.GET.get('code') is not None): authentication.request_oauth_tokens_by_response_uri(response_uri = request.get_full_path()) bingadsuser.refresh_token = authentication.oauth_tokens.refresh_token except OAuthTokenRequestException: bingadsuser.refresh_token = "" user.save() bingadsuser.save() # If there is no refresh token saved and no callback from the authorization server, # then connect to the authorization server and request user consent. if (bingadsuser.refresh_token == ""): return redirect(authentication.get_authorization_endpoint()) set_session_data(request, authentication) # At this point even if the user has valid Django web application credentials, # we don't know whether they have access to Microsoft Advertising. # Let's test to see if they can call Bing Ads API service operations. bing_ads_user = None accounts=[] errors=[] try: bing_ads_user = get_user(None) accounts = search_accounts_by_user_id(bing_ads_user.Id)['AdvertiserAccount'] except WebFault as ex: errors=get_webfault_errors(ex) pass context = { 'bingadsuser': bing_ads_user, 'accounts': accounts, 'errors': errors, } return render( request, 'app/index.html', context ) def revoke(request): """Deletes the refresh token for the user authenticated in the current session.""" assert isinstance(request, HttpRequest) try: Users = get_user_model() user = User.objects.get(username=request.user.username) bingadsuser = user.bingadsuser if(bingadsuser is not None): bingadsuser.refresh_token = "" bingadsuser.save() except User.DoesNotExist: pass except BingAdsUser.DoesNotExist: pass clear_session_data(request) return render( request, 'app/index.html' ) def user_has_active_session(request): try: return True if request.session['is_authenticated'] else False except KeyError: return False def user_has_refresh_token(username): try: Users = get_user_model() user = User.objects.get(username=username) bingadsuser = user.bingadsuser if(bingadsuser is not None and bingadsuser.refresh_token != ""): return True except User.DoesNotExist: return False except BingAdsUser.DoesNotExist: return False def set_session_data(request, authentication): global authorization_data global customer_service try: request.session['is_authenticated'] = True authorization_data.authentication = authentication authorization_data.developer_token = settings.DEVELOPER_TOKEN customer_service = ServiceClient( service='CustomerManagementService', version=settings.API_VERSION, authorization_data=authorization_data, environment=settings.ENVIRONMENT ) except KeyError: pass return None def clear_session_data(request): global authorization_data global customer_service request.session['is_authenticated'] = False authorization_data = AuthorizationData(account_id=None, customer_id=None, developer_token=None, authentication=None) customer_service = None def applogout(request): logout(request) clear_session_data(request) return redirect('/') def get_user(user_id): ''' Gets a Microsoft Advertising User object by the specified user ID. :param user_id: The Microsoft Advertising user identifier. :type user_id: long :return: The Microsoft Advertising user. :rtype: User ''' global customer_service return customer_service.GetUser(UserId = user_id).User def search_accounts_by_user_id(user_id): ''' Search for account details by UserId. :param user_id: The Microsoft Advertising user identifier. :type user_id: long :return: List of accounts that the user can manage. :rtype: Dictionary of AdvertiserAccount ''' predicates={ 'Predicate': [ { 'Field': 'UserId', 'Operator': 'Equals', 'Value': user_id, }, ] } accounts=[] page_index = 0 PAGE_SIZE=100 found_last_page = False while (not found_last_page): paging=set_elements_to_none(customer_service.factory.create('ns5:Paging')) paging.Index=page_index paging.Size=PAGE_SIZE search_accounts_response = customer_service.SearchAccounts( PageInfo=paging, Predicates=predicates ) if search_accounts_response is not None and hasattr(search_accounts_response, 'AdvertiserAccount'): accounts.extend(search_accounts_response['AdvertiserAccount']) found_last_page = PAGE_SIZE > len(search_accounts_response['AdvertiserAccount']) page_index += 1 else: found_last_page=True return { 'AdvertiserAccount': accounts } def set_elements_to_none(suds_object): for (element) in suds_object: suds_object.__setitem__(element[0], None) return suds_object def get_webfault_errors(ex): errors=[] if not hasattr(ex.fault, "detail"): raise Exception("Unknown WebFault") error_attribute_sets = ( ["ApiFault", "OperationErrors", "OperationError"], ["AdApiFaultDetail", "Errors", "AdApiError"], ["ApiFaultDetail", "BatchErrors", "BatchError"], ["ApiFaultDetail", "OperationErrors", "OperationError"], ["EditorialApiFaultDetail", "BatchErrors", "BatchError"], ["EditorialApiFaultDetail", "EditorialErrors", "EditorialError"], ["EditorialApiFaultDetail", "OperationErrors", "OperationError"], ) for error_attribute_set in error_attribute_sets: errors = get_api_errors(ex.fault.detail, error_attribute_set) if errors is not None: return errors return None def get_api_errors(error_detail, error_attribute_set): api_errors = error_detail for field in error_attribute_set: api_errors = getattr(api_errors, field, None) if api_errors is None: return None errors=[] if type(api_errors) == list: for api_error in api_errors: errors.append(api_error) else: errors.append(api_errors) return errors以下列內容取代 的內容
web_project/urls.py。 您可以在檔案中urls.py指定模式,將不同的 URL 路由傳送至其適當的檢視。 例如,下列程式碼會將應用程式 ()""的根 URL 對home應至您剛新增app/views.py的函數:from django.contrib import admin from django.urls import path from app import views as app_views from django.contrib.auth import views as auth_views from datetime import datetime from django.conf.urls import include, url from app.forms import BootstrapAuthenticationForm from django.contrib.auth.views import HttpResponseRedirect from django.contrib import admin admin.autodiscover() urlpatterns = [ url(r'^applogout', app_views.applogout, name='applogout'), url(r'^callback', app_views.callback, name='callback'), url(r'^revoke', app_views.revoke, name='revoke'), url(r'^$', app_views.home, name='home'), url(r'^login/$', auth_views.LoginView.as_view( template_name='app/login.html', authentication_form=BootstrapAuthenticationForm, extra_context= { 'title':'Log in', 'year':datetime.now().year, } ), name='login'), url(r'^logout$', auth_views.LogoutView.as_view(), { 'next_page': '/', }, name='logout'), url(r'^admin/', admin.site.urls), ]使用 儲存所有修改過的檔案。
Ctrl+K S執行
python manage.py makemigrations以在 migrations 資料夾中產生指令碼,將資料庫從目前狀態移轉至新狀態。執行
python manage.py migrate以將指令碼套用至實際資料庫。 移轉指令碼可有效地記錄您對資料模型所做的所有增量變更, (models.py一段時間內) 。 透過套用遷移,Django 會更新資料庫以符合您的模型。 由於每個增量變更都有自己的腳本,因此 Django 可以自動將任何先前版本的資料庫 (包括新的資料庫) 遷移到目前版本。 因此,您只需要關注 models.py 中的模型,而不需要關注基礎資料庫結構描述或移轉指令碼。 你讓 Django 做那部分!通過在虛擬環境中的 VS Code 中打開終端,然後運行命令
python manage.py createsuperuser --username=<username> --email=<email>,在應用程序中創建超級用戶帳戶,替換<username><email>,當然還有您的個人信息。 當您執行該命令時,Django 會提示您輸入並確認密碼。重要事項
請務必記住您的使用者名稱和密碼組合。 這些是您用於在 Web 應用程式的系統管理入口網站中進行驗證的認證。
在 VS Code 終端機中,再次激活虛擬環境,運行
python manage.py runserver開發服務器並打開瀏覽器以http://127.0.0.1:8000/查看呈現“Hello, Django”的頁面。在網頁瀏覽器中,移至
http://127.0.0.1:8000/admin/[使用者] 下並建立新的 Django 網頁使用者。 這與您的 Microsoft Advertising 使用者認證不同,因此多個 Microsoft Advertising 使用者可以分別登入您的應用程式。
使用新使用者登入, (不是超級管理員) ,您應該會看到使用 Microsoft 帳戶進行驗證的選項。
按一下 [ 驗證 Microsoft 帳戶] 之後,系統會提示您授與自己的 Web 應用程式權限來管理您的 Microsoft Advertising 帳戶。 如果您同意,而且如果您有權存取 Microsoft Advertising 帳戶,那麼系統應該會將您重新導向至帳戶名稱的檢視, () 和 ID () 。