zoukankan      html  css  js  c++  java
  • Django文档阅读-Day4

    Django文档阅读-Day4

    Writing your first Django app, part 4

    Writing a minimal form(it begins where Day3 left off)

    As mentioned in Tutorial 3, request is an HttpRequest object. For more on HttpRequest objects, see the request and response documentation.

    After somebody votes in a question, the vote() view redirects to the results page for the question. Let’s write that view:

    from django.shortcuts import get_object_or_404, render
    
    
    def results(request, question_id):
        question = get_object_or_404(Question, pk=question_id)
        return render(request, 'polls/results.html', {'question': question})
    

    This is almost exactly the same as the detail() view from Tutorial 3. The only difference is the template name. We’ll fix this redundancy later.

    Now, create a polls/results.html template:

    <h1>{{ question.question_text }}</h1>
    
    <ul>
    {% for choice in question.choice_set.all %}
        <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
    {% endfor %}
    </ul>
    
    <a href="{% url 'polls:detail' question.id %}">Vote again?</a>
    

    Now, go to /polls/1/ in your browser and vote in the question. You should see a results page that gets updated each time you vote. If you submit the form without having chosen a choice, you should see the error message.

    Note

    The code for our vote() view does have a small problem. It first gets the selected_choice object from the database, then computes the new value of votes, and then saves it back to the database. If two users of your website try to vote at exactly the same time, this might go wrong: The same value, let’s say 42, will be retrieved for votes. Then, for both users the new value of 43 is computed and saved, but 44 would be the expected value.

    This is called a race condition. If you are interested, you can read Avoiding race conditions using F() to learn how you can solve this issue.

    Use generic views: Less code is better

    The detail() (from Tutorial 3) and results() views are very short – and, as mentioned above, redundant. The index() view, which displays a list of polls, is similar.

    These views represent a common case of basic Web development: getting data from the database according to a parameter passed in the URL, loading a template and returning the rendered template. Because this is so common, Django provides a shortcut, called the “generic views” system.

    Generic views abstract common patterns to the point where you don’t even need to write Python code to write an app.

    Let’s convert our poll app to use the generic views system, so we can delete a bunch of our own code. We’ll have to take a few steps to make the conversion. We will:

    1. Convert the URLconf.
    2. Delete some of the old, unneeded views.
    3. Introduce new views based on Django’s generic views.

    Read oWhy the code-shuffle?

    Generally, when writing a Django app, you’ll evaluate whether generic views are a good fit for your problem, and you’ll use them from the beginning, rather than refactoring your code halfway through. But this tutorial intentionally has focused on writing the views “the hard way” until now, to focus on core concepts.

    You should know basic math before you start using a calculator.n for details.

    Amend URLconf

    First, open the polls/urls.py URLconf and change it like so:

    from django.urls import path
    
    from . import views
    
    app_name = 'polls'
    urlpatterns = [
        path('', views.IndexView.as_view(), name='index'),
        path('<int:pk>/', views.DetailView.as_view(), name='detail'),
        path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
        path('<int:question_id>/vote/', views.vote, name='vote'),
    ]
    

    Note that the name of the matched pattern in the path strings of the second and third patterns has changed from <question_id> to <pk>.

    Amend views

    Next, we’re going to remove our old index, detail, and results views and use Django’s generic views instead. To do so, open the polls/views.py file and change it like so:

    from django.http import HttpResponseRedirect
    from django.shortcuts import get_object_or_404, render
    from django.urls import reverse
    from django.views import generic #通用视图
    
    from .models import Choice, Question
    
    
    class IndexView(generic.ListView):
        template_name = 'polls/index.html'
        context_object_name = 'latest_question_list'
    
        def get_queryset(self):
            """Return the last five published questions."""
            return Question.objects.order_by('-pub_date')[:5]
    
    
    class DetailView(generic.DetailView):
        model = Question
        template_name = 'polls/detail.html'
    
    
    class ResultsView(generic.DetailView):
        model = Question
        template_name = 'polls/results.html'
    
    
    def vote(request, question_id):
        ... # same as above, no changes needed.
    

    We’re using two generic views here: ListView and DetailView. Respectively, those two views abstract the concepts of “display a list of objects” and “display a detail page for a particular type of object.”

    • Each generic view needs to know what model it will be acting upon. This is provided using the model attribute.
    • The DetailView generic view expects the primary key value captured from the URL to be called "pk", so we’ve changed question_id to pk for the generic views.

    By default, the DetailView generic view uses a template called <app name>/<model name>_detail.html. In our case, it would use the template "polls/question_detail.html". The template_name attribute is used to tell Django to use a specific template name instead of the autogenerated default template name. We also specify the template_name for the results list view – this ensures that the results view and the detail view have a different appearance when rendered, even though they’re both a DetailView behind the scenes.

    Similarly, the ListView generic view uses a default template called <app name>/<model name>_list.html; we use template_name to tell ListView to use our existing "polls/index.html" template.

    In previous parts of the tutorial, the templates have been provided with a context that contains the question and latest_question_list context variables. For DetailView the question variable is provided automatically – since we’re using a Django model (Question), Django is able to determine an appropriate name for the context variable. However, for ListView, the automatically generated context variable is question_list. To override this we provide the context_object_name attribute, specifying that we want to use latest_question_list instead. As an alternative approach, you could change your templates to match the new default context variables – but it’s a lot easier to tell Django to use the variable you want.

    Run the server, and use your new polling app based on generic views.

    For full details on generic views, see the generic views documentation.

    When you’re comfortable with forms and generic views, read part 5 of this tutorial to learn about testing our polls app.

    Writing your first Django app, part 5

    We'll now create some automated tests for it.

    Introducing automated testing

    What art automated tests?

    Tests are routines that check the operation of your code.

    测试,是用来检查代码正确性的一些简单程序。

    Testing operates at different levels. Some tests might apply to a tiny detail (does a particular model method return values as expected?) while others examine the overall operation of the software (does a sequence of user inputs on the site produce the desired result?). That’s no different from the kind of testing you did earlier in Tutorial 2, using the shell to examine the behavior of a method, or running the application and entering data to check how it behaves.

    What’s different in automated tests is that the testing work is done for you by the system. You create a set of tests once, and then as you make changes to your app, you can check that your code still works as you originally intended, without having to perform time consuming manual testing.

    自动化测试是某个系统帮助你自动完成测试,当你创建好一系列测试,每次修改应用代码后,就可以自动检查出修改后的代码是否还像你曾经预期的那样正常工作。你不需要花大量时间去进行手工测试。

    why you need to create tests

    So why create tests, and why now?

    You may feel that you have quite enough on your plate just learning Python/Django, and having yet another thing to learn and do may seem overwhelming and perhaps unnecessary. After all, our polls application is working quite happily now; going through the trouble of creating automated tests is not going to make it work any better. If creating the polls application is the last bit of Django programming you will ever do, then true, you don’t need to know how to create automated tests. But, if that’s not the case, now is an excellent time to learn.

    Tests will save you time

    Up to a certain point, ‘checking that it seems to work’ will be a satisfactory test. In a more sophisticated application, you might have dozens of complex interactions between components.

    A change in any of those components could have unexpected consequences on the application’s behavior. Checking that it still ‘seems to work’ could mean running through your code’s functionality with twenty different variations of your test data to make sure you haven’t broken something - not a good use of your time.

    That’s especially true when automated tests could do this for you in seconds. If something’s gone wrong, tests will also assist in identifying the code that’s causing the unexpected behavior.

    Sometimes it may seem a chore to tear yourself away from your productive, creative programming work to face the unglamorous and unexciting business of writing tests, particularly when you know your code is working properly.

    However, the task of writing tests is a lot more fulfilling than spending hours testing your application manually or trying to identify the cause of a newly-introduced problem.

    Tests don’t just identify problems, they prevent them
    It’s a mistake to think of tests merely as a negative aspect of development.

    Without tests, the purpose or intended behavior of an application might be rather opaque. Even when it’s your own code, you will sometimes find yourself poking around in it trying to find out what exactly it’s doing.

    Tests change that; they light up your code from the inside, and when something goes wrong, they focus light on the part that has gone wrong - even if you hadn’t even realized it had gone wrong.

    如果没有测试,整个应用的行为意图会变得更加不清晰。甚至当你再看自己写的代码也是这样,有时候需要仔细研读一段代码才能搞清楚他有什么用。

    而测试的出现改变了这一状况。测试就好像是从内部仔细检查你的代码,当有些地方出错时,这些地方将会变得很显眼,就算你自己没有意识到哪里写错了

    Tests make your code more attractive

    You might have created a brilliant piece of software, but you will find that many other developers will refuse to look at it because it lacks tests; without tests, they won’t trust it. Jacob Kaplan-Moss, one of Django’s original developers, says “Code without tests is broken by design.”

    That other developers want to see tests in your software before they take it seriously is yet another reason for you to start writing tests.

    你也许遇到过这种情况:你编写了一个绝赞的软件,但是其他开发者看都不看它一眼,因为它缺少测试。没有测试的代码不值得信任。Django最初开发者之一的Jacob Kaplan-Moss说过,“项目规划时没有包含测试是不科学的”。
    其他的开发者希望在正式使用你的代码前看到它通过了测试,这是你需要写测试的另一个重要原因。

    Tests help teams work together

    The previous points are written from the point of view of a single developer maintaining an application. Complex applications will be maintained by teams. Tests guarantee that colleagues don’t inadvertently break your code (and that you don’t break theirs without knowing). If you want to make a living as a Django programmer, you must be good at writing tests

    Basic testing strategies

    There are many ways to approach writing tests.

    Some programmers follow a discipline called “test-driven development”; they actually write their tests before they write their code. This might seem counter-intuitive, but in fact it’s similar to what most people will often do anyway: they describe a problem, then create some code to solve it. Test-driven development formalizes the problem in a Python test case.

    More often, a newcomer to testing will create some code and later decide that it should have some tests. Perhaps it would have been better to write some tests earlier, but it’s never too late to get started.

    Sometimes it’s difficult to figure out where to get started with writing tests. If you have written several thousand lines of Python, choosing something to test might not be easy. In such a case, it’s fruitful to write your first test the next time you make a change, either when you add a new feature or fix a bug.

    So let’s do that right away.

    一些开发者遵循“测试驱动”的开发原则,他们在写代码之前先写测试。这种方法开起来有点反直觉,但事实上,这和大多数人日常的做法是相吻合的。我们会先描述一个问题,然后先写代码来解决它。“测试驱动”的开发方法只是将问题的描述抽象为了Python的测试样例。

    更普遍的情况是,一个刚接触自动化测试的新手更倾向于先写代码,然后再写测试。虽然提前写测试可能更好,但是晚点写起码比不写好。

    有时候很难决定从哪里开始下手写测试。如果你才写几千行Python代码,选择从哪里开始写测试确实不怎么简单。如果这种情况,那么在你下次修改代码(比如新功能,或者修复BUG)之前写个测试是比较合理且有效的。

    所以现在我们开始吧

    Writing our first test

    We identify a bug

    Fortunately, there’s a little bug in the polls application for us to fix right away: the Question.was_published_recently() method returns True if the Question was published within the last day (which is correct) but also if the Question’s pub_date field is in the future (which certainly isn’t).

    Confirm the bug by using the shell to check the method on a question whose date lies in the future:

    $ python manage.py shell
    
    >>> import datetime
    >>> from django.utils import timezone
    >>> from polls.models import Question
    >>> # create a Question instance with pub_date 30 days in the future
    >>> future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30))
    >>> # was it published recently?
    >>> future_question.was_published_recently()
    True
    

    Since things in the future are not ‘recent’, this is clearly wrong.

    Create a test to expose the bug

    What we’ve just done in the shell to test for the problem is exactly what we can do in an automated test, so let’s turn that into an automated test.

    A conventional place for an application’s tests is in the application’s tests.py file; the testing system will automatically find tests in any file whose name begins with test.

    按照惯例,Django应用的测试应该写在应用的test.py文件里。测试系统会自动的在所有以tests开头的文件里寻找并执行测试代码。

    Put the following in the tests.py file in the polls application:

    import datetime
    
    from django.test import TestCase
    from django.utils import timezone
    
    from .models import Question
    
    
    class QuestionModelTests(TestCase):
    
        def test_was_published_recently_with_future_question(self):
            """
            was_published_recently() returns False for questions whose pub_date
            is in the future.
            """
            time = timezone.now() + datetime.timedelta(days=30)
            future_question = Question(pub_date=time)
            self.assertIs(future_question.was_published_recently(), False)
    

    Here we have created a django.test.TestCase subclass with a method that creates a Question instance with a pub_date in the future. We then check the output of was_published_recently() - which ought to be False.

    Runing tests

    In the terminal, we can run our test:

    $ python manage.py test polls
    

    and you'll see someting like:

    Creating test database for alias 'default'...
    System check identified no issues (0 silenced).
    F
    ======================================================================
    FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/path/to/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_question
        self.assertIs(future_question.was_published_recently(), False)
    AssertionError: True is not False
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    FAILED (failures=1)
    Destroying test database for alias 'default'...
    

    What happened is this:

    • manage.py test polls looked for tests in the polls application

    寻找polls应用中的测试代码

    找到了django.test.TestCase的一个子类

    • it created a special database for the purpose of testing

    创建一个用于测试的数据库

    • it looked for test methods - ones whose names begin with test

    在类中寻找测试方法------以test开头的方法

    • in test_was_published_recently_with_future_question it created a Question instance whose pub_date field is 30 days in the future
    • … and using the assertIs() method, it discovered that its was_published_recently() returns True, though we wanted it to return False

    The test informs us which test failed and even the line on which the failure occurred.

    Fix the bug

    We already know what the problem is: Question.was_published_recently() should return False if its pub_date is in the future. Amend the method in models.py, so that it will only return True if the date is also in the past:

    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now
    

    and run the test again:

    Creating test database for alias 'default'...
    System check identified no issues (0 silenced).
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    OK
    Destroying test database for alias 'default'...
    

    After identifying a bug, we wrote a test that exposes it and corrected the bug in the code so our test passes.

    Many other things might go wrong with our application in the future, but we can be sure that we won’t inadvertently reintroduce this bug, because running the test will warn us immediately. We can consider this little portion of the application pinned down safely forever.

    将来,我们的应用可能会出现其他的问题,但是我们可以肯定的是,一定不会在出现这个bug,因为我们只要简单的运行一遍测试,就会立刻受到警告。我们可以认为应用的这一小部分代码永远是安全的。

    More comprehensive tests

    While we’re here, we can further pin down the was_published_recently() method; in fact, it would be positively embarrassing if in fixing one bug we had introduced another.

    Add two more test methods to the same class, to test the behavior of the method more comprehensively:

    def test_was_published_recently_with_old_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is older than 1 day.
        """
        time = timezone.now() - datetime.timedelta(days=1, seconds=1)
        old_question = Question(pub_date=time)
        self.assertIs(old_question.was_published_recently(), False)
    
    def test_was_published_recently_with_recent_question(self):
        """
        was_published_recently() returns True for questions whose pub_date
        is within the last day.
        """
        time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
        recent_question = Question(pub_date=time)
        self.assertIs(recent_question.was_published_recently(), True)
    

    And now we have three tests that confirm that Question.was_published_recently() returns sensible values for past, recent, and future questions.

    Again, polls is a minimal application, but however complex it grows in the future and whatever other code it interacts with, we now have some guarantee that the method we have written tests for will behave in expected ways.

    Test a view

    The polls application is fairly undiscriminating: it will publish any question, including ones whose pub_date field lies in the future. We should improve this. Setting a pub_date in the future should mean that the Question is published at that moment, but invisible until then.

    A test for a view

    When we fixed the bug above, we wrote the test first and then the code to fix it. In fact that was an example of test-driven development, but it doesn’t really matter in which order we do the work.

    In our first test, we focused closely on the internal behavior of the code. For this test, we want to check its behavior as it would be experienced by a user through a web browser.

    Before we try to fix anything, let’s have a look at the tools at our disposal.

    The Django test client

    Django provides a test Client to simulate a user interacting with the code at the view level. We can use it in tests.py or even in the shell.

    We will start again with the shell, where we need to do a couple of things that won’t be necessary in tests.py. The first is to set up the test environment in the shell:

    $ python manage.py shell
    
    >>> from django.test.utils import setup_test_environment #测试工具所在位置,用于设置测试所需环境变量
    >>> setup_test_environment()
    

    setup_test_environment() installs a template renderer which will allow us to examine some additional attributes on responses such as response.context that otherwise wouldn’t be available. Note that this method does not setup a test database, so the following will be run against the existing database and the output may differ slightly depending on what questions you already created. You might get unexpected results if your TIME_ZONE in settings.py isn’t correct. If you don’t remember setting it earlier, check it before continuing.

    setup_test_environment提供了一个模板渲染器,允许我们为responses增加一些额外的属性,例如response.context,未安装此app无法使用此功能。注意,这个方法并不会配置测试数据库,所以接下来的代码将会在当前存在的数据库上运行,输出的内容可能由于数据库内容的不同而不同。如果你的settings.py中关于TIME_ZONE的设置不对,你可能无法获得期待的结果。如果你之前忘记设置,在继续之前检查一下。

    Next we need to import the test client class (later in tests.py we will use the django.test.TestCase class, which comes with its own client, so this won’t be required):

    >>> from django.test import Client
    >>> # create an instance of the client for our use
    >>> client = Client()
    

    With that ready, we can ask the client to do some work for us:

    >>> # get a response from '/'
    >>> response = client.get('/')
    Not Found: /
    >>> # we should expect a 404 from that address; if you instead see an
    >>> # "Invalid HTTP_HOST header" error and a 400 response, you probably
    >>> # omitted the setup_test_environment() call described earlier.
    >>> response.status_code
    404
    >>> # on the other hand we should expect to find something at '/polls/'
    >>> # we'll use 'reverse()' rather than a hardcoded URL
    >>> from django.urls import reverse
    >>> response = client.get(reverse('polls:index'))
    >>> response.status_code
    200
    >>> response.content
    b'
        <ul>
        
            <li><a href="/polls/1/">What&#x27;s up?</a></li>
        
        </ul>
    
    '
    >>> response.context['latest_question_list']
    <QuerySet [<Question: What's up?>]>
    
    Improving our view

    The list of polls shows polls that aren’t published yet (i.e. those that have a pub_date in the future). Let’s fix that.

    In Tutorial 4 we introduced a class-based view, based on ListView:

    class IndexView(generic.ListView):
        template_name = 'polls/index.html'
        context_object_name = 'latest_question_list'
    
        def get_queryset(self):
            """Return the last five published questions."""
            return Question.objects.order_by('-pub_date')[:5]
    

    We need to amend the get_queryset() method and change it so that it also checks the date by comparing it with timezone.now(). First we need to add an import:

    from django.utils import timezone
    

    and then we must amend the get_queryset method like so:

    def get_queryset(self):
        """
        Return the last five published questions (not including those set to be
        published in the future).
        """
        return Question.objects.filter(
            pub_date__lte=timezone.now()
        ).order_by('-pub_date')[:5]
    

    Question.objects.filter(pub_date__lte=timezone.now()) returns a queryset containing Questions whose pub_date is less than or equal to - that is, earlier than or equal to - timezone.now.

    Test our new view

    Now you can satisfy yourself that this behaves as expected by firing up runserver, loading the site in your browser, creating Questions with dates in the past and future, and checking that only those that have been published are listed. You don’t want to have to do that every single time you make any change that might affect this - so let’s also create a test, based on our shell session above.

    Add the following to polls/tests.py:

    from django.urls import reverse
    

    and we’ll create a shortcut function to create questions as well as a new test class:

    def create_question(question_text, days):
        """
        Create a question with the given `question_text` and published the
        given number of `days` offset to now (negative for questions published
        in the past, positive for questions that have yet to be published).
        """
        time = timezone.now() + datetime.timedelta(days=days)
        return Question.objects.create(question_text=question_text, pub_date=time)
    
    
    class QuestionIndexViewTests(TestCase):
        def test_no_questions(self):
            """
            If no questions exist, an appropriate message is displayed.
            """
            response = self.client.get(reverse('polls:index'))
            self.assertEqual(response.status_code, 200)
            self.assertContains(response, "No polls are available.")
            self.assertQuerysetEqual(response.context['latest_question_list'], [])
    
        def test_past_question(self):
            """
            Questions with a pub_date in the past are displayed on the
            index page.
            """
            create_question(question_text="Past question.", days=-30)
            response = self.client.get(reverse('polls:index'))
            self.assertQuerysetEqual(
                response.context['latest_question_list'],
                ['<Question: Past question.>']
            )
    
        def test_future_question(self):
            """
            Questions with a pub_date in the future aren't displayed on
            the index page.
            """
            create_question(question_text="Future question.", days=30)
            response = self.client.get(reverse('polls:index'))
            self.assertContains(response, "No polls are available.")
            self.assertQuerysetEqual(response.context['latest_question_list'], [])
    
        def test_future_question_and_past_question(self):
            """
            Even if both past and future questions exist, only past questions
            are displayed.
            """
            create_question(question_text="Past question.", days=-30)
            create_question(question_text="Future question.", days=30)
            response = self.client.get(reverse('polls:index'))
            self.assertQuerysetEqual(
                response.context['latest_question_list'],
                ['<Question: Past question.>']
            )
    
        def test_two_past_questions(self):
            """
            The questions index page may display multiple questions.
            """
            create_question(question_text="Past question 1.", days=-30)
            create_question(question_text="Past question 2.", days=-5)
            response = self.client.get(reverse('polls:index'))
            self.assertQuerysetEqual(
                response.context['latest_question_list'],
                ['<Question: Past question 2.>', '<Question: Past question 1.>']
            )
    

    Let’s look at some of these more closely.

    First is a question shortcut function, create_question, to take some repetition out of the process of creating questions.

    test_no_questions doesn’t create any questions, but checks the message: “No polls are available.” and verifies the latest_question_list is empty. Note that the django.test.TestCase class provides some additional assertion methods. In these examples, we use assertContains() and assertQuerysetEqual().

    In test_past_question, we create a question and verify that it appears in the list.

    In test_future_question, we create a question with a pub_date in the future. The database is reset for each test method, so the first question is no longer there, and so again the index shouldn’t have any questions in it.

    And so on. In effect, we are using the tests to tell a story of admin input and user experience on the site, and checking that at every state and for every new change in the state of the system, the expected results are published.

    Testing the DetailView

    What we have works well; however, even though future questions don’t appear in the index, users can still reach them if they know or guess the right URL. So we need to add a similar constraint to DetailView:

    class DetailView(generic.DetailView):
        ...
        def get_queryset(self):
            """
            Excludes any questions that aren't published yet.
            """
            return Question.objects.filter(pub_date__lte=timezone.now())
    

    And of course, we will add some tests, to check that a Question whose pub_date is in the past can be displayed, and that one with a pub_date in the future is not:

    class QuestionDetailViewTests(TestCase):
        def test_future_question(self):
            """
            The detail view of a question with a pub_date in the future
            returns a 404 not found.
            """
            future_question = create_question(question_text='Future question.', days=5)
            url = reverse('polls:detail', args=(future_question.id,))
            response = self.client.get(url)
            self.assertEqual(response.status_code, 404)
    
        def test_past_question(self):
            """
            The detail view of a question with a pub_date in the past
            displays the question's text.
            """
            past_question = create_question(question_text='Past Question.', days=-5)
            url = reverse('polls:detail', args=(past_question.id,))
            response = self.client.get(url)
            self.assertContains(response, past_question.question_text)
    

    Ideas for more tests

    We ought to add a similar get_queryset method to ResultsView and create a new test class for that view. It’ll be very similar to what we have just created; in fact there will be a lot of repetition.

    We could also improve our application in other ways, adding tests along the way. For example, it’s silly that Questions can be published on the site that have no Choices. So, our views could check for this, and exclude such Questions. Our tests would create a Question without Choices and then test that it’s not published, as well as create a similar Question with Choices, and test that it is published.

    Perhaps logged-in admin users should be allowed to see unpublished Questions, but not ordinary visitors. Again: whatever needs to be added to the software to accomplish this should be accompanied by a test, whether you write the test first and then make the code pass the test, or work out the logic in your code first and then write a test to prove it.

    At a certain point you are bound to look at your tests and wonder whether your code is suffering from test bloat, which brings us to:

    如果你想要增加一个新功能,那么同时一定要为它编写测试。不过你是先写代码,还是先写测试,就随你了。

    在未来某个时刻,你一定会去查看测试代码,然后开始怀疑:“这么多的测试不会使代码越来越复杂?

    When testing, more is better

    It might seem that our tests are growing out of control. At this rate there will soon be more code in our tests than in our application, and the repetition is unaesthetic, compared to the elegant conciseness of the rest of our code.

    It doesn’t matter. Let them grow. For the most part, you can write a test once and then forget about it. It will continue performing its useful function as you continue to develop your program.

    Sometimes tests will need to be updated. Suppose that we amend our views so that only Questions with Choices are published. In that case, many of our existing tests will fail - telling us exactly which tests need to be amended to bring them up to date, so to that extent tests help look after themselves.

    At worst, as you continue developing, you might find that you have some tests that are now redundant. Even that’s not a problem; in testing redundancy is a good thing.

    As long as your tests are sensibly arranged, they won’t become unmanageable. Good rules-of-thumb include having:

    • a separate TestClass for each model or view
    • a separate test method for each set of conditions you want to test
    • test method names that describe their function

    Further testing

    This tutorial only introduces some of the basics of testing. There’s a great deal more you can do, and a number of very useful tools at your disposal to achieve some very clever things.

    For example, while our tests here have covered some of the internal logic of a model and the way our views publish information, you can use an “in-browser” framework such as Selenium to test the way your HTML actually renders in a browser. These tools allow you to check not just the behavior of your Django code, but also, for example, of your JavaScript. It’s quite something to see the tests launch a browser, and start interacting with your site, as if a human being were driving it! Django includes LiveServerTestCase to facilitate integration with tools like Selenium.

    If you have a complex application, you may want to run tests automatically with every commit for the purposes of continuous integration, so that quality control is itself - at least partially - automated.

    A good way to spot untested parts of your application is to check code coverage. This also helps identify fragile or even dead code. If you can’t test a piece of code, it usually means that code should be refactored or removed. Coverage will help to identify dead code. See Integration with coverage.py for details.

    Testing in Django has comprehensive information about testing.

    在上述的测试中,我们已经从代码逻辑和视图响应的角度检查了应用的输出,现在你可以从一个更加“in-brower"的角度来检查最终渲染出的HTML是否符合预期,使用Selenium可以很轻松的完成这件事。这个工具不仅可以测试Django框架里的代码,还可以检查其他部分,比如说你的js。它假装成是一个正在和你站点进行交互的浏览器。就像有个真人在访问你的网站一样。Django提供了LiveServerTestCase来和Selenium这样的工具交互。

    如果你在开发一个复杂的应用,你也许想每次提交代码是自动运行测试,也就是我们所说的持续集成,这样就能实现质量控制的自动化,起码是部分自动化。

    一个找出代码中未被测试的方法是检查代码覆盖率。它有助于找出代码助攻的薄弱部分和无用部分。如果你无法测试一段代码,通常说明这段代码需要被重构或者删除。想知道代码覆盖率和无用代码的详细信息,查看文档。

    Writing your first Django app, part 6

    This tutorial begins where Tutorial 5 left off. We’ve built a tested Web-poll application, and we’ll now add a stylesheet and an image.

    Aside from the HTML generated by the server, web applications generally need to serve additional files — such as images, JavaScript, or CSS — necessary to render the complete web page. In Django, we refer to these files as “static files”.

    For small projects, this isn’t a big deal, because you can keep the static files somewhere your web server can find it. However, in bigger projects – especially those comprised of multiple apps – dealing with the multiple sets of static files provided by each application starts to get tricky.

    That’s what django.contrib.staticfiles is for: it collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.

    Customize your app's look and feel

    First, create a directory called static in your polls directory. Django will look for static files there, similarly to how Django finds templates inside polls/templates/.

    Django’s STATICFILES_FINDERS setting contains a list of finders that know how to discover static files from various sources. One of the defaults is AppDirectoriesFinder which looks for a “static” subdirectory in each of the INSTALLED_APPS, like the one in polls we just created. The admin site uses the same directory structure for its static files.

    Within the static directory you have just created, create another directory called polls and within that create a file called style.css. In other words, your stylesheet should be at polls/static/polls/style.css. Because of how the AppDirectoriesFinder staticfile finder works, you can refer to this static file in Django as polls/style.css, similar to how you reference the path for templates.

    Static file namespacing
    
    Just like templates, we might be able to get away with putting our static files directly in polls/static (rather than creating another polls subdirectory), but it would actually be a bad idea. Django will choose the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the best way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.
    

    Put the following code in that stylesheet (polls/static/polls/style.css):

    li a {
        color: green;
    }
    

    Next, add the following at the top of polls/templates/polls/index.html:

    {% load static %}
    
    <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
    

    The {% static %} template tag generates the absolute URL of static files.

    That’s all you need to do for development.

    Start the server (or restart it if it’s already running):

    $ python manage.py runserver
    

    Reload http://localhost:8000/polls/ and you should see that the question links are green (Django style!) which means that your stylesheet was properly loaded.

    Adding a backgrouind-image

    Next, we’ll create a subdirectory for images. Create an images subdirectory in the polls/static/polls/ directory. Inside this directory, put an image called background.gif. In other words, put your image in polls/static/polls/images/background.gif.

    Then, add to your stylesheet (polls/static/polls/style.css):

    body {
        background: white url("images/background.gif") no-repeat;
    }
    

    Reload http://localhost:8000/polls/ and you should see the background loaded in the top left of the screen.

    Warning
    
    Of course the {% static %} template tag is not available for use in static files like your stylesheet which aren’t generated by Django. You should always use relative paths to link your static files between each other, because then you can change STATIC_URL (used by the static template tag to generate its URLs) without having to modify a bunch of paths in your static files as well.
    

    These are the basics. For more details on settings and other bits included with the framework see the static files howto and the staticfiles reference. Deploying static files discusses how to use static files on a real server.

    When you’re comfortable with the static files, read part 7 of this tutorial to learn how to customize Django’s automatically-generated admin site.

    Writing your first Django app, part 7

    This tutorial begins where Tutorial 6 left off. We’re continuing the Web-poll application and will focus on customizing Django’s automatically-generated admin site that we first explored in Tutorial 2.

    Customizing the admin form

    By registering the Question model with admin.site.register(Question), Django was able to construct a default form representation. Often, you’ll want to customize how the admin form looks and works. You’ll do this by telling Django the options you want when you register the object.

    Let’s see how this works by reordering the fields on the edit form. Replace the admin.site.register(Question) line with:

    from django.contrib import admin
    
    from .models import Question
    
    
    class QuestionAdmin(admin.ModelAdmin):
        fields = ['pub_date', 'question_text']
    
    admin.site.register(Question, QuestionAdmin)
    

    You’ll follow this pattern – create a model admin class, then pass it as the second argument to admin.site.register() – any time you need to change the admin options for a model.

    This particular change above makes the “Publication date” come before the “Question” field:

    Fields have been reordered

    This isn’t impressive with only two fields, but for admin forms with dozens of fields, choosing an intuitive order is an important usability detail.

    And speaking of forms with dozens of fields, you might want to split the form up into fieldsets:

    from django.contrib import admin
    
    from .models import Question
    
    
    class QuestionAdmin(admin.ModelAdmin):
        fieldsets = [
            (None,               {'fields': ['question_text']}),
            ('Date information', {'fields': ['pub_date']}),
        ]
    
    admin.site.register(Question, QuestionAdmin)
    

    The first element of each tuple in fieldsets is the title of the fieldset. Here’s what our form looks like now:

    Form has fieldsets now

    OK, we have our Question admin page, but a Question has multiple Choices, and the admin page doesn’t display choices.

    Yet.

    There are two ways to solve this problem. The first is to register Choice with the admin just as we did with Question:

    from django.contrib import admin
    
    from .models import Choice, Question
    # ...
    admin.site.register(Choice)
    

    Now “Choices” is an available option in the Django admin. The “Add choice” form looks like this:

    Choice admin page

    In that form, the “Question” field is a select box containing every question in the database. Django knows that a ForeignKey should be represented in the admin as a <select> box. In our case, only one question exists at this point.

    Also note the “Add Another” link next to “Question.” Every object with a ForeignKey relationship to another gets this for free. When you click “Add Another”, you’ll get a popup window with the “Add question” form. If you add a question in that window and click “Save”, Django will save the question to the database and dynamically add it as the selected choice on the “Add choice” form you’re looking at.

    But, really, this is an inefficient way of adding Choice objects to the system. It’d be better if you could add a bunch of Choices directly when you create the Question object. Let’s make that happen.

    Remove the register() call for the Choice model. Then, edit the Question registration code to read:

    from django.contrib import admin
    
    from .models import Choice, Question
    
    
    class ChoiceInline(admin.StackedInline):
        model = Choice
        extra = 3
    
    
    class QuestionAdmin(admin.ModelAdmin):
        fieldsets = [
            (None,               {'fields': ['question_text']}),
            ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
        ]
        inlines = [ChoiceInline]
    
    admin.site.register(Question, QuestionAdmin)
    

    This tells Django: “Choice objects are edited on the Question admin page. By default, provide enough fields for 3 choices.”

    Load the “Add question” page to see how that looks:

    Add question page now has choices on it

    It works like this: There are three slots for related Choices – as specified by extra – and each time you come back to the “Change” page for an already-created object, you get another three extra slots.

    At the end of the three current slots you will find an “Add another Choice” link. If you click on it, a new slot will be added. If you want to remove the added slot, you can click on the X to the top right of the added slot. Note that you can’t remove the original three slots. This image shows an added slot:

    Additional slot added dynamically

    One small problem, though. It takes a lot of screen space to display all the fields for entering related Choice objects. For that reason, Django offers a tabular way of displaying inline related objects. To use it, change the ChoiceInline declaration to read:

    class ChoiceInline(admin.TabularInline):
        #...
    

    With that TabularInline (instead of StackedInline), the related objects are displayed in a more compact, table-based format:

    Add question page now has more compact choices

    Note that there is an extra “Delete?” column that allows removing rows added using the “Add Another Choice” button and rows that have already been saved.

    Customize the admin change list

    Now that the Question admin page is looking good, let’s make some tweaks to the “change list” page – the one that displays all the questions in the system.

    Here’s what it looks like at this point:

    Polls change list page

    By default, Django displays the str() of each object. But sometimes it’d be more helpful if we could display individual fields. To do that, use the list_display admin option, which is a tuple of field names to display, as columns, on the change list page for the object:

    class QuestionAdmin(admin.ModelAdmin):
        # ...
        list_display = ('question_text', 'pub_date')
    

    For good measure, let’s also include the was_published_recently() method from Tutorial 2:

    class QuestionAdmin(admin.ModelAdmin):
        # ...
        list_display = ('question_text', 'pub_date', 'was_published_recently')
    

    Now the question change list page looks like this:

    Polls change list page, updated

    You can click on the column headers to sort by those values – except in the case of the was_published_recently header, because sorting by the output of an arbitrary method is not supported. Also note that the column header for was_published_recently is, by default, the name of the method (with underscores replaced with spaces), and that each line contains the string representation of the output.

    You can improve that by giving that method (in polls/models.py) a few attributes, as follows:

    class Question(models.Model):
        # ...
        def was_published_recently(self):
            now = timezone.now()
            return now - datetime.timedelta(days=1) <= self.pub_date <= now
        was_published_recently.admin_order_field = 'pub_date'
        was_published_recently.boolean = True
        was_published_recently.short_description = 'Published recently?'
    

    For more information on these method properties, see list_display.

    Edit your polls/admin.py file again and add an improvement to the Question change list page: filters using the list_filter. Add the following line to QuestionAdmin:

    list_filter = ['pub_date']
    

    That adds a “Filter” sidebar that lets people filter the change list by the pub_date field:

    Polls change list page, updated

    The type of filter displayed depends on the type of field you’re filtering on. Because pub_date is a DateTimeField, Django knows to give appropriate filter options: “Any date”, “Today”, “Past 7 days”, “This month”, “This year”.

    This is shaping up well. Let’s add some search capability:

    search_fields = ['question_text']
    

    That adds a search box at the top of the change list. When somebody enters search terms, Django will search the question_text field. You can use as many fields as you’d like – although because it uses a LIKE query behind the scenes, limiting the number of search fields to a reasonable number will make it easier for your database to do the search.

    Now’s also a good time to note that change lists give you free pagination. The default is to display 100 items per page. Change list pagination, search boxes, filters, date-hierarchies, and column-header-ordering all work together like you think they should.

    今日收获单词

    refactor 重构
    code-shuffle 代码重构
    intention 计划,打算,意图
    intentionally 故意地,存心地
    amend 改善,改进,改正
    respective 分别的
    respectively 分别地
    alternative 可选择的,另类,替代物
    override 推翻,不顾
    routine 常规,正常顺序
    desired 渴望的
    have quite enough on your plate. 对你已经足够了
    overwhelming 巨大的,压倒性的,无法抗拒的,负担过重的
    excellent 卓越的,杰出的
    up to certain point 某种程度上说
    sophiosticate so phis ti cate 使复杂
    dozens 一打,一大堆,十二个
    dozens of
    consequence 结果,重要性,推论
    assist 帮助,助攻,参加
    chore 日常职务,杂务
    away from 远离,距离
    unglamorous 单调的,乏味的
    fulfilling 令人满意的
    aspect 方面,方向,形式
    opaque 不透明的,迟钝的
    poke 刺戳
    maintain 维护
    inadvertently 无心的
    inadvertent 疏忽的,不注意的
    advertent 留意的
    advert 引起注意
    discipline 学科,纪律,训练,惩罚,规范
    intuitive 直觉的
    intuition 直觉
    counter-intuitive 反直觉的
    counter 反击,反驳,相反的
    pin down 查明,确定,使受约束
    comprhensive 更加广泛的,有理解力的
    undiscriminating 不加以区别的
    internal 内部的,内在的
    disposal 处理,支配,安排
    simulate 模仿,假装,冒充
    a couple of 三两
    omit 省略,遗漏,删除
    fire up 启动,煽动
    repetition 重复,副本
    unaesthetic  无美感的,缺乏美感的
    conciseness 简明,简洁
    facilitate 促进,帮助
    integration 集成,综合
    fragile 脆的,易碎的
    coverage 覆盖
    comprise 包含,由...组成
    underscore 强调,下划线
    
  • 相关阅读:
    js递归函数和call()
    前端常用
    整理项目中用到的angularjs及其他js代码
    体验设计真的是让一切简单到极致吗?
    iview table中利用render动态循环输出
    Vue+iview在render函数中添加Poptip提示操作
    jQuery
    外部js调用vue实例方法
    es6 filter() 数组过滤方法总结
    vue通信、传值的多种方式
  • 原文地址:https://www.cnblogs.com/xunjishu/p/12775332.html
Copyright © 2011-2022 走看看