Django Design Patterns and Best Practices: Industry-standard web development techniques and solutions using Python, 2nd Edition
Rate it:
Open Preview
77%
Flag icon
while writing a characterization test, you will first write a failing test with a dummy output, say X, because you don't know what to expect. When the test harness fails with an error, such as Expected output X but got Y, you will change your test to expect Y. So, now the test will pass, and it becomes a record of the code's existing behavior.
77%
Flag icon
python manage.py inspectdb > models.py
Mohamed Feddad
migrating legacy database into django models
78%
Flag icon
Django deprectations: Deprectations tell you whether a feature or idiom will be discontinued from Django in the future. Since Django 1.11, they are quiet by default. Use python -Wd so that deprecation warnings do appear.
78%
Flag icon
view = resolve('/')
Mohamed Feddad
Using resolve() is great way to test the routing, instead of calling the view directly
79%
Flag icon
self.assertListEqual(self.l1,     self.l2)
Mohamed Feddad
A more specific assertion results in a more descriptive errors
80%
Flag icon
it is always better to use the most specific assert* method for your tests. Since the second argument is not a list, the error clearly tells you that a list was expected.
80%
Flag icon
Here are some qualities of a good test case (which is a subjective term, of course) in the form of an easy-to-remember mnemonic fast, independent, repeatable, small, transparent (FIRST)
80%
Flag icon
Do not (re)test the framework: Django is well tested. Don't check for URL lookup, template rendering, and other framework-related functionalities.
80%
Flag icon
Test models most, templates least:
80%
Flag icon
Avoid using the web test client in unit tests:
80%
Flag icon
Avoid interacting with external systems: Mock them if possible.
81%
Flag icon
a test fixture is a file that contains a set of data that can be imported into your database to bring it to a known state. Typically, they are YAML or JSON files previously exported from the same database when it had some data.
Mohamed Feddad
Legacy and uncommon way of handling testing data
81%
Flag icon
many consider using fixtures as an anti-pattern. It is recommended that you use factories instead.
83%
Flag icon
The django-extensions package is shipped with the fantastic Werkzeug debugger that provides exactly this feature. In the following screenshot of the same exception, note the fully interactive Python interpreter available at each level of the call stack:
83%
Flag icon
Logging has several advantages over printing: it has a timestamp, a clearly marked level of urgency (for example, INFO, DEBUG), and you don't have to remove them from your code later.
83%
Flag icon
Once you have configured the LOGGING variable in settings.py, adding a logger to your existing code is quite easy, as shown here: # views.py import logging logger = logging.getLogger(__name__)
84%
Flag icon
We need to set TEMPLATE_DEBUG to True (in addition to DEBUG) in settings.py so that Django shows a better error page when there is an error in your templates.
84%
Flag icon
if you want to dump all the variables, use the built-in debug tag like this (inside a conveniently clickable text area):
84%
Flag icon
However, you might want to pause in the middle of a template to inspect the state (say, inside a loop). A debugger will be perfect for such cases. In fact, it is possible to use any one of the aforementioned Python debuggers for your templates using custom template tags.
Mohamed Feddad
Debugging templates with pudb!
85%
Flag icon
class XSSDemoView(View):     def get(self, request):         # WARNING: This code is insecure and prone to XSS attacks         #          *** Do not use it!!! ***         if 'q' in request.GET:             return HttpResponse("Searched for: {}".format(                     request.GET['q']))         else:             return HttpResponse("""<form method="get">         <input type="text" name="q" placeholder="Search" value="">         <button type="submit">Go</button>         </form>""") The preceding code is a View class that shows a search form when accessed without any GET parameters. If the ...more
Mohamed Feddad
Basic example of cross-site-scripting
86%
Flag icon
we can use a RegexValidator class in our search-term field to restrict the user to alphanumeric characters and allow punctuation symbols recognized by your search module. Restrict the acceptable range of the user input as strictly as possible.
86%
Flag icon
For instance, the following fake 0x0 image can be embedded in a comment: <img src="http://superbook.com/post?message=I+am+a+Dufus" width="0" height="0" border="0">
Mohamed Feddad
Basic example of cross-site request forgery
86%
Flag icon
The basic protection against CSRF is to use an HTTP POST (or PUT and DELETE, if supported) for any action that has side effects. Any GET (or HEAD) request must be used for information retrieval, for example, read-only.
87%
Flag icon
name = request.GET['user'] sql = "SELECT email FROM users WHERE username = '{}';".format(name) At first glance, it might appear that only the email address corresponds to the username mentioned as the GET parameter will be returned. However, imagine if an attacker entered ' OR '1'='1' in the form field, then the SQL code would be as follows: SELECT email FROM users WHERE username = '' OR '1'='1';
Mohamed Feddad
Basic example of SQL injection
87%
Flag icon
Clickjacking is a means of misleading a user to click on a hidden link or button in the browser when they were intending to click on something else. This is typically implemented using an invisible IFRAME that contains the target website over a dummy web page
88%
Flag icon
It works by setting the X-Frame-Options header to SAMEORIGIN for every outgoing HttpResponse. Most modern browsers recognize the header, which means that this page should not be inside a frame in other domains.
Mohamed Feddad
Django setting enabled by default to prevent iframe embedding
88%
Flag icon
os.system("ls -l {}".format(filename)) An attacker can enter the filename as manage.py; rm -rf * and delete all the files in your directory. In
Mohamed Feddad
Basic example of shell injection
88%
Flag icon
use call() from the subprocess module to run command-line programs with its default shell=False parameter to handle arguments securely if shell interpolation is not necessary.
89%
Flag icon
Don't trust data from a browser, API, or any outside sources
89%
Flag icon
Don't keep SECRET_KEY in version control
89%
Flag icon
Don't store passwords in plain text
89%
Flag icon
Don't log any sensitive data
89%
Flag icon
use SSL
89%
Flag icon
Avoid using redirects to user-supplied URLs
89%
Flag icon
Use the strictest possible regular expressions
89%
Flag icon
Don't keep your Python code in web root
89%
Flag icon
CSRF should be enabled and used
90%
Flag icon
Limit the size and type of user-uploaded files
90%
Flag icon
Have a backup and recovery plan
90%
Flag icon
there must be no development performed directly on the production environment. In fact, there is no need to install development tools, such as a compiler or debugger, in production. The presence of any unneeded software increases the attack surface of your site and could pose a security risk.
90%
Flag icon
even if an internal component fails, there is enough redundancy to prevent the entire system crashing. This concept of avoiding a single point of failure (SPOF) can be applied at every level, hardware or software.
91%
Flag icon
For example, username availability might be looked up on Redis, while the primary database might be PostgreSQL.
91%
Flag icon
As you can imagine, most real-world applications will be composed of multiple Microservices and each of them would require multiple containers. If you run them on multiple servers, how would you deploy these containers across them? How can you scale individual microservices up or down? Kubernetes is the most widely recommended solution for managing such container clusters.
91%
Flag icon
Platform as a Service (PaaS) is defined as a cloud service where the solution stack is already provided and managed for you. Popular platforms for Django hosting include Heroku, PythonAnywhere, and Google App Engine.
92%
Flag icon
A more appropriate term would be Function as a Service (FaaS),
93%
Flag icon
Monitoring tools usually need a backend service (sometimes called agents) to collect the statistics and frontend service to display dashboards or generate reports. Popular data collection backends include StatsD and Monit. This data can be passed to frontend tools, such as Graphite. There are several hosted monitoring tools, such as New Relic and Status.io, which are easier to set up and use.
93%
Flag icon
tests at Amazon in 2007 revealed that for every 100 ms increase in load time of amazon.com, the sales decreased by 1 percent.
93%
Flag icon
The key to improving performance is finding where the bottlenecks are. Rather than relying on guesswork, it is always recommended that you measure and profile your application to identify these performance bottlenecks. As Lord Kelvin would say: "If you can't measure it, you can't improve it."
94%
Flag icon
Cache infinitely with CachedStaticFilesStorage:
94%
Flag icon
Use a static asset manager: