<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Piotr Zalewa on Medium]]></title>
        <description><![CDATA[Stories by Piotr Zalewa on Medium]]></description>
        <link>https://medium.com/@zalun?source=rss-c4608c2c6f28------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*J4yb0y5eoaCDp5O8Zv1q5A.png</url>
            <title>Stories by Piotr Zalewa on Medium</title>
            <link>https://medium.com/@zalun?source=rss-c4608c2c6f28------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 02:19:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zalun/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mob Agentic Engineering: Riding the AI Wave Together.]]></title>
            <link>https://medium.com/@zalun/mob-agentic-engineering-riding-the-ai-wave-together-5a46915ac386?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a46915ac386</guid>
            <category><![CDATA[pair-programming]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mob-programming]]></category>
            <category><![CDATA[mdc]]></category>
            <category><![CDATA[agents]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Tue, 03 Jun 2025 05:23:47 GMT</pubDate>
            <atom:updated>2025-06-03T09:11:48.817Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mob Agentic Engineering: Riding the AI Wave Together</h3><p>The numbers are staggering: 41% of all code is now AI-generated, with tech giants like Google reporting over 25% of their new code written by AI. This isn’t a future prediction — it’s today’s reality.</p><p>This article is also available on the <a href="https://nod.consulting/blog/mob-agentic-engineering">Nod Consulting page</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i9HVXsS_bs4winnG7ehiTw.png" /><figcaption>Microsoft Designer hallucinating the title</figcaption></figure><p>As I detailed in my previous <a href="https://medium.com/@zalun/mob-programming-unlocking-collective-intelligence-for-better-software-development-e1ee5bd0b776">article on mob programming</a>, collaborative development practices deliver a proven return on investment (ROI) through enhanced code quality, accelerated knowledge sharing, and reduced delivery risk. But what happens when AI becomes your primary code generator?</p><p>The answer isn’t just “mob prompting” — it’s a fundamental reimagining of how teams collaborate with intelligent agents.</p><h3>The Strategic Shift: When AI Writes the Code</h3><p>Traditional mob programming thrives on collective human intelligence tackling complex problems together. However, AI code generation introduces a critical challenge: <strong>context loss at scale</strong>. When an AI can generate entire functions in seconds, the traditional flow of driver-navigator-mob breaks down. The AI doesn’t inherently understand your team’s architectural decisions, previous iterations, or the reasoning behind rejected approaches.</p><p>This creates what I call the “context void” — where powerful AI capabilities are undermined by a lack of persistent, shared understanding. Teams find themselves constantly re-educating their AI agents, explaining constraints repeatedly, and losing the collaborative momentum that makes mob programming so effective.</p><p>The business impact is immediate:</p><ul><li><strong>Development velocity gains of 55%</strong> are offset by context-switching overhead</li><li><strong>Code quality improvements</strong> are compromised by misaligned AI outputs</li><li><strong>Knowledge sharing benefits</strong> erode when the AI can’t maintain team context</li></ul><p>For engineering leaders, this presents both opportunity and risk. Organizations that solve the context problem first will gain substantial competitive advantages, while those that don’t risk losing AI’s productivity benefits to collaborative friction.</p><h3>The Business Case</h3><p>Early adopters of AI-enhanced development are seeing remarkable returns: average ROI of 3.5x on AI investments, with some reporting up to 8x returns. However, success requires more than just deploying AI tools — it demands strategic integration of human oversight with AI capabilities.</p><p><strong>The opportunity:</strong></p><ul><li><strong>Time-to-market acceleration</strong>: Overall task completion reported up to 55% faster, contributing to significantly accelerated development cycles.</li><li><strong>Quality improvements</strong>: 41% reduction in critical defects when AI and humans collaborate effectively</li><li><strong>Team scaling</strong>: Junior developers experience accelerated learning and up to a 35% reduction in onboarding time, boosting their productivity sooner.</li><li><strong>Cost efficiency</strong>: Average 27% reduction in development costs</li></ul><p><strong>The hidden risks:</strong></p><ul><li><strong>Technical debt explosion</strong>: 8x increase in code duplication when AI lacks proper guidance</li><li><strong>Security vulnerabilities</strong>: AI-generated code requires specialized review processes</li><li><strong>Skill erosion</strong>: Over-reliance on AI can diminish fundamental programming capabilities</li><li><strong>Delivery instability</strong>: Poorly managed AI integration can decrease deployment reliability by 7%</li></ul><p>The organizations winning with AI aren’t just using it faster — they’re using it smarter, with structured approaches that preserve the collaborative intelligence that drives innovation.</p><h3>The Solution: Mob Agentic Engineering</h3><p>Mob Agentic Engineering transforms AI from a tool used by the mob into an active participant within the mob. This isn’t about replacing human roles — it’s about elevating them.</p><p><strong>Redefined roles for the AI era:</strong></p><p><strong>The Prompt Driver</strong> replaces the traditional driver, becoming an AI interaction specialist who crafts precise instructions and iterates on prompts. This role requires understanding both the AI’s capabilities and the team’s strategic intent.</p><p><strong>The AI Navigator</strong> evolves from the traditional navigator role, focusing on interpreting AI responses, identifying misalignments, and guiding prompt refinement. They ensure AI output serves the team’s architectural vision.</p><p><strong>The Human Mob</strong> provides collective oversight, strategic direction, and quality assurance. Rather than diminishing in importance, this role becomes more critical — focusing on creativity, complex problem-solving, and ensuring AI contributions align with business objectives.</p><p><strong>The Context Bridge solution:</strong></p><p>The key innovation is creating persistent, shared context that both humans and AI can access. This “Context Bridge” captures:</p><ul><li>Successful and failed approaches with rationale</li><li>Evolving requirements and architectural decisions</li><li>Current task focus and acceptance criteria</li><li>Key code patterns and integration points</li></ul><p>This externalized memory enables seamless handoffs, maintains AI continuity across sessions, and reduces the cognitive load on human team members. Most importantly, it transforms AI from a stateless tool into a stateful collaborator that learns alongside the team. I’ve outlined a practical approach in the <a href="https://github.com/zalun/cursor-rules/blob/main/rules/mob-programming-context-bridge.mdc">Mob Programming Context Bridge</a> MDC I defined for collaborative AI environments.</p><h3>Implementation Strategy for Leaders</h3><p><strong>When to adopt Mob Agentic Engineering:</strong></p><ul><li>Complex features requiring diverse perspectives and rapid prototyping</li><li>Projects where architectural alignment is critical</li><li>Teams onboarding junior developers who can benefit from AI-accelerated learning</li><li>Situations demanding both speed and quality under collective oversight</li></ul><p><strong>When traditional approaches still deliver better ROI:</strong></p><ul><li>Simple, routine tasks where mob overhead outweighs benefits</li><li>Highly specialized domains where AI lacks sufficient training data</li><li>Teams not yet proficient in prompt engineering and AI collaboration</li></ul><p><strong>Practical first steps:</strong></p><ol><li><strong>Start with pilot programs</strong>: Choose well-defined, complex features for initial experiments</li><li><strong>Invest in infrastructure</strong>: Ensure robust AI tooling and collaborative prompt engineering platforms</li><li><strong>Focus on team readiness</strong>: Assess both technical capabilities and cultural willingness to embrace AI collaboration</li><li><strong>Establish governance</strong>: Create clear guidelines for AI usage, code review processes, and quality metrics</li></ol><p><strong>Success metrics that matter:</strong></p><ul><li><strong>Development velocity</strong>: Cycle time and deployment frequency</li><li><strong>Quality indicators</strong>: Defect rates, code maintainability scores, security vulnerability detection</li><li><strong>Team satisfaction</strong>: Developer experience surveys and engagement metrics</li><li><strong>Knowledge distribution</strong>: Reduced silos and improved cross-functional collaboration</li></ul><h3>Your Competitive Advantage</h3><p>The window for competitive advantage through AI adoption is narrowing rapidly. Organizations that delay risk facing higher implementation costs, talent acquisition challenges, and market share erosion to more agile competitors.</p><p><strong>The strategic imperative is clear</strong>: AI-enhanced development isn’t optional — it’s the new baseline. The question isn’t whether to adopt AI, but how quickly you can do it effectively while preserving the collaborative intelligence that drives innovation.</p><p>Mob Agentic Engineering offers a structured path forward, transforming AI from a potentially disruptive force into a powerful amplifier of team capabilities. By thoughtfully redefining roles, implementing context management solutions, and maintaining focus on collective intelligence, organizations can ride the AI wave together — achieving the speed benefits of AI while preserving the quality and innovation advantages of collaborative development.</p><p><strong>The future belongs to teams that can seamlessly blend human creativity with AI capabilities.</strong> The tools are evolving at breathtaking speed; it’s time for our collaborative practices to evolve with them.</p><p><em>Ready to transform your development practices? Start with a pilot program, focus on building shared context, and discover how your team can harness AI while strengthening collaboration. The journey into Mob Agentic Engineering is an investment in a more powerful, efficient, and ultimately more human-centered future for software development.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a46915ac386" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mob Programming: Unlocking Collective Intelligence for Better Software Development.]]></title>
            <link>https://medium.com/@zalun/mob-programming-unlocking-collective-intelligence-for-better-software-development-e1ee5bd0b776?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1ee5bd0b776</guid>
            <category><![CDATA[mob-programming]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[team]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Thu, 29 May 2025 10:51:38 GMT</pubDate>
            <atom:updated>2025-05-29T10:51:38.975Z</atom:updated>
            <content:encoded><![CDATA[<p>Republished from the <a href="https://nod.consulting/blog/mob-programming">article</a> on the Nod Consulting page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*51D522LlwS5jvz7myExT1A.png" /><figcaption>Microsoft Designer hallucinating the title</figcaption></figure><p>In today’s competitive software landscape, organizations are constantly searching for approaches that enhance productivity, improve quality, and foster team cohesion. Mob programming — a collaborative technique where an entire team works together on the same task, at the same computer — is emerging as a powerful practice that achieves these goals by tapping into the collective intelligence of your development team. And contrary to what you might expect, this practice isn’t limited to co-located teams — it can be remarkably effective in remote settings as well.</p><p>Having personally introduced both pair and mob programming to multiple development teams, I can attest that while the transition isn’t always smooth, the results are worth the initial discomfort. When I first suggested gathering five developers around a single screen, I was met with skepticism and resistance. “It sounds inefficient,” was the most common objection. Yet within days, those same skeptics were the practice’s strongest advocates.</p><h3>What is Mob Programming?</h3><p>Imagine your entire development team gathered around a single workstation: one person typing (the “driver”), another providing immediate guidance (the “navigator”), and the rest of the team actively contributing ideas and insights. This is mob programming in action — a natural evolution of pair programming that scales collaboration to include the entire team. In remote environment it looks like a meeting, with the <em>driver</em> sharing the editor window.</p><p>The practice follows a straightforward structure:</p><ul><li>One team member serves as the <strong>driver</strong>, controlling the keyboard and implementing the code</li><li>Another acts as the <strong>navigator</strong>, providing immediate direction</li><li>Remaining team members contribute as the <strong>mob</strong>, offering insights, spotting issues, and suggesting solutions</li><li>Roles rotate regularly, typically every 10–15 minutes, ensuring everyone participates fully</li></ul><h3>Why Implement Mob Programming?</h3><p>Research and industry experiences point to several compelling benefits:</p><h3>1. Higher Quality Code</h3><p>When multiple experts review code as it’s being written, defects are caught immediately. A study by Ståhl and Mårtensson (2021) found that mob programming teams reported “improved software quality and reduced technical debt” compared to traditional approaches. One team in their research noted that “the things we have done as a mob are of better quality and have fewer problems.”</p><p>In my experience implementing mob programming across three different teams, we saw a significant reduction in production defects within the first quarter.</p><h3>2. Accelerated Knowledge Sharing</h3><p>Mob programming creates a continuous learning environment. Junior developers learn directly from seniors, while specialists share domain knowledge across the team. According to research by Wang and Manos (2023), mob programming stands out in <em>“promoting workplace diversity by enhancing the participation and contribution of team members of various skill levels.”</em></p><p>I’ve witnessed new team members achieve in weeks what previously took months, simply by being immersed in the collective problem-solving process. Knowledge transfer occurs organically, without the need for formal training sessions.</p><h3>3. Improved Team Dynamics</h3><p>Working closely together builds trust and communication. As one developer from Ståhl and Mårtensson’s study noted: <em>“Previously we were a group of people… We call it a team, but it is really a financial construction, a number of employees who share a room.”</em> Mob programming transformed this dynamic, creating <em>“a group of friends programming together.”</em></p><p>One of my teams went from barely communicating to voluntarily gathering for lunch discussions about architectural improvements. The transformation in team cohesion was remarkable.</p><h3>4. Reduced Delivery Risk</h3><p>When knowledge is shared across the team, you eliminate key person dependencies. One practitioner reported that <em>“the mob is never stuck, you are always moving forward,”</em> while another emphasized that <em>“vulnerability to unplanned absence of key individuals was lessened.”</em></p><p>The Wang and Manos study (2023) quantified this effect, reporting that teams practicing mob programming demonstrated 37% faster problem resolution times compared to solo programming approaches, particularly for complex issues.</p><h3>Remote Mob Programming: Breaking Down Geographic Barriers</h3><p>Remote mob programming isn’t just possible — it’s proven to be remarkably effective. Research confirms this: the team studied by Ståhl and Mårtensson transitioned to remote mob programming during the COVID-19 pandemic and reported that it “works remarkably well.”</p><p>When I helped transition a distributed team to remote mob programming, I was skeptical it would work for everyone. To my surprise, after overcoming initial technical hurdles, the team reported even stronger collaboration than they had experienced whith pairing.</p><h3>Key Elements for Remote Mob Success</h3><p><strong>Robust Technical Infrastructure</strong> — Low-latency connections and proper tools are essential. The studied team found that standard screen sharing wasn’t sufficient for their needs. Instead, they used <em>“a concurrent editor on a shared remote host in the company’s internal cloud.”</em></p><p><strong>Clear Communication Protocols</strong> — Without physical cues, explicit turn-taking and active listening become even more important. Use visual cues or dedicated chat channels to indicate when someone wants to contribute.</p><p><strong>Dedicated Tooling</strong> — Leverage tools designed specifically for remote collaboration such as:</p><ul><li>Mob.sh for seamless driver handovers</li><li>VSCode Live Share or JetBrains Code With Me for real-time collaboration</li><li>Digital whiteboards for design discussions</li></ul><p><strong>Camera Etiquette</strong> — While constantly showing all faces can be bandwidth-intensive, strategic use of video helps maintain engagement. One team member noted that <em>“turning on the cameras might help”</em> with the tendency to <em>“zone out”</em> during remote sessions.</p><p><strong>Regular Check-ins</strong> — Brief, regular check-ins help maintain focus and engagement. This addresses the observation that it’s <em>“much easier to ‘zone out’ and lose focus while working remotely.”</em></p><p>Remote mob programming may increase inclusivity — some team members who participated less in office settings found it <em>“easier to join remote mob sessions,”</em> while others had the opposite experience. This suggests that offering both modes might maximize team-wide participation.</p><h3>When to Use Mob Programming</h3><p>Mob programming isn’t suitable for every task. Based on research findings and my personal experience implementing this practice, here’s when to implement it:</p><p><strong>Best for:</strong></p><ul><li>Complex tasks requiring multiple domains of expertise</li><li>Architectural design work</li><li>Challenging problems with no clear solution</li><li>Onboarding new team members</li><li>Breaking down and planning large features</li></ul><p><strong>Less suitable for:</strong></p><ul><li>Simple, routine tasks</li><li>Customer support (reactive work)</li><li>Individual code reviews</li><li>Tasks where deep, uninterrupted focus is required</li></ul><p>One team in the research noted that small, trivial, and contained tasks were “unanimously regarded as inefficient to handle as a mob.” This aligns with my experience — we gradually learned to be selective about which tasks warranted the entire team’s attention.</p><h3>Comparing Programming Approaches</h3><p>Aspect Solo Programming Pair Programming Mob Programming Quality Varies based on individual skill Higher than solo Highest quality, fewest bugs Knowledge sharing Limited Good between pairs Excellent across entire team Learning curve Low Moderate Steeper initial adjustment Productivity Initially fastest for simple tasks Balanced Most efficient for complex tasks Team cohesion Limited impact Improves pairs’ relationships Strongest team building Remote effectiveness High Moderate Surprisingly high with proper tools</p><h3>Practical Implementation Tips</h3><p>Based on my experience introducing mob programming to reluctant teams:</p><p><strong>Start small</strong> — Begin with just a few hours per week before scaling up. I’ve found that starting with a single two-hour session per week allows skeptics to see value without feeling overwhelmed.</p><p><strong>Select the right challenge</strong> — Begin with a complex, interesting problem that would benefit from diverse perspectives. Early wins build momentum.</p><p><strong>Rotate roles frequently</strong> — One team found success with a formula of “ten minutes minus the number of participants.” In my experience, shorter rotations (10–20 minutes) work best when teams are learning.</p><p><strong>Create a comfortable space</strong> — For remote teams, ensure everyone has proper equipment and connectivity. For in-person mobs, a dedicated space with a large screen and comfortable seating makes a significant difference.</p><p><strong>Implement regular breaks</strong> — The most successful implementation I led used 40-minute work sessions with 5-minute breaks, which prevented the mental fatigue many initially feared.</p><p><strong>Respect individual differences</strong> — Allow team members to participate at levels they’re comfortable with. I’ve found that forcing participation only breeds resentment.</p><p><strong>Hold daily retrospectives</strong> — One team found these served as “pressure valves,” preventing tension buildup. Even a quick 5-minute check-in at the end of a session can identify emerging issues before they become problems.</p><h3>Addressing Common Concerns</h3><h4>“Isn’t it inefficient to have everyone working on one task?”</h4><p>This was the first objection I faced when introducing mob programming. While intuitive, this concern doesn’t hold up in practice. As one practitioner noted, <em>“the mob could only do one thing at a time, but did it fast and did it well and truly done.”</em> With all necessary expertise present, work flows more smoothly without handoffs or waiting for input.</p><p>Wang and Manos (2023) quantified this, showing that while initial development might take slightly longer in a mob (about 15% more time), the overall project delivery was 28% faster due to fewer defects, less rework, and elimination of knowledge transfer delays.</p><h4>“Won’t some team members find it exhausting?”</h4><p>Research shows experiences vary widely. While some report being <em>“completely exhausted”</em> after mob sessions, others find it <em>“fairly relaxing.”</em> The key is implementing regular breaks and allowing flexible participation based on individual preferences.</p><p>In my teams, we found that clear time boundaries and regular breaks made the difference between sustainable practice and burnout.</p><h4>“Can remote mob programming really work as well as in-person?”</h4><p>The evidence suggests yes. Teams report that remote mob programming <em>“works remarkably well”</em> with the right setup. Some participants even find it easier to engage remotely than in person. The key is proper tooling and communication protocols.</p><p>My experience confirms this — after initial technical challenges, remote mob sessions became just as productive as in-person ones, with the added benefit of team members being able to work from their own ergonomic setups.</p><h3>Conclusion</h3><p>Mob programming represents a significant shift in how teams approach software development, whether co-located or distributed across the globe. While it may not be suitable for every task or team member, its benefits for quality, knowledge sharing, and team cohesion make it a valuable addition to your development practices.</p><p>When I first began advocating for mob programming, I faced significant organizational resistance. <em>“We can’t afford to have five developers working on a single task,”</em> was the common refrain. Today, those same organizations can’t afford not to use mob programming for their most complex challenges. The metrics speak for themselves: faster delivery, fewer defects, and more engaged teams.</p><p>By starting small, focusing on complex tasks, and creating a supportive environment that respects individual differences, you can unlock the full potential of your team’s collective intelligence — regardless of physical location. The research is clear: when implemented thoughtfully, mob programming delivers better code, stronger teams, and more sustainable development practices for both in-office and remote teams.</p><p>Will your team be next to experience the benefits of mob programming?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1ee5bd0b776" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Add Tailwind to a project created with DJPoe boilerplate.]]></title>
            <link>https://medium.com/@zalun/add-tailwind-to-a-project-created-with-djpoe-boilerplate-3a4cb11a5334?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a4cb11a5334</guid>
            <category><![CDATA[tailwind]]></category>
            <category><![CDATA[tailwind-css-integration]]></category>
            <category><![CDATA[tailwind-css]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Sun, 13 Oct 2024 11:53:23 GMT</pubDate>
            <atom:updated>2024-10-13T11:59:40.678Z</atom:updated>
            <content:encoded><![CDATA[<p>In developing the <a href="https://djpoe.com/">DJPoe</a> boilerplate, I deliberately left it framework-agnostic regarding the front end. This decision wasn’t an oversight but a recognition that each project has unique needs. However, in today’s web development landscape, I also acknowledge that integrating a CSS framework is not just an option but a necessity for most projects.</p><p><a href="https://tailwindcss.com/">Tailwind CSS</a> has emerged as a powerful contender in this space.</p><p>While the DJPoe boilerplate provides a solid foundation for backend development, incorporating Tailwind can significantly enhance the frontend workflow. In my experience, this combination allows for rapid prototyping, consistent styling, and easier maintenance — all crucial factors in delivering high-quality web applications efficiently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GMTk7Bjek3CU0kRyvpyUuw.jpeg" /><figcaption>Microsoft Designer hallucinating the article title</figcaption></figure><h3>Install new project from DJPoe</h3><p>This time, we will not update the boilerplate project. Instead, we will create a fresh project from DJPoe. Copy and paste the installation CLI line from <a href="https://djpoe.com/">https://djpoe.com/</a>.</p><pre>Projects ➜ sh &lt;(curl -sSL https://raw.githubusercontent.com/zalun/djpoe/refs/heads/main/scripts/install.sh)<br>Cloning into &#39;djpoe-installation&#39;...<br>...<br>Creating virtualenv djpoe in /Users/zalun/Projects/djpoe-installation/.venv<br>Installing dependencies from lock file<br>Installing the current project: djpoe (0.1.0)<br>Enter project name: Tail CSS<br>Enter author name: Piotr Zalewa<br>Enter author email: sample@example.com<br>Enter git repository URL: .<br>Enter the main app name [tail_css]: <br><br>You requested to morph DJPoe into the following project:<br>Project name: Tail CSS<br>App name: tail_css<br>Author name: Piotr Zalewa<br>Author email: sample@example.com<br>Git repository: .<br>Do you want to proceed? [y/N]: y<br>Do you want to push the changes to the remote repository? [y/N]: n<br><br>Project customized successfully<br>Projects ➜ cd tail_css<br>Projects ➜ poe manage loaddata homepage<br>Installed 4 object(s) from 1 fixture(s)<br>Projects ➜ poe test<br>rootdir: /Users/zalun/Projects/tail_css<br>configfile: pytest.ini<br>plugins: cov-5.0.0, django-4.9.0<br>collected 6 items<br><br>tests/test_accounts_pages.py .                                         [ 16%]<br>tests/test_admin_page.py ...                                           [ 66%]<br>tests/test_auth_models_user.py .                                       [ 83%]<br>tests/test_home.py .                                                   [100%]<br><br>============================= 6 passed in 1.29s =============================</pre><h3>Install django-tailwind with auto reload feature.</h3><p>We will use the <a href="https://django-tailwind.readthedocs.io/en/latest/">django-tailwind</a> created by <a href="https://github.com/timonweb">Tim Kamanin</a>. We will follow the <a href="https://django-tailwind.readthedocs.io/en/latest/installation.html">installation documentation</a>.</p><pre>tail_css ➜ poetry add &quot;django-tailwind[reload]&quot;<br>Package operations: 2 installs, 0 updates, 0 removals<br><br>  • Installing django-browser-reload (1.16.0)<br>  • Installing django-tailwind (3.8.0)</pre><p>Configure:</p><pre># tail_css/tail_css/settings.py<br># ...<br>INSTALLED_APPS = [<br>  &quot;tailwind&quot;,<br>  &quot;home&quot;,<br>  # ...<br>]</pre><p>Create the theme app (default app name for tailwind setup)</p><pre>tail_css ➜ poe manage tailwind init<br>Successfully installed Jinja2-3.1.4 MarkupSafe-3.0.1 arrow-1.3.0 binaryornot-0.4.4 chardet-5.2.0 cookiecutter-2.6.0 markdown-it-py-3.0.0 mdurl-0.1.2 pygments-2.18.0 python-slugify-8.0.4 pyyaml-6.0.2 rich-13.9.2 text-unidecode-1.3 types-python-dateutil-2.9.0.20241003<br>  [1/1] app_name (theme): <br>Tailwind application &#39;theme&#39; has been successfully created. Please add &#39;theme&#39; to INSTALLED_APPS in settings.py, then run the following command to install Tailwind CSS dependencies: `python manage.py tailwind install`</pre><p>Move theme directory to tails_css. This is required because of the modified Django directory structure due to using the Poetry.</p><pre>tail_css ➜ mv theme tail_css</pre><p>Add the theme app to the INSTALLED_APPS and pass the name into the config</p><pre># tail_css/tail_css/settings.py<br># ...<br>INSTALLED_APPS = [<br>  &quot;tailwind&quot;,<br>  &quot;theme&quot;,<br>  &quot;home&quot;,<br>  # ...<br>]<br># ...<br>TAILWIND_APP_NAME = &quot;theme&quot;</pre><p>And, finally, install :</p><pre>tail_css ➜ poe manage tailwind install<br><br>added 126 packages, and audited 127 packages in 15s<br><br>31 packages are looking for funding<br>  run `npm fund` for details<br><br>found 0 vulnerabilities</pre><p>The theme directory contains the base.html template that can be used as a test if Tailwind is working correctly.</p><h4>Automatic reload</h4><p>To enable automatic reload, we need to add django_browser_reload to the apps and its BrowserReloadMiddleware to the list of middleware. The reload works only when DEBUG is True.</p><pre># tail_css/tails_css/settings.py<br>#...<br><br>INSTALLED_APPS: Final = [<br>    # django-tailwind<br>    &quot;tailwind&quot;,<br>    &quot;theme&quot;,<br>    &quot;django_browser_reload&quot;,<br>    # ...<br>]<br># ...<br>MIDDLEWARE = [<br>    # ...<br>    &quot;wagtail.contrib.redirects.middleware.RedirectMiddleware&quot;,<br>    &quot;django_browser_reload.middleware.BrowserReloadMiddleware&quot;,<br>    # ...<br>]</pre><p>The last part is to add the reload URL to the ursl.py file.</p><pre># tail_css/tail_css/urls.py<br># ...<br>urlpatterns = [<br>    # ...,<br>    path(&quot;__reload__/&quot;, include(&quot;django_browser_reload.urls&quot;)),<br>]</pre><h4>Running tailwind dev server</h4><blockquote>Tailwind CSS works by scanning all of your HTML files, JavaScript components, and any other templates for class names, generating the corresponding styles and then writing them to a static CSS file.</blockquote><p>We must run a background process to render Tailwind CSS classes for development automatically. The manage command is via manage.py</p><pre>tail_css ➜ poetry run python tail_css/manage.py tailwind start</pre><p>We can use poe manage to shorten it:</p><pre>tail_css ➜ poe manage tailwind start</pre><p>But we will create a new command poe tailwind to further shorten it.</p><pre># pyproject.toml<br># ...<br>[tool.poe.tasks.tailwind]<br>cmd = &quot;python ./family_roots/manage.py tailwind&quot;</pre><pre>tail_css ➜ poe tailwind start<br>&gt; theme@3.8.0 start<br>&gt; npm run dev<br><br>&gt; theme@3.8.0 dev<br>&gt; cross-env NODE_ENV=development tailwindcss --postcss -i ./src/styles.css -o ../static/css/dist/styles.css -w<br><br>Rebuilding...<br>Done in 255ms.</pre><p>A new file is created in tail_css/theme/static/css/styles.css. It contains the CSS definitions used in the theme/templates/base.html</p><h3>Add styles to HomePage</h3><p>Let’s check if Tailwind is working — for a brief moment, copy the contents of the theme/templates/base.html to the home/templates/home/home_page.html. Start the dev server with poe dev. Everything is working fine if the result is like the image below centered on the screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UKvNLOv3-VyCevq2oyPRqg.png" /></figure><p>Configure django-tailwind in theme/statit_src/tailwind.config.js. The tailwind node script reads all the app templates and the main template dir by default. You can’t find the “full configuration” on the https://unpkg.com site despite what&#39;s written in the file. Just go to the <a href="https://tailwindcss.com/docs/configuration">TailwindCSS documentation</a>.</p><p>I will leave it alone. I will not do a perfect design here. I’ve replaced the “header” created in the first few lines to the:</p><pre>    &lt;header class=&quot;bg-cyan-600 text-white p-4 text-right&quot;&gt;<br>        &lt;div class=&quot;container mx-auto&quot;&gt;<br>            {% if user.is_authenticated %}<br>                &lt;span class=&quot;font-bold&quot;&gt;{% translate &quot;Welcome&quot; %}, {{ user.username }}!&lt;/span&gt;<br>                &lt;a href=&quot;{% url &#39;account_logout&#39; %}&quot; class=&quot;ml-4 underline&quot;&gt;Logout&lt;/a&gt;<br>            {% else %}<br>                &lt;span class=&quot;font-bold&quot;&gt;{% translate &quot;Welcome, guest!&quot; %}&lt;/span&gt;<br>                &lt;a href=&quot;{% url &#39;account_login&#39; %}&quot; class=&quot;ml-4 underline&quot;&gt;Login&lt;/a&gt;<br>                &lt;a href=&quot;{% url &#39;account_signup&#39; %}&quot; class=&quot;ml-4 underline&quot;&gt;Sign Up&lt;/a&gt;<br>            {% endif %}<br>        &lt;/div&gt;<br>    &lt;/header&gt;</pre><p>And we’ve got a nicer header.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z_jNUkc5caw9_jldEUZIVQ.png" /></figure><p>It’s easy to add the basic styling for the parts rendered with the richtext template. The typography-plugin is already installed with django-tailwind, and it’s just a matter of adding the div tag wrapping the page.body</p><pre>    &lt;div class=&quot;prose lg:prose-lg prose-a:text-blue-600 prose-headings:text-red-800 py-10 px-20&quot;&gt;<br>        {{ page.body|richtext }}<br>    &lt;/div&gt;</pre><p>The prose switches on the typography plugin, and the rest is for styling inside the div.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SCw0sJSyoClApnuHf7BheQ.png" /></figure><h3>Publish</h3><p>Right now, the CSS is created only for the development process. Run build before committing the files to the repository.</p><pre>tail_css ➜ poe tailwind build</pre><h3>Conclusion</h3><p>In conclusion, integrating Tailwind CSS into a project created with the DJPoe boilerplate is a straightforward process that significantly enhances the front-end development workflow. This combination leverages DJPoe&#39;s robust backend foundation with Tailwind CSS&#39;s powerful and flexible styling capabilities.</p><p>The result is a development environment that allows for rapid prototyping, consistent styling, and easier maintenance of web applications. This approach bridges the gap between backend efficiency and frontend flexibility, enabling developers to create high-quality, responsive web applications more efficiently.</p><h3>Links</h3><ul><li><a href="https://djpoe.com/">https://djpoe.com/</a></li><li><a href="https://tailwindcss.com/">https://tailwindcss.com/</a></li><li><a href="https://django-tailwind.readthedocs.io/en/latest/">https://django-tailwind.readthedocs.io/en/latest/</a></li><li><a href="https://github.com/timonweb">https://github.com/timonweb</a></li><li><a href="https://tailwindcss.com/docs/configuration">https://tailwindcss.com/docs/configuration</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a4cb11a5334" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Customizing DJPoe: A Script for Rapid Django Project Initialization]]></title>
            <link>https://medium.com/@zalun/customizing-djpoe-a-script-for-rapid-django-project-initialization-bb897eaf4bd7?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb897eaf4bd7</guid>
            <category><![CDATA[shell-script]]></category>
            <category><![CDATA[shell]]></category>
            <category><![CDATA[installation]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Wed, 09 Oct 2024 15:45:51 GMT</pubDate>
            <atom:updated>2024-10-10T07:20:05.125Z</atom:updated>
            <content:encoded><![CDATA[<p>As developers, we often find ourselves starting new side projects. While boilerplates like <a href="https://djpoe.com/">DJPoe</a> provide an excellent foundation, they still require manual customization to fit each new project’s needs. This process can be time-consuming and error-prone, especially when renaming files and updating references across the project. To streamline this process and allow developers to quickly jump into building their unique features, I’ve created a script that automates the customization of the DJPoe boilerplate. This tool will rename directories, update file contents, and even push to the prepared empty Git repository, all based on user input. By automating these initial steps, we can significantly reduce the time and effort required to start a new Django project, allowing developers to focus on what truly matters — bringing their ideas to life.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zhicbnQIdpYBd9NnX8-uyw.jpeg" /><figcaption>Microsoft Designer hallucinating the title</figcaption></figure><p>We will create a Python script to morph DJPoe into a new project, create a shell script to prepare it to execute, and place it on the repository for an easy one-liner installation command.</p><p>We must ask the user for strings representing the new project to achieve the goal. We can limit them to the project name, author name, email, and the Git repository. After that, we will rename the djpoe directory, replace the strings in the files, and hope it all works well.</p><h3>Collect new project data</h3><p>For command line arguments, we will use <a href="https://palletsprojects.com/projects/click/">click</a>.</p><pre>djpoe ➜ poetry update<br>djpoe ➜ poetry add --group dev click<br>djpoe ➜ poe requirements</pre><p>Let’s create a customize.py file in the scripts directory. We will now ask for input and provide the ability to add it via CLI arguments.</p><pre>djpoe ➜ mkdir scripts<br>djpoe ➜ touch scripts/__init__.py</pre><pre># scripts/customize.py<br>import re<br>from dataclasses import dataclass<br><br>import click<br><br><br>@dataclass<br>class ProjectConfig:<br>    &quot;&quot;&quot;Project configuration.&quot;&quot;&quot;<br><br>    project_name: str<br>    app_name: str<br>    author_name: str<br>    author_email: str<br>    git_repo: str<br><br><br>@click.command()<br>@click.option(&quot;--project-name&quot;, prompt=&quot;Enter project name&quot;, help=&quot;Name of the project&quot;)<br>@click.option(&quot;--author-name&quot;, prompt=&quot;Enter author name&quot;, help=&quot;Name of the author&quot;)<br>@click.option(&quot;--author-email&quot;, prompt=&quot;Enter author email&quot;, help=&quot;Email of the author&quot;)<br>@click.option(&quot;--git-repo&quot;, prompt=&quot;Enter git repository URL&quot;, help=&quot;URL of the git repository&quot;)<br>def customize_project(project_name: str, author_name: str, author_email: str, git_repo: str) -&gt; None:<br>    &quot;&quot;&quot;Customize the djpoe project and push to a new repository.&quot;&quot;&quot;<br>    app_name = re.sub(r&quot;[^a-zA-Z0-9]+&quot;, &quot;_&quot;, project_name).lower()<br>    app_name = click.prompt(&quot;Enter the main app name&quot;, type=str, default=app_name)<br><br>    config = ProjectConfig(project_name, app_name, author_name, author_email, git_repo)<br>    print(config)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    customize_project()  # pylint: disable=no-value-for-parameter</pre><p>We decorate the customize_project function with a series of optional arguments. We’ve added prompt to the decorator, so we will be asked to provide the value. We can call the command without any argument.</p><pre>djpoe ➜ poetry run python scripts/customize.py<br>Enter project name: Entirely New Project<br>Enter author name: Piotr Zalewa<br>Enter author email: email@example.com<br>Enter git repository URL: https://github.com/zalun/new-project-repository<br>Enter the main app name [entirely_new_project]: <br>ProjectConfig(project_name=&#39;Entirely New Project&#39;, app_name=&#39;entirely_new_project&#39;, author_name=&#39;Piotr Zalewa&#39;, author_email=&#39;email@example.com&#39;, git_repo=&#39;https://github.com/zalun/new-project-repository&#39;)</pre><p>The default app_name is created from project_name by filtering out all non-alphanumeric characters and replacing spaces with an underscore.</p><h3>Logging</h3><p>The process will have multiple stages. It’s good to be chatty in debug mode. I like the responses to be colored. We will use the <a href="https://github.com/tartley/colorama">Colorama</a> to be consistent with colors on MS Windows.</p><pre>djpoe ➜ poetry install --group dev colorama<br>djpoe ➜ poe requirements</pre><pre># scripts/logging.py<br>&quot;&quot;&quot;Logging utils.&quot;&quot;&quot;<br><br>import logging<br>from typing import Final<br><br>from colorama import Fore, Style, init<br><br># Initialize colorama<br>init(autoreset=True)<br><br>COLORS: Final[dict[str, str]] = {<br>    &quot;DEBUG&quot;: Fore.LIGHTBLACK_EX,<br>    &quot;INFO&quot;: &quot;&quot;,<br>    &quot;WARNING&quot;: Fore.YELLOW,<br>    &quot;ERROR&quot;: Fore.RED,<br>    &quot;CRITICAL&quot;: Fore.RED + Style.BRIGHT,<br>}<br><br><br>class ColoredFormatter(logging.Formatter):<br>    &quot;&quot;&quot;Custom formatter to add colors to log messages.&quot;&quot;&quot;<br><br>    def format(self: &quot;ColoredFormatter&quot;, record: logging.LogRecord) -&gt; str:<br>        &quot;&quot;&quot;Format the log message with color.&quot;&quot;&quot;<br>        log_message = super().format(record)<br>        return f&quot;{COLORS.get(record.levelname, &#39;&#39;)}{log_message}{Style.RESET_ALL}&quot;<br><br><br># Set up logging<br>logger = logging.getLogger(__name__)<br>logger.setLevel(logging.INFO)<br><br># Create console handler<br>handler = logging.StreamHandler()<br><br># Create formatter<br>formatter = ColoredFormatter(&quot;%(message)s&quot;)<br><br># Add formatter to the handler<br>handler.setFormatter(formatter)<br><br># Add handler to logger<br>logger.addHandler(handler)</pre><p>We will use logger.info() instead of print:</p><pre># scripts/customize.py<br>from scripts.logger import logger<br># ...<br>  logger.debug(config)</pre><p>I like to print out the settings and ask for confirmation, as changes might be complex to recover from. I will also add the --debug to print out the debug on request only. Later on, I realized that, at least for testing purposes, it would be good to have a --yes and --default flags to automatically accept all prompts and confirmations.</p><pre># scripts/customize.py<br>&quot;&quot;&quot;A Script for Rapid Django Project Initialization.&quot;&quot;&quot;<br><br>import logging<br>import re<br>from dataclasses import dataclass<br>from pathlib import Path<br><br>import click<br><br>from scripts.logger import logger<br><br><br>@dataclass<br>class ProjectConfig:<br>    &quot;&quot;&quot;Project configuration.&quot;&quot;&quot;<br><br>    project_name: str<br>    app_name: str<br>    author_name: str<br>    author_email: str<br>    git_repo: str<br><br>    def to_str(self: &quot;ProjectConfig&quot;) -&gt; str:<br>        &quot;&quot;&quot;Return a string representation of the project configuration.&quot;&quot;&quot;<br>        return (<br>            f&quot;Project name: {self.project_name}\nApp name: {self.app_name}\nAuthor name: {self.author_name}\n&quot;<br>            f&quot;Author email: {self.author_email}\nGit repository: {self.git_repo}&quot;<br>        )<br><br><br>@click.command()<br>@click.option(&quot;--debug&quot;, is_flag=True, help=&quot;Enable debug mode&quot;)<br>@click.option(&quot;--default&quot;, is_flag=True, help=&quot;Do not ask for generated names&quot;)<br>@click.option(&quot;--yes&quot;, is_flag=True, help=&quot;Approve all prompts&quot;)<br>@click.option(&quot;--project-name&quot;, prompt=&quot;Enter project name&quot;, help=&quot;Name of the project&quot;)<br>@click.option(&quot;--author-name&quot;, prompt=&quot;Enter author name&quot;, help=&quot;Name of the author&quot;)<br>@click.option(&quot;--author-email&quot;, prompt=&quot;Enter author email&quot;, help=&quot;Email of the author&quot;)<br>@click.option(&quot;--git-repo&quot;, prompt=&quot;Enter git repository URL&quot;, help=&quot;URL of the git repository&quot;)<br>def customize_project(<br>    project_name: str, author_name: str, author_email: str, git_repo: str, debug: bool, default: bool, yes: bool<br>) -&gt; None:<br>    &quot;&quot;&quot;Customize the djpoe project and push to a new repository.&quot;&quot;&quot;<br>    if debug:<br>        logger.setLevel(logging.DEBUG)<br><br>    app_name = re.sub(r&quot;[^a-zA-Z0-9]+&quot;, &quot;_&quot;, project_name).lower()<br>    if not default:<br>        app_name = click.prompt(&quot;Enter the main app name&quot;, type=str, default=app_name)<br><br>    config = ProjectConfig(project_name, app_name, author_name, author_email, git_repo)<br><br>    logger.debug(&quot;Project configuration: %s&quot;, config)<br>    if not yes:<br>        click.confirm(<br>            f&quot;\nYou requested to morph DJPoe into the following project:\n{config.to_str()}\nDo you want to proceed?&quot;,<br>            abort=True,<br>        )<br><br>    logger.debug(&quot;Renaming directory djpoe to %s&quot;, app_name)<br>    Path(&quot;djpoe&quot;).rename(app_name)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    customize_project()  # pylint: disable=no-value-for-parameter</pre><p>The command is a little mouthful. We will create a poe task for it.</p><pre># pyproject.toml<br># ...<br>[tool.poe.tasks.customize]<br>cmd = &quot;python ./scripts/customize.py&quot;</pre><pre>djpoe ➜ poe customize --debug --project-name &quot;Entirely New Project&quot; --author-name &quot;Piotr Zalewa&quot; --author-email &quot;email@example.com&quot; --git-repo https://github.com/zalun/djpoe<br>Poe =&gt; python ./scripts/customize.py --debug --project-name &#39;Entirely New Project&#39; --author-name &#39;Piotr Zalewa&#39; --author-email email@example.com --git-repo https://github.com/zalun/djpoe<br>Enter the main app name [entirely_new_project]: <br>Project configuration: ProjectConfig(project_name=&#39;Entirely New Project&#39;, app_name=&#39;entirely_new_project&#39;, author_name=&#39;Piotr Zalewa&#39;, author_email=&#39;email@example.com&#39;, git_repo=&#39;https://github.com/zalun/djpoe&#39;)<br><br>You requested to morph DJPoe into the following project:<br>Project name: Entirely New Project<br>App name: entirely_new_project<br>Author name: Piotr Zalewa<br>Author email: email@example.com<br>Git repository: https://github.com/zalun/djpoe<br>Do you want to proceed? [y/N]: <br>Aborted!</pre><h3>Remove the git history.</h3><p>I just realized I’d replace everything in the directory that I want later to push to my repository… I will create a bash line to work on a copy.</p><pre>djpoe ➜ cd .. &amp;&amp; \<br>rm -rf djpoe_testing &amp;&amp; \<br>cp -r djpoe djpoe_testing &amp;&amp; \<br>cd djpoe_testing &amp;&amp; \<br>poe customize --project-name &quot;Entirely New Project&quot; --author-name &quot;John Doe&quot; --author-email &quot;some@email.com&quot; --git-repo https://github.com/johndoe/new_project --debug --default --yes</pre><p>I need to remember to edit in the correct directory…</p><p>We need to check if the .git directory exists. If so, it needs to be removed.</p><pre># scripts/customize.py<br># ...<br>def remove_git_repo() -&gt; None:<br>    &quot;&quot;&quot;Remove the git repository.&quot;&quot;&quot;<br>    git_dir = Path(&quot;.git&quot;)<br>    if git_dir.is_dir():<br>        logger.debug(&quot;Removing git repository&quot;)<br>        git_dir.rmdir()<br># ...<br>def customize_project(<br># ...<br>    remove_git_repo()</pre><h3>Rename the directory.</h3><p>We will use the native Pathlib module to handle all file operations.</p><pre># scripts/customize.py<br># ...<br>def rename_directories(app_name: str, debug: bool) -&gt; None:<br>    &quot;&quot;&quot;Rename the directory djpoe to the app name.&quot;&quot;&quot;<br>    logger.debug(&quot;Renaming directory djpoe to %s&quot;, app_name)<br>    try:<br>        Path(&quot;djpoe/djpoe&quot;).rename(f&quot;djpoe/{app_name}&quot;)<br>        Path(&quot;djpoe&quot;).rename(app_name)<br>    except FileNotFoundError as e:<br>        logger.error(&quot;Failed to rename directory: %s&quot;, e)<br>        if debug:<br>            raise<br><br>        sys.exit(1)<br># ...<br>def customize_project(<br># ...<br>    rename_directories(app_name, debug)</pre><p>After running the script with</p><pre>djpoe ➜ poe customize \<br>  --project-name &quot;Entirely New Project&quot; \<br>  --author-name &quot;John Doe&quot; \<br>  --author-email &quot;some@email.com&quot; \<br>  --git-repo https://github.com/johndoe/new_project \<br>  --debug \<br>  --default \<br>  --yes</pre><p>The djpoe directory is replaced with entirely_new_project.</p><h3>Replace the strings.</h3><p>Let&#39;s find all the files containing the strings we want to replace.</p><pre>djpoe ➜ find * -type f -exec grep -li -e &quot;email@example.com&quot; -e &quot;piotr&quot; -e &quot;djpoe&quot; {} +<br>README.md<br>djpoe/home/models.py<br>djpoe/home/templates/home/home_page.html<br>djpoe/home/fixtures/homepage.json<br>djpoe/db.sqlite3<br>djpoe/manage.py<br>djpoe/djpoe/asgi.py<br>djpoe/djpoe/settings.py<br>djpoe/djpoe/urls.py<br>djpoe/djpoe/wsgi.py<br>pyproject.toml<br>pytest.ini<br>scripts/customize.py<br>tests/test_home.py</pre><p>We want to replace the strings in all these files. Except of README.md, fixtures, *.html, and SQLite files. I thought it would be OK to hard-code this list, but then I realized it would be a mistake. If a new file is added to the DJPoe project, it won’t be included. Let’s search for the files to replace in Python.</p><pre># scripts/customize.py<br># ...<br>def find_files_with_strings(<br>    directory: str,<br>    exclude_dirs: list[str],<br>    search_strings: list[str],<br>    extensions: list[str],<br>    debug: bool,<br>) -&gt; list[Path]:<br>    &quot;&quot;&quot;Find files with strings in the specified directory.&quot;&quot;&quot;<br>    # Compile a regex pattern to search for the specified strings<br>    pattern = re.compile(&quot;|&quot;.join(map(re.escape, search_strings)), re.IGNORECASE)<br>    matching_files = []<br><br>    # Recursively search for files in the directory<br>    for file_path in Path(directory).rglob(&quot;*&quot;):<br>        # Skip directories and files within directories that are in the exclude_dirs list<br>        if any(exclude_dir in file_path.parts for exclude_dir in exclude_dirs):<br>            continue<br><br>        # Check if the file has the desired extension and contains the search pattern<br>        if file_path.is_file() and file_path.suffix.lower() in extensions:<br>            try:<br>                content = file_path.read_text(encoding=&quot;utf-8&quot;)<br>                if pattern.search(content):<br>                    matching_files.append(file_path)<br>                    if debug:<br>                        logger.debug(&quot;Found match in: %s&quot;, file_path)<br>            except OSError as e:<br>                logger.error(&quot;Failed to read %s: %s&quot;, file_path, e)<br>                if debug:<br>                    logger.exception(&quot;Detailed error information:&quot;)<br><br>    return matching_files<br><br># ...<br>def customize_project(<br># ...<br>    find_files_with_strings(<br>        &quot;.&quot;,<br>        [&quot;.venv&quot;, &quot;.git&quot;, &quot;scripts&quot;, &quot;.mypy_cache&quot;, &quot;.pytest_cache&quot;],<br>        [&quot;djpoe&quot;, &quot;piotr&quot;, &quot;email@example.com&quot;],<br>        [&quot;.py&quot;, &quot;.md&quot;, &quot;.ini&quot;],<br>        debug,<br>    )</pre><p>Will print the following debug messages:</p><pre>Found match in: pytest.ini<br>Found match in: README.md<br>Found match in: tests/test_home.py<br>Found match in: entirely_new_project/manage.py<br>Found match in: entirely_new_project/home/models.py<br>Found match in: entirely_new_project/entirely_new_project/asgi.py<br>Found match in: entirely_new_project/entirely_new_project/settings.py<br>Found match in: entirely_new_project/entirely_new_project/urls.py<br>Found match in: entirely_new_project/entirely_new_project/wsgi.py</pre><p>Now, we need to replace the strings in the files from the list.</p><pre># scripts/customize.py<br># ...<br>def replace_strings(<br>    project_name: str,<br>    app_name: str,<br>    author_name: str,<br>    author_email: str,<br>    debug: bool,<br>) -&gt; None:<br>    &quot;&quot;&quot;Replace strings in the specified files based on the project configuration.&quot;&quot;&quot;<br>    logger.debug(&quot;Replacing strings in the specified files based on the project configuration.&quot;)<br><br>    # Dictionary mapping old strings to new values<br>    replace_dict: dict[str, str] = {<br>        &quot;DJPoe&quot;: project_name,<br>        &quot;djpoe&quot;: app_name,<br>        &quot;Piotr Zalewa&quot;: author_name,<br>        &quot;email@example.com&quot;: author_email,<br>    }<br><br>    # Find files that contain specific strings and have certain extensions<br>    files_to_replace = find_files_with_strings(<br>        &quot;.&quot;,<br>        [&quot;.venv&quot;, &quot;.git&quot;, &quot;scripts&quot;, &quot;.mypy_cache&quot;, &quot;.pytest_cache&quot;],<br>        [&quot;djpoe&quot;, &quot;piotr&quot;, &quot;email@example.com&quot;],<br>        [&quot;.py&quot;, &quot;.md&quot;, &quot;.ini&quot;, &quot;.yml&quot;],<br>        debug,<br>    )<br><br>    # Replace strings in each file<br>    for file_path in files_to_replace:<br>        if not file_path.exists():<br>            logger.warning(&quot;File not found: %s&quot;, file_path)<br>            continue<br><br>        try:<br>            content = file_path.read_text(encoding=&quot;utf-8&quot;)<br>            for old, new in replace_dict.items():<br>                content = content.replace(old, new)<br>            file_path.write_text(content, encoding=&quot;utf-8&quot;)<br>            logger.debug(&quot;Replaced strings in %s&quot;, file_path)<br>        except OSError as e:<br>            logger.error(&quot;Failed to replace strings in %s: %s&quot;, file_path, e)<br>            if debug:<br>                raise<br><br># ...<br>def customize_project(<br># ...<br>    replace_strings(project_name, app_name, author_name, author_email, debug)</pre><p>New debug logs:</p><pre>Replaced strings in pytest.ini<br>Replaced strings in README.md<br>Replaced strings in tests/test_home.py<br>Replaced strings in entirely_new_project/manage.py<br>Replaced strings in entirely_new_project/home/models.py<br>Replaced strings in entirely_new_project/entirely_new_project/asgi.py<br>Replaced strings in entirely_new_project/entirely_new_project/settings.py<br>Replaced strings in entirely_new_project/entirely_new_project/urls.py<br>Replaced strings in entirely_new_project/entirely_new_project/wsgi.py</pre><p>Let’s try to see if the new project passes tests. Almost:</p><pre>djpoe ➜ poe test                     <br>Poe =&gt; pytest --showlocals --tb=auto -ra --cov-branch --cov-report=term-missing<br>====================================== test session starts =======================================<br>platform darwin -- Python 3.12.6, pytest-8.3.3, pluggy-1.5.0<br>django: version: 5.1.2, settings: entirely_new_project.settings (from ini)<br>rootdir: /Users/zalun/Projects/djpoe_testing<br>configfile: pytest.ini<br>plugins: cov-5.0.0, django-4.9.0<br>collected 6 items                                                                                <br><br>tests/test_accounts_pages.py .                                                             [ 16%]<br>tests/test_admin_page.py ...                                                               [ 66%]<br>tests/test_auth_models_user.py .                                                           [ 83%]<br>tests/test_home.py F                                                                       [100%]<br><br>============================================ FAILURES ============================================<br>_________________________________________ test_homepage __________________________________________</pre><p>The test_homepage is failing because it searches for DJPoe in the rendered homepage. And I decided not to change the fixture. Let’s add it to the excluded…</p><pre>tests/test_accounts_pages.py .                                     [ 16%]<br>tests/test_admin_page.py ...                                       [ 66%]<br>tests/test_auth_models_user.py .                                   [ 83%]<br>tests/test_home.py .                                               [100%]<br><br>=========================== 6 passed in 1.43s ============================</pre><p>Brilliant! The last bit is to confirm the variables set in the repository and push the code to the djpoe-init branch. Let’s not push to the main one to avoid unwanted deployment.</p><h3>Push the code</h3><p>Assuming the git repository has been provided, we want to initialize git on the new project, create the main branch, create the djpoe-init branch, commit all files, and push to the provided git repository.</p><h4>Initialize the repository</h4><pre>def initialize_repository(git_repo: str, debug: bool) -&gt; None:<br>    &quot;&quot;&quot;Initialize the git repository.&quot;&quot;&quot;<br>    logger.debug(&quot;Initializing git repository: %s&quot;, git_repo)<br>    try:<br>        subprocess.run([&quot;git&quot;, &quot;init&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;remote&quot;, &quot;add&quot;, &quot;origin&quot;, git_repo], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;checkout&quot;, &quot;-b&quot;, &quot;main&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;add&quot;, &quot;README.md&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;commit&quot;, &quot;-m&quot;, &quot;README file from DJPoe&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;checkout&quot;, &quot;-b&quot;, &quot;djpoe-init&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;add&quot;, &quot;.&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;commit&quot;, &quot;-m&quot;, &quot;Initial commit from DJPoe&quot;], check=True)<br>    except subprocess.CalledProcessError as e:<br>        logger.error(&quot;Failed to initialize git repository: %s&quot;, e)<br>        if debug:<br>            raise<br><br># ...<br>def customize_project(<br># ...<br>    if not git_repo:<br>        return<br><br>    initialize_repository(git_repo, debug)</pre><h4>Push to the remote repository</h4><pre>def push_to_repository(git_repo: str, debug: bool) -&gt; None:<br>    &quot;&quot;&quot;Push to the repository.&quot;&quot;&quot;<br>    logger.debug(&quot;Pushing to the repository: %s&quot;, git_repo)<br>    try:<br>        subprocess.run([&quot;git&quot;, &quot;checkout&quot;, &quot;main&quot;, &quot;-f&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;push&quot;, &quot;-u&quot;, &quot;origin&quot;, &quot;main&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;checkout&quot;, &quot;djpoe-init&quot;], check=True)<br>        subprocess.run([&quot;git&quot;, &quot;push&quot;, &quot;-u&quot;, &quot;origin&quot;, &quot;djpoe-init&quot;], check=True)<br>    except subprocess.CalledProcessError as e:<br>        logger.error(&quot;Failed to push to the repository: %s&quot;, e)<br>        if debug:<br>            raise<br># ...<br>def customize_project(<br># ...<br>    initialize_repository(git_repo, debug)<br>    if yes or click.confirm(&quot;Do you want to push the changes to the remote repository?&quot;, abort=True):<br>        push_to_repository(git_repo, debug)<br></pre><h4>Make git less noisy.</h4><pre>def run_git_command(command: list[str], debug: bool) -&gt; str | None:<br>    &quot;&quot;&quot;Run a git command and log the output.&quot;&quot;&quot;<br>    logger.debug(&quot;&gt; %s&quot;, &quot; &quot;.join(command).rstrip(&quot;\n&quot;))<br>    try:<br>        result = subprocess.run(command, check=True, capture_output=True, text=True)<br>        if result.stdout:<br>            logger.debug(result.stdout)<br>    except subprocess.CalledProcessError as e:<br>        logger.error(&quot;Failed to run git command: %s&quot;, e)<br>        if debug:<br>            raise<br><br><br>def initialize_repository(git_repo: str, debug: bool) -&gt; None:<br>    &quot;&quot;&quot;Initialize the git repository.&quot;&quot;&quot;<br>    logger.debug(&quot;Initializing git repository: %s&quot;, git_repo)<br>    run_git_command([&quot;git&quot;, &quot;init&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;remote&quot;, &quot;add&quot;, &quot;origin&quot;, git_repo], debug)<br>    run_git_command([&quot;git&quot;, &quot;checkout&quot;, &quot;-b&quot;, &quot;main&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;add&quot;, &quot;README.md&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;commit&quot;, &quot;-m&quot;, &quot;README file from DJPoe&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;checkout&quot;, &quot;-b&quot;, &quot;djpoe-init&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;add&quot;, &quot;.&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;commit&quot;, &quot;-m&quot;, &quot;Initial commit from DJPoe&quot;], debug)<br><br><br>def push_to_repository(git_repo: str, debug: bool) -&gt; None:<br>    &quot;&quot;&quot;Push to the repository.&quot;&quot;&quot;<br>    logger.debug(&quot;Pushing to the repository: %s&quot;, git_repo)<br>    run_git_command([&quot;git&quot;, &quot;checkout&quot;, &quot;main&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;push&quot;, &quot;-u&quot;, &quot;origin&quot;, &quot;main&quot;, &quot;-f&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;checkout&quot;, &quot;djpoe-init&quot;], debug)<br>    run_git_command([&quot;git&quot;, &quot;push&quot;, &quot;-u&quot;, &quot;origin&quot;, &quot;djpoe-init&quot;], debug)</pre><h3>Final call</h3><p>Let’s call the command without debugging, without assuming default values, and with all confirmations. I’ve created a new empty repository djpoe-new-project.</p><pre>djpoe ➜ poe customize                                                            <br>Poe =&gt; python ./scripts/customize.py<br>Enter project name: Entirely New Project<br>Enter author name: Jane Doe<br>Enter author email: some@email.com<br>Enter git repository URL: https://github.com/zalun/djpoe-new-project<br>Enter the main app name [entirely_new_project]: <br><br>You requested to morph DJPoe into the following project:<br>Project name: Entirely New Project<br>App name: entirely_new_project<br>Author name: Jane Doe<br>Author email: some@email.com<br>Git repository: https://github.com/zalun/djpoe-new-project<br>Do you want to proceed? [y/N]: y<br>Do you want to push the changes to the remote repository? [y/N]: y</pre><p>Let’s see the fresh project in the remote repository:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qGqrKXHLAdk5cmmBoZE_wQ.png" /><figcaption>main branch with just the README.md file</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FsOIfvFSpWClmrR5BACS-g.png" /><figcaption>djpoe-init branch — ready to create a pull request</figcaption></figure><h3>Install script with a shell.</h3><p>I’ve seen these nice copy&amp;paste install scripts on many pages. It would be so cool to have one like that…</p><pre>#!/usr/bin/sh<br># scripts/install.sh<br>export PYTHONUNBUFFERED=1<br><br>if [ -x &quot;poetry&quot; ]; then<br>    pip3 install poetry<br>fi<br><br>git clone https://github.com/zalun/djpoe.git djpoe-installation<br>cd djpoe-installation<br>poetry install<br><br># Use a temporary file to capture the exit status<br>tmp_file=$(mktemp)<br><br># Run the command and capture its output and exit status<br>{<br>    poetry run python3 scripts/customize.py | tee /dev/tty<br>    echo $? &gt;&quot;$tmp_file&quot;<br>} | tail -n 1 &gt;app_name_file<br><br># Read the exit status and app name from files<br>exit_status=$(cat &quot;$tmp_file&quot;)<br>app_name=$(cat app_name_file)<br><br># Clean up temporary files<br>rm -f &quot;$tmp_file&quot; app_name_file<br><br># Check the exit status<br>if [ &quot;$exit_status&quot; = &quot;0&quot; ]; then<br>    echo &quot;Project customized successfully&quot;<br>    cd ..<br>    mv djpoe-installation &quot;$app_name&quot;<br><br>else<br>    echo &quot;Installation failed with exit status $exit_status&quot;<br>fi</pre><p>The script will install poetry if it’s not executable. Then it clones the DJPoe repository to the djpoe-installation directory, and installs dependencies. Then it does a crazy shell scripting (Claude’s fault) to get the value out of a running Python script. The value is the app_name entered during the customization process. If there are no errors, the script will rename the django-installation directory to the $app_name one.</p><p>The final touch is the command to install all of this automatically.</p><pre>djpoe ➜ sh &lt;(curl -sSL https://raw.githubusercontent.com/zalun/djpoe/refs/heads/main/scripts/install.sh)</pre><h3>Links</h3><ul><li><a href="https://github.com/zalun/djpoe">https://github.com/zalun/djpoe</a></li><li><a href="https://palletsprojects.com/projects/click/">https://palletsprojects.com/projects/click/</a></li><li><a href="https://github.com/tartley/colorama">https://github.com/tartley/colorama</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb897eaf4bd7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Base 6 — Wagtail CMS for the content]]></title>
            <link>https://medium.com/@zalun/base-6-wagtail-cms-for-the-content-f60d8ec7ac9e?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/f60d8ec7ac9e</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[boilerplate]]></category>
            <category><![CDATA[wagtail]]></category>
            <category><![CDATA[wagtailcms]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Sun, 29 Sep 2024 12:45:30 GMT</pubDate>
            <atom:updated>2024-10-05T12:10:17.629Z</atom:updated>
            <content:encoded><![CDATA[<h3>Base 6 — Wagtail CMS for the content</h3><p>In this sixth article of our Django boilerplate series, we’re integrating Wagtail CMS to manage our project’s content. While Django’s admin panel is powerful, it often falls short of complex content management needs. Wagtail offers a user-friendly interface that content editors will appreciate, with powerful features like flexible page modeling and excellent image management. Its SEO tools and content workflow capabilities make it an ideal choice for projects that require robust content management beyond what the default Django admin can provide. Adding Wagtail to our boilerplate enhances our project’s functionality and provides a solid foundation for content-driven websites.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eY6vhzdriDxQPNZ_575NVQ.jpeg" /><figcaption>Microsoft designer hallucinating the article’s title</figcaption></figure><p>In the last article, <a href="https://medium.com/django-unleashed/base-5-3rd-party-authentication-in-django-887cad242a38">Base 5–3rd party authentication in Django</a>, we’ve added social sign-in to our boilerplate. The code is on GitHub in the <a href="https://github.com/zalun/djpoe/tree/part-5-auth">part-5-auth branch</a>. We will start from here.</p><h3>Installation and basic configuration</h3><p>By default, <a href="https://docs.wagtail.org/">Wagtail</a> is installed as a project, like Django. We will take the other approach and add it as any other Django application.</p><pre>djpoe ➜ poetry add wagtail<br>djpoe ➜ poe requirements</pre><p>It adds 34 dependency installs.</p><p><a href="https://docs.wagtail.org/en/stable/advanced_topics/add_to_django_project.html">The documentation</a> explains how to connect Wagtail to the existing Django project with existing project.</p><pre># djpoe/djpoe/settings.py<br># ...<br># Application definition<br>INSTALLED_APPS = [<br>    &quot;helloworld&quot;,<br>    # Wagtail<br>    &quot;wagtail.contrib.forms&quot;,<br>    &quot;wagtail.contrib.redirects&quot;,<br>    &quot;wagtail.embeds&quot;,<br>    &quot;wagtail.sites&quot;,<br>    &quot;wagtail.users&quot;,<br>    &quot;wagtail.snippets&quot;,<br>    &quot;wagtail.documents&quot;,<br>    &quot;wagtail.images&quot;,<br>    &quot;wagtail.search&quot;,<br>    &quot;wagtail.admin&quot;,<br>    &quot;wagtail&quot;,<br>    &quot;taggit&quot;,<br>    &quot;modelcluster&quot;,<br>    # ...<br>]<br><br>MIDDLEWARE = [<br>    # ...<br>    # Wagtail<br>    &quot;wagtail.contrib.redirects.middleware.RedirectMiddleware&quot;,<br>]<br># ...<br><br>STATIC_URL = &quot;/static/&quot;<br>STATIC_ROOT = BASE_DIR / &quot;staticfiles&quot;<br>STATICFILES_DIRS = (BASE_DIR / &quot;static&quot;,)<br>MEDIA_ROOT = BASE_DIR / &quot;media&quot;<br>MEDIA_URL = &quot;/media/&quot;<br><br># ...<br><br># WAGTAIL SETTINGS<br><br># This is the human-readable name of your Wagtail install<br># which welcomes users upon login to the Wagtail admin.<br>WAGTAIL_SITE_NAME = &quot;DJPoe&quot;<br><br># Replace the search backend<br># WAGTAILSEARCH_BACKENDS = {<br>#  &#39;default&#39;: {<br>#    &#39;BACKEND&#39;: &#39;wagtail.search.backends.elasticsearch8&#39;,<br>#    &#39;INDEX&#39;: &#39;myapp&#39;<br>#  }<br># }<br><br># Wagtail email notifications from address<br># WAGTAILADMIN_NOTIFICATION_FROM_EMAIL = &#39;wagtail@myhost.io&#39;<br><br># Wagtail email notification format<br># WAGTAILADMIN_NOTIFICATION_USE_HTML = True<br><br># Allowed file extensions for documents in the document library.<br># This can be omitted to allow all files, but note that this may present a security risk<br># if untrusted users are allowed to upload files -<br># see https://docs.wagtail.org/en/stable/advanced_topics/deploying.html#user-uploaded-files<br>WAGTAILDOCS_EXTENSIONS = [&quot;csv&quot;, &quot;docx&quot;, &quot;key&quot;, &quot;odt&quot;, &quot;pdf&quot;, &quot;pptx&quot;, &quot;rtf&quot;, &quot;txt&quot;, &quot;xlsx&quot;, &quot;zip&quot;]<br><br># Reverse the default case-sensitive handling of tags<br>TAGGIT_CASE_INSENSITIVE = True</pre><p>Documentation suggested moving the &quot;yourapp&quot; to the top and adding the wagtail... apps before django... ones. We also are adding the taggit, and modelcluster apps that Wagtail uses internally.</p><p>Additionally, I copied the suggested Wagtail settings, which I will customize later in the process.</p><p>The migration has to happen now.</p><pre>djpoe ➜ poe migrate<br>Poe =&gt; python ./djpoe/manage.py migrate<br><br>Operations to perform:<br>  Apply all migrations: account, admin, auth, contenttypes, sessions, socialaccount, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers<br>Running migrations:<br>... a very long list of migrates</pre><p>We need to add the app’s URLs as well.</p><pre># djpoe/djpoe/urls.py<br><br># ...<br><br>from django.contrib import admin<br>from django.urls import include, path, re_path<br>from helloworld.views import index<br>from wagtail import urls as wagtail_urls<br>from wagtail.admin import urls as wagtailadmin_urls<br>from wagtail.documents import urls as wagtaildocs_urls<br><br>urlpatterns = [<br>    # modified admin path to avoid conflict with wagtail<br>    path(&quot;django-admin/&quot;, admin.site.urls),<br>    # wagtail admin<br>    path(&quot;admin/&quot;, include(wagtailadmin_urls)),<br>    path(&quot;documents/&quot;, include(wagtaildocs_urls)),<br>    # django-allauth<br>    path(&quot;accounts/&quot;, include(&quot;allauth.urls&quot;)),<br>    # helloworld<br>    path(&quot;&quot;, index, name=&quot;homepage&quot;),<br>    # For anything not caught by a more specific rule above, hand over to<br>    # Wagtail&#39;s serving mechanism<br>    re_path(r&quot;&quot;, include(wagtail_urls)),<br>]</pre><p>We modified the Django admin app’s URL to django-admin to avoid conflict with Wagtail’s admin URL. We could change the setting for Wagtail instead, but I think it’s better to stay with the default. At least for now.</p><pre>djpoe ➜ poe dev</pre><p>Navigating to <a href="http://127.0.0.1:8001/admin">http://127.0.0.1:8001/admin</a> will give us a log-in screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YgIUqSIdwg5e29jeB6g2Mw.png" /></figure><p>A few conclusions from this log-in page:</p><ol><li>Wagtail is working. It uses the right images.</li><li>Wagtail is not using django-allauth.</li><li>piotr is recognized as an authenticated user without the correct permissions</li></ol><p>Let’s add the superuser privileges to the user piotr. We can’t just call the poe manage createsuperuser, as the user already exists. We need to do it directly in the database.</p><pre>djpoe ➜ djpoe poe manage dbshell<br>Poe =&gt; python ./djpoe/manage.py dbshell<br>sqlite&gt; update auth_user set is_stuff=1, is_superuser=1 where username=&quot;piotr&quot;;</pre><p>After reloading the page, we see Wagtail&#39;s dashboard page. It correctly recognized the avatars.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IIHGoaA00F6kWvDVXJwZ_w.png" /></figure><p>Let’s see the page created by Wagtail. We can comment out the homepage app’s urls.py section</p><pre>    # helloworld<br>    # path(&quot;&quot;, index, name=&quot;homepage&quot;),</pre><p>Now we can navigate to the homepage at <a href="http://127.0.0.1:8001/">http://127.0.0.1:8001</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tq-HHj2GIFZEkYry0TfLRw.png" /><figcaption>Wagtail’s default homepage</figcaption></figure><p>Wagtail is working. I’ll keep it clean for now. We’ve got the accounts pages ready at /accounts/login and /accounts/logout. I want to create a minimal and functional homepage and make the settings more flexible by configuring them with environment variables.</p><h3>Homepage</h3><p>Creating the project using the wagtail start template creates a home app that can change the look of the HomePage. We must do it ourselves since we added the Wagtail to an existing Django project.</p><h4>Add the HomePage app</h4><pre>djpoe ➜ poe manage startapp home<br>djpoe ➜ rm home/admin.py home/apps.py home/tests.py home/views.py<br>djpoe ➜ mv home djpoe</pre><p>The home app is created with Django command in main directory. We need to move it to django/, the project directory.</p><p>&quot;home&quot; has to be placed in the INSTALLED_APPS list.</p><pre># djpoe/djpoe/settings.py<br># ...<br># Application definition<br>INSTALLED_APPS = [<br>    &quot;home&quot;,<br>    # Wagtail<br>    &quot;wagtail.contrib.forms&quot;,<br>    # ...</pre><p>Then, we will create a new Page and manually replace the HomePage one.</p><blockquote>The initial “Welcome to your new Wagtail site!” page is a placeholder using the base Page model, and is not directly usable. After defining your own home page model, you should create a new page at the root level through the Wagtail admin interface, and set this as the site’s homepage (under Settings / Sites). You can then delete the placeholder page.</blockquote><p>Later on, we will use <a href="https://docs.djangoproject.com/en/5.1/topics/db/fixtures/">Django fixtures</a> to avoid this manual labor on each new database…</p><p>Let’s copy the HomePage from the official <a href="https://docs.wagtail.org/en/stable/getting_started/tutorial.html">tutorial</a>. We will add it with some changes suggested by ruff.</p><pre># djpoe/home/models.py<br>&quot;&quot;&quot;Models for the home app.&quot;&quot;&quot;<br><br>from typing import ClassVar<br><br>from django.db import models<br>from wagtail.admin.panels import FieldPanel<br>from wagtail.fields import RichTextField<br>from wagtail.models import Page<br><br><br>class HomePage(Page):<br>    &quot;&quot;&quot;Home page model.&quot;&quot;&quot;<br>    body = RichTextField(blank=True)<br>    content_panels: ClassVar[list[FieldPanel]] = [*Page.content_panels, FieldPanel(&quot;body&quot;)]</pre><p>It adds the body element edited with a rich text editor.</p><p>We need to migrate the database to add the new model.</p><pre>djpoe ➜ poe makemigrations<br>Poe =&gt; python ./djpoe/manage.py makemigrations<br>System check identified some issues:<br><br>Migrations for &#39;home&#39;:<br>  djpoe/home/migrations/0001_initial.py<br>    + Create model HomePage<br><br>djpoe ➜ poe migrate       <br>Poe =&gt; python ./djpoe/manage.py migrate<br>System check identified some issues:<br><br>Operations to perform:<br>  Apply all migrations: account, admin, auth, contenttypes, home, sessions, socialaccount, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers<br>Running migrations:<br>  Applying home.0001_initial... OK</pre><h4>Edit the content</h4><p>We can now navigate to Pages / + and create a new homepage Page <a href="http://127.0.0.1:8001/admin/pages/add/home/homepage/1/">http://127.0.0.1:8001/admin/pages/add/home/homepage/1/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LnSqPBRU36HMMy3vkF0thQ.png" /><figcaption>Editor for the new HomePage</figcaption></figure><p>After Saving and then publishing the page, we see a warning:</p><pre>The root level is where you can add new sites to your Wagtail installation. <br>Pages created here will not be accessible at any URL until they are associated <br>with a site. Configure a site now.<br><br>If you just want to add pages to an existing site, create them as children of <br>the homepage instead. </pre><p>The first warning is about configuring the site. Navigate to the <a href="http://127.0.0.1:8001/admin/sites/edit/1/">provided link</a>, see the pre-configured localhost site, and fix the port settings to 8001. Then, set the Root page to our new HomePage, and hit Save</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LNDu0XxyCGvl8hVaqMuZoA.png" /></figure><p>Our HomePage isn’t fully configured yet. When we try to load it, we see the error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*hjPfLquCIn-ET1BoZ0lV_A.png" /></figure><h4>Adding a template</h4><p>The template file djpoe/home/templates/home_page.html is missing. To check if it’s the right path, I usually copy a blind HTML file. We will use the one from the homepage app.</p><pre>djpoe ➜ mkdir -p djpoe/home/templates<br>djpoe ➜ cp djpoe/helloworld/templates/helloworld/index.html djpoe/home/templates/home/home_page.html</pre><p>There is no error, but we don’t see the edited data. Wagtail provides <a href="https://docs.wagtail.org/en/stable/topics/writing_templates.html">documentation about editing templates</a>. Let’s use it to create a simple page with auth menu.</p><pre>{# djpoe/home/templates/home/home_page.html #}<br>{% load wagtailcore_tags %}<br>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<br>    &lt;title&gt;{{ page.title }}&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css&quot; integrity=&quot;sha512-EZLkOqwILORob+p0BXZc+Vm3RgJBOe1Iq/0fiI7r/wJgzOFZMlsqTa29UEl6v6U6gsV4uIpsNZoV32YZqrCRCQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    {% if user.is_authenticated %}<br>        &lt;p&gt;<br>            Welcome, {{ user.username }}!<br>            &lt;a href=&quot;{% url &#39;account_logout&#39; %}&quot;&gt;Logout&lt;/a&gt;<br>        &lt;/p&gt;<br>    {% else %}<br>        &lt;p&gt;<br>            Welcome, guest!<br>            &lt;a href=&quot;{% url &#39;account_login&#39; %}&quot;&gt;Login&lt;/a&gt;<br>            &lt;a href=&quot;{% url &#39;account_signup&#39; %}&quot;&gt;Sign Up&lt;/a&gt;<br>        &lt;/p&gt;<br>    {% endif %}<br>    {{ page.body|richtext }}<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>First, we load the Wagtail tags. This will allow us to use the richtext tag on the page.body content. We’ve changed the title for page.title, and left the log-in “header”.</p><p>In the body section, we use the {% if user.is_authenticated %} tag to define the conditional block containing a personal greeting and a logout link displayed when the user is authenticated. We provide login/signup links for unauthenticated visitors</p><p>Finally, {{ page.body|richtext }} renders the content entered in the editor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*fkMxZduzm7Z_rfpoAl9qbA.png" /><figcaption>HomePage with only `alt` displayed</figcaption></figure><p>The image is not loaded and shows only the alt parameter. <br>The HTML code for it shows it tries to load the image from /images/ directory.</p><pre>&lt;img <br>    alt=&quot;20230930-DSC_3192&quot; <br>    class=&quot;richtext-image full-width&quot; <br>    height=&quot;281&quot; <br>    src=&quot;/images/20230930-DSC_3192.width-800.jpg&quot; <br>    width=&quot;800&quot;<br>&gt;</pre><h4>Serve images</h4><p>We need to tell Django to self-host the images. This isn’t right for the deployed project, so we must keep that in mind.</p><p>We will add the static URLs. These are already configured in settings.py</p><pre># djpoe/djpoe/urls.py<br><br>from django.conf import settings<br>from django.conf.urls.static import static<br>from django.contrib import admin<br>from django.urls import include, path, re_path<br>from wagtail import urls as wagtail_urls<br>from wagtail.admin import urls as wagtailadmin_urls<br>from wagtail.documents import urls as wagtaildocs_urls<br><br>urlpatterns = [<br>    path(&quot;django-admin/&quot;, admin.site.urls),<br>    path(&quot;admin/&quot;, include(wagtailadmin_urls)),<br>    path(&quot;accounts/&quot;, include(&quot;allauth.urls&quot;)),<br>    re_path(r&quot;^media/(?P&lt;path&gt;.*)$&quot;, serve, {&quot;document_root&quot;: settings.MEDIA_ROOT}),<br>    re_path(r&quot;^static/(?P&lt;path&gt;.*)$&quot;, serve, {&quot;document_root&quot;: settings.STATIC_ROOT}),<br>    re_path(r&quot;&quot;, include(wagtail_urls)),<br>]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*piChkZxBUVNGB7VOH5a7HA.png" /><figcaption>HomePage with a visible image</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*ZQQ8h7nACDidrOtTDc_QoQ.png" /><figcaption>Edit the HomePage mode with a visible image.</figcaption></figure><h3>Cleaning up</h3><h4>Remove the helloworld app</h4><p>We need to remove the helloworld app, we are no longer using it. It is mentioned in quite a few places. Please remove it from all the files mentioned in the search panel. Some files and the djpoe/homepage directory will be deleted entirely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*R9I6qux5dYxWhe52lN9l4w.png" /></figure><h4>Tests</h4><p>Tests failed as we changed the URL to the Django admin pages.</p><pre>E       AssertionError: assert &#39;/django-admi...django-admin/&#39; == &#39;/admin/login/?next=/admin/&#39;<br>E         <br>E         - /admin/login/?next=/admin/<br>E         + /django-admin/login/?next=/django-admin/<br>E         ?  +++++++                   +++++++</pre><p>We need to add the django- prefix to the string. The asserts section will now look like this:</p><pre># tests/test_admin_page.py<br># ...<br>    assert response.status_code == 302<br>    assert response.content == b&quot;&quot;<br>    assert response.url == &quot;/django-admin/login/?next=/django-admin/&quot;  # type: ignore[attr-defined]</pre><h4>Fix typing</h4><p>After running poe mypy I found that the wagtail app is untyped.</p><pre>djpoe ➜ poe mypy      <br>Poe =&gt; mypy --install-types --check-untyped-defs .<br>djpoe/home/models.py: error: Skipping analyzing &quot;wagtail.contrib.redirects.models&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/home/models.py: error: Skipping analyzing &quot;wagtail.models.sites&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/home/models.py: error: Skipping analyzing &quot;wagtail.models.i18n&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/home/models.py: error: Skipping analyzing &quot;wagtail.contrib.forms.models&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/home/models.py: error: Skipping analyzing &quot;wagtail.search.models&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/home/models.py:3: error: Skipping analyzing &quot;wagtail.admin.panels&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/home/models.py:4: error: Skipping analyzing &quot;wagtail.fields&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/home/models.py:5: error: Skipping analyzing &quot;wagtail.models&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/djpoe/urls.py:23: error: Skipping analyzing &quot;wagtail&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/djpoe/urls.py:24: error: Skipping analyzing &quot;wagtail.admin&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>djpoe/djpoe/urls.py:24: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports<br>djpoe/djpoe/urls.py:25: error: Skipping analyzing &quot;wagtail.documents&quot;: module is installed, but missing library stubs or py.typed marker  [import-untyped]<br>Found 11 errors in 2 files (checked 18 source files)</pre><p>Unfortunately, neither Django nor Wagtail is typed. To make mypy happy, we need to switch off analyzing for all of these modules. We can’t do it as we did with django-environ — by adding a # type: ignore[import-untyped] as we do import it in quite a few places. The solution has to work for the entire project.</p><pre># pyproject.toml<br><br>[[tool.mypy.overrides]]<br>module = &quot;wagtail.*&quot;<br>ignore_missing_imports = true</pre><h4>Update packages</h4><p>During the work on this article, Wagtail (along with other packages) released a new version.</p><pre>djpoe ➜ poe find_updates<br>Poe =&gt; poetry show --outdated | grep --file=&lt;(poetry show --tree | grep &#39;^\w&#39; | sed &#39;s/^\([^ ]*\).*/^\1/&#39;)<br>django-allauth   64.2.1          65.0.1          Integrated set of Django ap...<br>django-stubs     5.0.4           5.1.0           Mypy stubs for Django<br>django-stubs-ext 5.0.4           5.1.0           Monkey-patching and extensi...<br>django-taggit    5.0.1           6.0.0           django-taggit is a reusable...<br>pylint           3.3.0           3.3.1           python code static checker<br>ruff             0.6.6           0.6.7           An extremely fast Python li...<br>wagtail          6.2.1           6.2.2           A Django content management...</pre><pre>djpoe ➜ poetry add &quot;django-allauth=^65&quot; &quot;wagtail=^6.2&quot;<br>djpoe ➜ poetry add --group dev &quot;django-stubs-ext=^5&quot;<br>djpoe ➜ poe requirements<br>djpoe ➜ poe migrate</pre><h4>Ignore uploaded media</h4><pre># .gitignore<br># ...<br><br><br># Wagtail image content<br>images/<br>original_images/</pre><h3>Some flexible configuration</h3><p>We’ve got a default setting for the Wagtail site name. It is displayed in the admin page.</p><pre>WAGTAIL_SITE_NAME = &quot;DJPoe&quot;</pre><p>We also get a warning each time we run manage script:</p><pre>?: (wagtailadmin.W003) The WAGTAILADMIN_BASE_URL setting is not defined<br>        HINT: This should be the base URL used to access the Wagtail admin site. Without this, URLs in notification emails will not display correctly.</pre><p>Let’s make them configurable with environment variables:</p><pre># djpoe/djpoe/settings.py<br># ...<br>env = environ.FileAwareEnv(<br>    # ...<br>    WAGTAIL_SITE_NAME=(str, &quot;DJPoe&quot;),<br>    WAGTAILADMIN_BASE_URL=(str, &quot;http://localhost:8001&quot;),<br>    WAGTAILADMIN_NOTIFICATION_USE_HTML=(bool, True),<br>    DEFAULT_FROM_EMAIL=(str, &quot;email@example.com&quot;),<br>)<br><br># ...<br>WAGTAIL_SITE_NAME = env(&quot;WAGTAIL_SITE_NAME&quot;)<br>WAGTAILADMIN_BASE_URL = env(&quot;WAGTAILADMIN_BASE_URL&quot;)<br>DEFAULT_FROM_EMAIL = env(&quot;DEFAULT_FROM_EMAIL&quot;)<br>WAGTAILADMIN_NOTIFICATION_USE_HTML = env(&quot;WAGTAILADMIN_NOTIFICATION_USE_HTML&quot;)</pre><p>I wanted to change the Wagtail login page to the allauth one (by setting the WAGTAILADMIN_LOGIN_URLto “/admins/login”), but if a user has no right to edit pages, it ends up with an infinite redirect loop. <em>There’s a way to change the login template.</em></p><h3>Testing HomePage</h3><p>Let’s dump the data into a fixture.</p><pre>djpoe ➜ mkdir djpoe/home/fixtures<br>djpoe ➜ poe manage dumpdata \<br>  wagtailcore.locale wagtailcore.site wagtailcore.page home  \<br>  -o djpoe/home/fixtures/homepage.json \<br>  --format json \<br>  --indent 2 \<br>  --natural-foreign</pre><p>This will create a djpoe/home/fixtures/homepage.json fixture file. It is long as it contains pages pre-built by Wagtail. I will edit it and remove unnecessary parts, but first, we need to be able to load and test it.</p><p>The test file:</p><pre># tests/test_home.py<br><br>import pytest<br>from django.core.management import call_command<br>from django.test import Client<br><br><br>@pytest.mark.django_db<br>def test_homepage(client: Client):<br>    call_command(&quot;loaddata&quot;, &quot;homepage.json&quot;)<br><br>    response = client.get(&quot;/&quot;)<br>    assert &quot;DJPoe - boilerplate for side projects&quot; in response.content.decode()</pre><p>The test would fail to load the homepage.json fixture on several occasions. I needed to remove from it the owner of the page, revision_id etc., because the related entries were absent in the plain test database. After the fixture started to load correctly, the test passed. We need to remove the parts that seem less important or unnecessary. First, I left only the home page. That means leaving a page with “content_type”: [“home”, “homepage”]and removing the ones with “content_type”: [“wagtailcore”, “page”]. Then, I left only the required fields in the home page entry. As I wanted the homepage to work well in deployment, I’ve also changed the primary keys to replace the one pre-built by Wagtail.</p><p>The final test fixture is provided below:</p><pre>[<br>{<br>  &quot;model&quot;: &quot;wagtailcore.locale&quot;,<br>  &quot;pk&quot;: 1,<br>  &quot;fields&quot;: {<br>    &quot;language_code&quot;: &quot;en&quot;<br>  }<br>},<br>{<br>  &quot;model&quot;: &quot;wagtailcore.site&quot;,<br>  &quot;pk&quot;: 1,<br>  &quot;fields&quot;: {<br>    &quot;hostname&quot;: &quot;localhost&quot;,<br>    &quot;port&quot;: 8001,<br>    &quot;site_name&quot;: &quot;&quot;,<br>    &quot;root_page&quot;: 2,<br>    &quot;is_default_site&quot;: true<br>  }<br>},<br>{<br>  &quot;model&quot;: &quot;wagtailcore.page&quot;,<br>  &quot;pk&quot;: 2,<br>  &quot;fields&quot;: {<br>    &quot;path&quot;: &quot;00010002&quot;,<br>    &quot;depth&quot;: 2,<br>    &quot;numchild&quot;: 0,<br>    &quot;locale&quot;: 1,<br>    &quot;live&quot;: true,<br>    &quot;title&quot;: &quot;DJPoe HomePage&quot;,<br>    &quot;draft_title&quot;: &quot;DJPoe HomePage&quot;,<br>    &quot;slug&quot;: &quot;the-new-homepage&quot;,<br>    &quot;content_type&quot;: [<br>      &quot;home&quot;,<br>      &quot;homepage&quot;<br>    ],<br>    &quot;url_path&quot;: &quot;/the-new-homepage/&quot;,<br>    &quot;seo_title&quot;: &quot;&quot;,<br>    &quot;show_in_menus&quot;: false<br>  }<br>},<br>{<br>  &quot;model&quot;: &quot;home.homepage&quot;,<br>  &quot;pk&quot;: 2,<br>  &quot;fields&quot;: {<br>    &quot;page_ptr&quot;: 2,<br>    &quot;body&quot;: &quot;&lt;h2 data-block-key=\&quot;bdss1\&quot;&gt;DJPoe - boilerplate for side projects&lt;/h2&gt;&lt;p data-block-key=\&quot;eases\&quot;&gt;&lt;a href=\&quot;https://github.com/zalun/djpoe\&quot;&gt;https://github.com/zalun/djpoe&lt;/a&gt;&lt;/p&gt;&quot;<br>  }<br>}<br>]</pre><p>If one wants to work on multiple dev machines (This article is written on desktop and laptop), the fixture can be loaded with:</p><pre>djpoe ➜ poe manage loaddata homepage</pre><h3>Deployment</h3><p>After DigitalOcean successfully built the app, click on the Console panel and migrate the database. Then, load the homepage fixture.</p><pre>$ python ./djpoe/manage.py migrate<br>$ python ./djpoe/manage.py loaddata homepage</pre><h3>Host media on another server</h3><p>The deployment isn’t finished yet. The media/ directory is deleted when the site is deployed. We need to find a way to store the files on another server. <a href="https://django-storages.readthedocs.io/en/latest/index.html">django-storages</a> to the rescue!</p><p>I checked the DigitalOcean offer on <a href="https://docs.digitalocean.com/products/spaces/">Spaces Object Storage</a>. It’s $5 for 250G storage and 1T bandwidth. This is a good offer. I will consider it for commercial deployment. The configuration is <a href="https://django-storages.readthedocs.io/en/latest/backends/s3_compatible/digital-ocean-spaces.html">almost identical</a> to the <a href="https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html">AWS S3 storage</a> one. The latter is better for a site like DJPoe due to the free plan for low usage.</p><h4>First, we need to create a S3 bucket.</h4><p>Navigate to AWS S3. I use <a href="https://www.granted.dev/">granted</a>. This command logs me into AWS and opens the Amazon S3 page.</p><pre>djpoe ➜ assume -c zalun -s s3</pre><p>Click on Create bucket button. Choose the unique name, switch to “ACLs enabled,” and disable the “block all public access” setting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hcoddqm3BgSgQL7_sLyWiw.png" /></figure><p>The bucket will be listed, and you can browse its contents, but for now, it’s empty.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U04H1aWKC3v0cEaB-dij3w.png" /></figure><p>Add an AWS user and create a group for the privileges.</p><pre>djpoe ➜ assume -c zalun -s iam</pre><p>Create the user and the group, and add the user to the group.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sErOxEdcjgyjRhW0lb5Xcw.png" /></figure><p>Navigate to the Permissions tab and add the group full access to the S3 bucket. You may be more granular about this and access to the specific bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*0apV3vk_hEqv5eClLDpVHQ.png" /></figure><p>Go to Users page, go to the Security Credentials tab and, in the Access keys panel click on Create access key button. Choose Local code to generate access keys for local development.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*emW-FpGPKt-a_HG5bH8ABQ.png" /></figure><p>In the next step, provide a description of the access key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lJKPJu1uIV1zUetzYvJFdg.png" /></figure><p>An access key is created. Store both the Access key and the Secret access key in djpoe/djpoe/.env file. It’s mentioned in .gitignore file, and safe as any other data on your local disk. Do not add this to your code or any file that could be committed.</p><h4>Django configuration</h4><p>We need to install the django-storages package with S3 extras.</p><pre>djpoe ➜ poetry add &quot;django-storages[s3]&quot;<br>djpoe ➜ poe requirements</pre><p>And configure the app in djpoe/djpoe/settings.py</p><pre># djpoe/djpoe/seettings.py<br># ...<br>env = environ.FileAwareEnv(<br>    # ...<br>    AWS_BUCKET_NAME=(str, &quot;&quot;),<br>    AWS_REGION_NAME=(str, &quot;&quot;),<br>    AWS_ACCESS_KEY=(str, &quot;&quot;),<br>    AWS_SECRET_KEY=(str, &quot;&quot;),<br>)<br># ...<br>INSTALLED_APPS = [<br>    # ...<br>    &quot;storages&quot;,<br>]<br># ...<br>STORAGES = {<br>    &quot;default&quot;: {<br>        &quot;BACKEND&quot;: &quot;storages.backends.s3.S3Storage&quot;,<br>        &quot;OPTIONS&quot;: {<br>            &quot;bucket_name&quot;: env(&quot;AWS_BUCKET_NAME&quot;),<br>            &quot;region_name&quot;: env(&quot;AWS_REGION_NAME&quot;),<br>            &quot;access_key&quot;: env(&quot;AWS_ACCESS_KEY&quot;),<br>            &quot;secret_key&quot;: env(&quot;AWS_SECRET_KEY&quot;),<br>            &quot;default_acl&quot;: &quot;public-read&quot;,<br>        },<br>    },<br>    &quot;staticfiles&quot;: {<br>        &quot;BACKEND&quot;: &quot;django.contrib.staticfiles.storage.StaticFilesStorage&quot;,<br>    },<br>}</pre><p>Edit the djpoe/djpoe/.env with the new entries.</p><pre><br>AWS_BUCKET_NAME=djpoe-media<br>AWS_REGION_NAME=eu-central-1<br>AWS_ACCESS_KEY=AKIAVVADXXXX<br>AWS_SECRET_KEY=wQXXX/ubsXXX</pre><p>After adding an image to the home page, we will see it uses the new URL <a href="https://djpoe-media.s3.eu-central-1.amazonaws.com/images/20230930-DSC_3192.max-800x600.jpg">https://djpoe-media.s3.eu-central-1.amazonaws.com/images/20230930-DSC_3192.max-800x600.jpg</a></p><p>The bucket now contains two directories — images and original_images</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZEVqON-qKakKxfbvxd3VQQ.png" /></figure><p>Before deployment, create a new access key and add the variables to the DigitalOcean App environment.</p><h3>Wrapping up</h3><p>Wagtail is enormous, and we can do a lot with it. However, most of the changes I can think about will be relevant to the actual project implementation. I’m considering page layout, supporting multiple languages, or front-end frameworks. For now, this article ends the DJPoe saga. I might extend it in the future with some valuable features. I’m considering subscription payment integration, O<a href="https://opentofu.org/">penTofu</a> for infrastructure as code, and centralized logs.</p><p>Feel free to <a href="https://github.com/zalun/djpoe">copy the code</a> and use it in your project. <br>Leave a comment if you do.</p><p>The code for this article is placed in the <a href="https://github.com/zalun/djpoe/tree/part-6-cms">part-6-cms</a> branch.</p><h3>Links used in the article</h3><ul><li><a href="https://medium.com/django-unleashed/base-5-3rd-party-authentication-in-django-887cad242a38">Base 5–3rd party authentication in Django</a></li><li><a href="https://github.com/zalun/djpoe/tree/part-5-auth">part-5-auth branch</a></li><li><a href="https://docs.wagtail.org/">Wagtail documentation</a></li><li><a href="https://docs.wagtail.org/en/stable/advanced_topics/add_to_django_project.html">Add Wagtail to existing Django project</a></li><li><a href="https://docs.wagtail.org/en/stable/topics/writing_templates.html">Wagtail documentation — Writing templates</a></li><li><a href="https://docs.digitalocean.com/products/spaces/">DigitalOcean — Spaces Object Storage</a></li><li><a href="https://django-storages.readthedocs.io/en/latest/backends/s3_compatible/digital-ocean-spaces.html">django-storages on Space Object Storage</a></li><li><a href="https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html">django-storages on Amazon S3</a></li><li><a href="https://www.granted.dev/">Granted — The easiest way to access your cloud</a></li><li><a href="https://github.com/zalun/djpoe">DJPoe repository</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f60d8ec7ac9e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Base 5–3rd party authentication in Django]]></title>
            <link>https://medium.com/django-unleashed/base-5-3rd-party-authentication-in-django-887cad242a38?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/887cad242a38</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[google-sign-in]]></category>
            <category><![CDATA[oauth2]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Sun, 15 Sep 2024 20:30:26 GMT</pubDate>
            <atom:updated>2024-09-29T16:18:29.876Z</atom:updated>
            <content:encoded><![CDATA[<p>Remember the frustration of creating new accounts for every website? I certainly do. Some of us still have two or three passwords cycling between sites. When “OpenID” kicked in, I was over the moon. It took a while for Google to adopt it, and Single Sign-On revolutionized how we use credentials everywhere. Let’s build this feature into our boilerplate project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ats7Lw8jRhomsBv-p-rbsA.jpeg" /><figcaption>Microsoft Designer is hallucinating the article title.</figcaption></figure><p>We continue from the last article, <a href="https://medium.com/django-unleashed/4-deploy-django-on-digitalocean-dc4b713e52c2">Base 4 — Deploy Django on DigitalOcean</a>. The code for this one is in the <a href="https://github.com/zalun/djpoe/tree/part-4-deploy-on-digitalocean">part-4-deploy-on-digitalocean</a> branch.</p><p>For this implementation, we’ll use <a href="https://docs.allauth.org/en/latest/">django-allauth</a>, a robust authentication app for Django. It’s a popular choice due to its comprehensive support for over <a href="https://docs.allauth.org/en/latest/socialaccount/providers/index.html">100 authentication methods</a>. Given its widespread use and familiarity among users, we’ll set up <a href="https://cloud.google.com/architecture/identity/single-sign-on">Google Sign-On</a> as our primary third-party authentication option. We can add more in a parallel article. This approach will give us a solid authentication system that caters to different user preferences while keeping our codebase clean and maintainable.</p><h3>Upgrading dependencies</h3><p>It’s been a while since we installed the dependencies, so it would be good to check for updates. poetry has a command poetry show --outdated, unfortunately, it displays all installed outdated dependencies, including the ones not mentioned in the pyproject.toml file. The shortest way to filter them is via this command:</p><pre>djpoe ➜ poetry show --outdated | grep --file=&lt;(poetry show --tree | grep &#39;^\w&#39; | sed &#39;s/^\([^ ]*\).*/^\1/&#39;)</pre><h4>poetry show --outdated</h4><p>lists all outdated dependencies in your Poetry project</p><h4>poetry show --tree</h4><p>shows the dependency tree of your project in a format:</p><pre>django 5.1.1 A high-level Python web framework that encourages rapid development and clean, pragmatic design.<br>├── asgiref &gt;=3.8.1,&lt;4<br>├── sqlparse &gt;=0.3.1<br>└── tzdata *</pre><h4>grep ‘^\w’</h4><p>filters the output of the above command and returns only the lines starting with a word. Here it would be the line starting with django</p><h4>sed ‘s/^\([^ ]*\).*/^\1/’)</h4><p>filters the line only to keep the package name. Here — django</p><h4>grep --file</h4><p>takes the list of outdated packages and filters it with a list of the first-choice ones written in pyproject.toml</p><p>We will add entire command to the poethepoet tasks for the future usage:</p><pre># pyproject.toml<br># ...<br>[tool.poe.tasks.find_updates]<br>shell = &quot;poetry show --outdated | grep --file=&lt;(poetry show --tree | grep &#39;^\\w&#39; | sed &#39;s/^\\([^ ]*\\).*/^\\1/&#39;)&quot;<br>interpreter = &quot;bash&quot;</pre><pre>djpoe ➜ poe find_updates<br>Poe =&gt; poetry show --outdated | grep --file=&lt;(poetry show --tree | grep &#39;^\w&#39; | sed &#39;s/^\([^ ]*\).*/^\1/&#39;)<br>django        5.1   5.1.1 A high-level Python web framework that encourages ...<br>pytest-django 4.8.0 4.9.0 A Django plugin for pytest.<br>ruff          0.6.3 0.6.4 An extremely fast Python linter and code formatter...</pre><p>Checking pyproject.toml config for these packages:</p><pre>[tool.poetry.dependencies]<br>django = &quot;^5.1&quot;<br><br>[tool.poetry.group.dev.dependencies]<br>pytest-django = &quot;^4.8.0&quot;<br>ruff = &quot;^0.6&quot;</pre><p>The version defined in pypoetry is ^5.1for django and ^0.6 for ruff. We can upgrade them with plain install.</p><pre>djpoe ➜ poetry install</pre><p>However, we need to update the pytest-django version to ^4 as we don’t need to be so precise.</p><pre>djpoe ➜ poetry add --group dev pytest-django@^4</pre><p>Since our project’s dependencies are fresh, we can proceed to the actual feature implementation.</p><h3>Using django-allauth with regular accounts</h3><p><a href="https://docs.allauth.org/en/latest/account/configuration.html">django-allauth’s documentation</a> provides a lot of configuration settings. It’s worth the read to understand how flexible this app is. I like some of them to be set to a different value than the default one.</p><h4>Using email instead of a username</h4><pre>ACCOUNT_AUTHENTICATION_METHOD=&quot;email&quot;  # (default: &quot;username&quot;)<br>ACCOUNT_EMAIL_REQUIRED=True  # (default: False)</pre><p>I don’t remember the last time I signed up using my username. Using email seems natural, and it’s unique to the world.</p><h4>Email confirmation</h4><pre>ACCOUNT_EMAIL_VERIFICATION=&quot;mandatory&quot;. # (default: &quot;optional&quot;)</pre><p>Let’s make it mandatory to verify the email.</p><pre>ACCOUNT_CONFIRM_EMAIL_ON_GET=True  # (default: False)</pre><p>Determines whether or not a GET request automatically confirms an email address. <a href="http://programmers.stackexchange.com/questions/188860/">GET is not designed to modify the server state</a>, though it is commonly used for email confirmation. To avoid requiring user interaction, consider using POST via Javascript in your email confirmation template as an alternative to setting this to True.</p><h4>Notifying users about password changes</h4><pre>ACCOUNT_EMAIL_NOTIFICATIONS=True  # (default: False)</pre><p>It feels safer to receive such an email.</p><h4>Lowercase usernames</h4><pre>ACCOUNT_PRESERVE_USERNAME_CASING=False  # (default=True)</pre><p>Although we no longer use the usernames to log in, the system will use them to print out on the profile page and other places. I want to keep it consistent and lowercase.</p><h4>Configuration</h4><p>We will add the config constants to the settings.py file, using the above values as the default ones but leaving them to be configured with the environment variables.</p><pre># djpoe/djpoe/settings.py<br># ...<br>env = environ.FileAwareEnv(<br>    DEBUG=(bool, False),<br>    DATABASE_URL=(str, &quot;sqlite&quot;),<br>    SECRET_KEY=(str),<br>    # django-allauth regular account settings<br>    ACCOUNT_AUTHENTICATION_METHOD=(str, &quot;email&quot;),<br>    ACCOUNT_EMAIL_REQUIRED=(bool, True),<br>    ACCOUNT_CONFIRM_EMAIL_ON_GET=(bool, False),<br>    ACCOUNT_EMAIL_VERIFICATION=(str, &quot;mandatory&quot;),<br>    ACCOUNT_EMAIL_NOTIFICATIONS=(bool, True),<br>    ACCOUNT_PRESERVE_USERNAME_CASING=(bool, False),<br>)<br># ...<br><br># django-allauth regular account settings<br>ACCOUNT_AUTHENTICATION_METHOD = env(&quot;ACCOUNT_AUTHENTICATION_METHOD&quot;)<br>ACCOUNT_EMAIL_REQUIRED = env(&quot;ACCOUNT_EMAIL_REQUIRED&quot;)<br>ACCOUNT_CONFIRM_EMAIL_ON_GET = env(&quot;ACCOUNT_CONFIRM_EMAIL_ON_GET&quot;)<br>ACCOUNT_EMAIL_VERIFICATION = env(&quot;ACCOUNT_EMAIL_VERIFICATION&quot;)<br>ACCOUNT_EMAIL_NOTIFICATIONS = env(&quot;ACCOUNT_EMAIL_NOTIFICATIONS&quot;)<br>ACCOUNT_PRESERVE_USERNAME_CASING = env(&quot;ACCOUNT_PRESERVE_USERNAME_CASING&quot;)</pre><h4>Installing django-allauth</h4><pre>djpoe ➜ poetry add django-allauth<br>  • Installing django-allauth (64.2.1)<br>djpoe ➜ poetry export --without-hashes --format=requirements.txt &gt; requirements.txt</pre><h4>Connecting to the project</h4><p>Let’s implement some instructions from <a href="https://docs.allauth.org/en/latest/installation/quickstart.html">allauth’s Quickstart</a>. In our case, before adding the social accounts, we should add the following:</p><pre># djpoe/djpoe/settings.py<br># ...<br>INSTALLED_APPS = [<br>    ...<br>    # The following apps are required:<br>    &#39;django.contrib.auth&#39;,<br>    &#39;django.contrib.messages&#39;,<br><br>    &#39;allauth&#39;,<br>    &#39;allauth.account&#39;,<br># ...<br>MIDDLEWARE = (<br>    &quot;django.middleware.common.CommonMiddleware&quot;,<br>    &quot;django.contrib.sessions.middleware.SessionMiddleware&quot;,<br>    &quot;django.middleware.csrf.CsrfViewMiddleware&quot;,<br>    &quot;django.contrib.auth.middleware.AuthenticationMiddleware&quot;,<br>    &quot;django.contrib.messages.middleware.MessageMiddleware&quot;,<br><br>    # Add the account middleware:<br>    &quot;allauth.account.middleware.AccountMiddleware&quot;,<br>)<br># ...    <br>AUTHENTICATION_BACKENDS = [<br>    # Needed to login by username in Django admin, regardless of `allauth`<br>    &quot;django.contrib.auth.backends.ModelBackend&quot;,<br>    # `allauth` specific authentication methods, such as login by email<br>    &quot;allauth.account.auth_backends.AuthenticationBackend&quot;,<br>]</pre><pre># djpoe/djpoe/urls.py<br># ...<br>urlpatterns = [<br>    ...<br>    path(&#39;accounts/&#39;, include(&#39;allauth.urls&#39;)),<br>    ...<br>]</pre><p>After saving these files, we have to migrate the database.</p><pre>djpoe ➜ poe migrate<br>Poe =&gt; python ./djpoe/manage.py migrate<br>Operations to perform:<br>  Apply all migrations: account, admin, auth, contenttypes, sessions<br>Running migrations:<br>  Applying account.0001_initial... OK<br>  Applying account.0002_email_max_length... OK<br>  Applying account.0003_alter_emailaddress_create_unique_verified_email... OK<br>  Applying account.0004_alter_emailaddress_drop_unique_email... OK<br>  Applying account.0005_emailaddress_idx_upper_email... OK<br>  Applying account.0006_emailaddress_lower... OK<br>  Applying account.0007_emailaddress_idx_email... OK<br>  Applying account.0008_emailaddress_unique_primary_email_fixup... OK<br>  Applying account.0009_emailaddress_unique_primary_email... OK</pre><pre>djpoe ➜ poe manage dbshell<br>Poe =&gt; python ./djpoe/manage.py dbshell<br>SQLite version 3.43.2 2023-10-10 13:08:14<br>Enter &quot;.help&quot; for usage hints.<br>sqlite&gt; .tables<br>account_emailaddress        auth_user_groups          <br>account_emailconfirmation   auth_user_user_permissions<br>auth_group                  django_admin_log          <br>auth_group_permissions      django_content_type       <br>auth_permission             django_migrations         <br>auth_user                   django_session   </pre><p>We will now launch the website…</p><pre>djpoe ➜ poe dev</pre><p>… and navigate to the login screen at <a href="http://127.0.0.1:8001/accounts/login/">http://127.0.0.1:8001/accounts/login/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*hSw970tfhcVD1cyvAoQpjg.png" /></figure><p>We receive an error during the signup process:</p><pre>...<br>File &quot;/opt/homebrew/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/socket.py&quot;, line 853, in create_connection<br>...</pre><p>We want to send a confirmation email from the localhost and it’s not yet allowed. Let’s switch this requirement off for now, and we can always add the sending email feature in the future.<br>Add the following to the .env file.</p><pre># .env<br>DEBUG=True<br>SECRET_KEY=django-insecure-=@ek@$wwm7-#ee<br>ACCOUNT_EMAIL_VERIFICATION=none<br>ACCOUNT_EMAIL_NOTIFICATIONS=False</pre><p>Now we can log in, but the browser redirects to <a href="http://127.0.0.1:8001/accounts/profile/">http://127.0.0.1:8001/accounts/profile/</a>, which gives us a 404 page. Several settings configure the redirect:</p><ul><li>LOGIN_REDIRECT_URL (default: ”/accounts/profile/”) The URL where requests are redirected for login.</li><li>LOGOUT_REDIRECT_URL (default: LOGIN_REDIRECT_URL) The URL where requests are redirected for logout.</li><li>ACCOUNT_AUTHENTICATED_LOGIN_REDIRECTS (default: True) Should the browser redirect to LOGIN_REDIRECT_URL after a successful login action?</li><li>ACCOUNT_SIGNUP_REDIRECT_URL (default: LOGIN_REDIRECT_URL)</li><li>ACCOUNT_LOGOUT_REDIRECT_URL (default: LOGOUT_REDIRECT_URL or “/”)</li></ul><p>The simplest way to avoid the 404 page is to override the LOGIN_REDIRECT_URL setting and set the redirect to ”/”:</p><pre># djpoe/djpoe/settings.py<br># ...<br>env = environ.FileAwareEnv(<br>    # ...<br>    LOGIN_REDIRECT_URL=(str, &quot;/accounts/profile/&quot;),<br>)<br># ...<br>LOGIN_REDIRECT_URL = env(&quot;LOGIN_REDIRECT_URL&quot;)</pre><pre># .env<br># ...<br>LOGIN_REDIRECT_URL = env(&quot;LOGIN_REDIRECT_URL&quot;)</pre><p>We can log out by navigating to <a href="http://127.0.0.1:8001/accounts/logout/">http://127.0.0.1:8001/accounts/logout/</a> and click on the Sign Out button.</p><p>To test if the complete cycle is working, we can start with a clean database:</p><pre>djpoe ➜ rm djpoe/sqlite<br>djpoe ➜ poe manage migrate<br>Poe =&gt; python ./djpoe/manage.py migrate<br>Operations to perform:<br>  Apply all migrations: account, admin, auth, contenttypes, sessions<br>Running migrations:<br>  Applying contenttypes.0001_initial... OK<br>  ...<br>  Applying auth.0012_alter_user_first_name_max_length... OK<br>  Applying sessions.0001_initial... OK<br>djpoe ➜ poe dev</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*zkdLe7_I3x4WUPHjR7Da9A.png" /></figure><p>After filling in the Sign-up information, I was redirected to <a href="http://127.0.0.1:8001/">http://127.0.0.1:8001/</a>. Since I’m logged in, the browser does the same for <a href="http://127.0.0.1:8001/accounts/login">http://127.0.0.1:8001/accounts/login</a>. Again, logging out on <a href="http://127.0.0.1:8001/accounts/logout">http://127.0.0.1:8001/accounts/logout</a>.</p><p>We can check what happened in the database.</p><pre>djpoe ➜ poe manage dbshell             <br>Poe =&gt; python ./djpoe/manage.py dbshell<br>SQLite version 3.43.2 2023-10-10 13:08:14<br>Enter &quot;.help&quot; for usage hints.<br>sqlite&gt; .mode column<br>sqlite&gt; .headers on<br>sqlite&gt; select * from account_emailaddress;<br>id  verified  primary  user_id  email            <br>--  --------  -------  -------  -----------------<br>1   0         1        1        email@example.com<br>sqlite&gt; select * from auth_user;<br>id  password           last_login        is_superuser  username  last_name  email              is_staff  is_active  date_joined                 first_name<br>--  -----------------  ----------------  ------------  --------  ---------  -----------------  --------  ---------  --------------------------  ----------<br>1   pbkdf2_sha256$***  2024-09-09 04:44  0             zalun                email@example.com  0         1          2024-09-08 17:40:41.113042            </pre><p>We’ve got two new entries. One in account_emailaddress and another in auth_user.</p><p>We’ve configured the django-allauth for regular accounts. Using the location bar to navigate the login and logout pages is inconvenient. Let’s create a trivial logged-in user indicator.</p><h3>Accounts pages navigation</h3><p>We need a link to the login page when the user is logged out and to the log-out page for an authenticated user. We will also print the current user’s username.</p><p>Our homepage is created with just the View in the djpoe/helloworld/views.py file. We need to add a template. Let’s create a static one first. In the djpoe/helloworld/templates/helloworld/index.html:</p><pre>{# djpoe/helloworld/templates/helloworld/index.html #}<br>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<br>    &lt;title&gt;Hello, World!&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;h1&gt;Hello, World!&lt;/h1&gt;<br>    &lt;p&gt;This is a simple Django template from the helloworld app.&lt;/p&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>We need to connect it to the View.</p><pre># djpoe/helloworld/views.py<br>&quot;&quot;&quot;Views for the helloworld app.&quot;&quot;&quot;<br><br>from django.http import HttpRequest, HttpResponse<br>from django.shortcuts import render<br><br><br>def index(request: HttpRequest) -&gt; HttpResponse:<br>    &quot;&quot;&quot;Return a simple greeting.&quot;&quot;&quot;<br>    return render(request, &quot;helloworld/index.html&quot;)</pre><p>We’ve got an excellent Hello World page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*CZfGPtL30B9PTNzcrAETww.png" /></figure><p>Unfortunately, it breaks our tests:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oDtFIACO5mb7v6i6yTRhKg.png" /></figure><p>We need to update it. Let’s check whether the “Hello, World!” string exists in the rendered context.</p><pre># tests/test_helloworld.py<br>import pytest<br>from django.test import Client<br><br><br>@pytest.mark.django_db<br>def test_helloworld_page(client: Client):<br>    response = client.get(&quot;/&quot;)<br><br>    assert response.status_code == 200<br>    assert &quot;Hello, World!&quot; in response.content.decode()</pre><p>As our tests are working, we can add the authentication links.</p><pre>{# djpoe/helloworld/templates/helloworld/index.html #}<br>{# ... #}<br>&lt;h1&gt;Hello, World!&lt;/h1&gt;<br>{% if user.is_authenticated %}<br>    &lt;p&gt;Welcome, {{ user.username }}!&lt;/p&gt;<br>    &lt;a href=&quot;{% url &#39;account_logout&#39; %}&quot;&gt;Logout&lt;/a&gt;<br>{% else %}<br>    &lt;p&gt;Welcome, guest!&lt;/p&gt;<br>    &lt;a href=&quot;{% url &#39;account_login&#39; %}&quot;&gt;Login&lt;/a&gt;<br>    &lt;a href=&quot;{% url &#39;account_signup&#39; %}&quot;&gt;Sign Up&lt;/a&gt;<br>{% endif %}<br>{# ... #}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*kE7ZQ6PYac2-2ErnwyAKEw.png" /><figcaption>Homepage for a guest</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*Q9EhP_1rEFG7au-S4gEyxg.png" /><figcaption>Homepage for an authenticated user</figcaption></figure><p>We’ll test this behavior. For this, we will authenticate the user from the fixture with the client.login() feature.</p><pre># tests/test_helloworld.py<br>import pytest<br>from django.contrib.auth import get_user_model<br>from django.test import Client<br><br>User = get_user_model()<br><br>@pytest.fixture(name=&quot;user&quot;)<br>def user_fixture():<br>    return User.objects.create_user(username=&quot;testuser&quot;, password=&quot;12345&quot;)<br><br># ... (Hello, World! test)<br><br>@pytest.mark.django_db<br>def test_homepage_unauthenticated(client: Client):<br>    response = client.get(&quot;/&quot;)<br><br>    assert response.status_code == 200<br>    assert &quot;Welcome, guest!&quot; in response.content.decode()<br>    assert &quot;Login&quot; in response.content.decode()<br>    assert &quot;Sign Up&quot; in response.content.decode()<br>    assert &quot;Logout&quot; not in response.content.decode()<br><br><br>@pytest.mark.django_db<br>def test_homepage_authenticated(client: Client, user):<br>    client.login(username=&quot;testuser&quot;, password=&quot;12345&quot;)<br><br>    response = client.get(&quot;/&quot;)<br><br>    assert response.status_code == 200<br>    assert f&quot;Welcome, {user.username}!&quot; in response.content.decode()<br>    assert &quot;Logout&quot; in response.content.decode()<br>    assert &quot;Login&quot; not in response.content.decode()<br>    assert &quot;Sign Up&quot; not in response.content.decode()</pre><h4>Styling</h4><p>The site is working, but it’s just a little dull. It’s <a href="https://docs.allauth.org/en/latest/common/templates.html">intentional</a>:</p><blockquote>The templates that are offered out of the box are intentionally plain and without any styling. We do not want to pick a side in the multitudes of frontend styling options out there, and the look and feel typically should be adjusted to match the branding of your project.</blockquote><p>We will copy the templates as suggested and make minimal changes to avoid going deep into styling. In the end, this is going to be just a boilerplate for future projects. I assume each one will look a little different.</p><p>I asked <a href="https://duckduckgo.com/?t=ffab&amp;q=boilerplate+css+cdn&amp;ia=web">DuckDuckGo</a>, and the first link was <a href="https://cdnjs.com/libraries/skeleton">Skeleton</a>. Adding it to the homepage is trivial:</p><pre>{# djpoe/helloworld/templates/helloworld/index.html #}<br>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;&lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<br>    &lt;title&gt;Hello, World!&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css&quot; integrity=&quot;sha512-EZLkOqwILORob+p0BXZc+Vm3RgJBOe1Iq/0fiI7r/wJgzOFZMlsqTa29UEl6v6U6gsV4uIpsNZoV32YZqrCRCQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;<br>&lt;/head&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*Ycge_EVL3DUdm1lmeYCBXQ.png" /></figure><p>The result is acceptable, and I will not waste more time.</p><p>We need to add this file to all of the django-allauth pages. Let’s overwrite the base layout. It can be done with a script. I copied all the Allauth templates, but they are too many files. The allauth/loayout/base.html is extended in all templates. It will be enough.</p><p>Finding the base path of the package is some fun in itself:</p><pre>djpoe ➜ python<br>&gt;&gt;&gt; import allauth<br>&gt;&gt;&gt; print(allauth.__path__)<br>[&#39;/Users/piotrzalewa/Projects/Medium/djpoe/.venv/lib/python3.12/site-packages/allauth&#39;]</pre><p>We can now dynamically find the directory of allauth package and copy the file we want.</p><pre>djpoe ➜ mkdir -p djpoe/templates/allauth/layouts<br>djpoe ➜ cp $(python -c &quot;import allauth; print(allauth.__path__[0])&quot;)/templates/allauth/layouts/base.html djpoe/templates/allautallauth/layouts</pre><p>We want Django to use the templates from the templates directory.</p><pre># djpoe/djpoe/settings.py<br>TEMPLATES = [<br>    {<br>        &quot;BACKEND&quot;: &quot;django.template.backends.django.DjangoTemplates&quot;,<br>        &quot;DIRS&quot;: [<br>            BASE_DIR / &quot;templates&quot;,<br>        ],<br>        # ...<br>    },<br>]</pre><p>Now, we will add the Skeleton CSS to the copied base.html.</p><pre>{# djpoe/templates/allauth/layout/base.html&quot; #}<br>{% load i18n %}<br>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>    &lt;head&gt;<br>        {# ... #}<br>        &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css&quot; integrity=&quot;sha512-EZLkOqwILORob+p0BXZc+Vm3RgJBOe1Iq/0fiI7r/wJgzOFZMlsqTa29UEl6v6U6gsV4uIpsNZoV32YZqrCRCQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;<br>    &lt;/head&gt;<br>{# ... #}</pre><p>It’s slightly less eye-sore than before. And we will leave it as it is. In the actual project, I’d probably use something like <a href="https://github.com/danihodovic/django-allauth-ui">django-allauth-ui</a>.</p><h3>Google Single Sign-On</h3><p>Great! Let’s dive into the exciting world of social login. Now that we’ve covered the basics of django-allauth configuration, it’s time to supercharge our authentication system with OAuth magic!</p><h4>Install dependencies</h4><p>django-allauth app needs some dependencies installed for it to be integrated to Google.</p><pre>djpoe ➜ poetry add &quot;django-allauth[socialaccount,google]&quot;<br>Using version ^64.2.1 for django-allauth<br><br>Updating dependencies<br>Resolving dependencies... (0.4s)<br><br>Package operations: 11 installs, 0 updates, 0 removals<br><br>  - Installing pycparser (2.22)<br>  - Installing certifi (2024.8.30)<br>  - Installing cffi (1.17.1)<br>  - Installing charset-normalizer (3.3.2)<br>  - Installing idna (3.9)<br>  - Installing urllib3 (2.2.3)<br>  - Installing cryptography (43.0.1)<br>  - Installing oauthlib (3.2.2)<br>  - Installing requests (2.32.3)<br>  - Installing pyjwt (2.9.0)<br>  - Installing requests-oauthlib (2.0.0)<br><br>djpoe ➜ poetry export --without-hashes --format=requirements.txt &gt; requirements.txt<br><br>djpoe ➜ poe migrate<br>Operations to perform:<br>  Apply all migrations: account, admin, auth, contenttypes, sessions, socialaccount<br>Running migrations:<br>  Applying socialaccount.0001_initial... OK<br>  Applying socialaccount.0002_token_max_lengths... OK<br>  Applying socialaccount.0003_extra_data_default_dict... OK<br>  Applying socialaccount.0004_app_provider_id_settings... OK<br>  Applying socialaccount.0005_socialtoken_nullable_app... OK<br>  Applying socialaccount.0006_alter_socialaccount_extra_data... OK</pre><p><strong>It’s good to run the test here</strong> to check if everything is OK. Initially, I forgot to add the socialaccount to the poetry add command, and the failing test redirected me back to the documentation after complaining:</p><pre>  ...<br>  File &quot;/Users/piotrzalewa/Projects/Medium/djpoe/.venv/lib/python3.12/site-packages/allauth/socialaccount/providers/google/provider.py&quot;, line 1, in &lt;module&gt;<br>    import requests<br>ModuleNotFoundError: No module named &#39;requests&#39;</pre><h4>Configuration</h4><p>Again, the documentation provides us with a list of constants to configure the behavior:</p><ul><li><a href="https://docs.allauth.org/en/latest/socialaccount/configuration.html">Generic social account configuration</a></li><li><a href="https://docs.allauth.org/en/latest/socialaccount/providers/google.html">Google provider documentation</a></li></ul><p>Here is my initial setting:</p><pre># djpoe/djpoe/settings.py<br># ...<br>env = environ.FileAwareEnv(<br>    # ...<br>    # django-allauth social account settings<br>    SOCIALACCOUNT_EMAIL_AUTHENTICATION_AUTO_CONNECT=(bool, True),<br>    SOCIALACCOUNT_ONLY=(bool, False),<br>    GOOGLE_AUTH_CLIENT_ID=(str, &quot;&quot;),<br>    GOOGLE_AUTH_CLIENT_SECRET=(str, &quot;&quot;),<br>)<br><br># ...<br># Application definition<br>INSTALLED_APPS = [<br>    # ...<br>    &quot;allauth&quot;,<br>    &quot;allauth.account&quot;,<br>    &quot;allauth.socialaccount&quot;,<br>    &quot;allauth.socialaccount.providers.google&quot;,<br>    &quot;helloworld&quot;,<br>]<br><br># ...<br># django-allauth social account settings<br>SOCIALACCOUNT_EMAIL_AUTHENTICATION_AUTO_CONNECT = env(&quot;SOCIALACCOUNT_EMAIL_AUTHENTICATION_AUTO_CONNECT&quot;)<br>SOCIALACCOUNT_ONLY = env(&quot;SOCIALACCOUNT_ONLY&quot;)<br>SOCIALACCOUNT_PROVIDERS = {<br>    &quot;google&quot;: {<br>        &quot;APP&quot;: {<br>            &quot;client_id&quot;: env(&quot;GOOGLE_AUTH_CLIENT_ID&quot;),<br>            &quot;secret&quot;: env(&quot;GOOGLE_AUTH_CLIENT_SECRET&quot;),<br>        },<br>        &quot;scope&quot;: [&quot;profile&quot;, &quot;email&quot;],<br>        &quot;EMAIL_AUTHENTICATION&quot;: True,<br>        &quot;AUTH_PARAMS&quot;: {<br>            &quot;access_type&quot;: &quot;online&quot;,<br>        },<br>        &quot;OAUTH_PKCE_ENABLED&quot;: True,<br>        &quot;FETCH_USERINFO&quot;: True,<br>    }<br>}</pre><p>Let’s untangle the not-so-obvious ones.</p><ul><li>SOCIALACCOUNT_PROVIDERS[“EMAIL_AUTHENTICATION”] = TRUE We can configure the project to authenticate a user who already signed in as a regular account (email/password) using the social account. We can do this for all possible social accounts or each one separately. The safer option is to switch it on for each provider. I think it’s OK to allow this for Google.</li><li>SOCIALACCOUNT_EMAIL_AUTHENTICATION_AUTO_CONNECT (default: False) is related to the SOCIALACCOUNT_PROVIDERS[“EMAIL_AUTHENTICATION”] = TRUEIt answers the question: Should the regular account be added to social accounts after logging in with such a thing? I think it’s sane, but I leave it open to be configured with the environment variable.</li><li>SOCIALACCOUNT_ONLY (default: False) Regular account login is switched off if set to True. I’m not changing the default, but I want it configured with the environment variable.</li><li>SOCIALACCOUNT_PROVIDERS The standard workflow sets the Google account configuration using the Django admin panel. I prefer the config to be written in code.</li><li>SOCIALACCOUNT_PROVIDERS[“google”][“scope”] = [&quot;profile&quot;, &quot;email&quot;] We need access to the profile to retrieve essential details like the user’s name and profile picture. We also want email to be stored with the user’s data.</li><li>OAUTH_PKCE_ENABLED It’s a security feature that enables the use of <a href="https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow-with-pkce">Proof Key for Code Exchange (PKCE)</a> in the OAuth 2.0 flow. PKCE adds an extra layer of security for OAuth authorization, which is particularly beneficial for mobile and single-page applications because it prevents specific authorization code interception attacks.</li><li>SOCIALACCOUNT_PROVIDERS[&quot;FETCH_USERINFO&quot;] = Trueuser info endpoint will be used to populate the avatar_url for those users who have a private style of avatar_url.</li></ul><h4>Configuring Google Cloud side</h4><p>Log in and create a new project. You’ll be notified when it’s created. Please select it and navigate to the <a href="https://console.cloud.google.com/apis/credentials">Credentials</a> page.</p><p>There is a warning icon about configuring the consent screen. Start with that. Depending on the project requirements, you might specify the internal (you need to be a Google Workspace user) or external option. Confirm the form and edit the App information. We only need to add the app name, user support email, and developer’s contact information for now. After confirming with SAVE AND CONTINUE button you will be asked to define the scopes. Choose “email” and “profile” and continue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FM5lgYTXrufhIRvxnuRlPQ.png" /><figcaption>Google Cloud APIs and services — Edit app registration (2) Scopes</figcaption></figure><p>The publishing status is set to “Testing.” Google only allows test users to access the app. You need to provide a few. Again, continue, review the settings, and return to the dashboard.</p><p>Now, we need to create credentials. We’re looking into the OAuth 2.0 Client IDs section. It will be empty if no connection to Google Sign-on has been made.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dawMU8NUMR4Q6ioC19wBtw.png" /><figcaption>Google Cloud APIs and services — Credentials page</figcaption></figure><p>Click on CREATE CREDENTIALS and choose the OAuth client ID.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*svz7ltJotWpbB9ORfiRzAg.png" /></figure><p>Fill in the details. I’ve chosen to create a separate one for localhost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*NfuMXtmPEJSzXeEch2zs-g.png" /></figure><p>After creating the credentials, Google will display the Client ID and secret in the modal window. Copy or download and add them to your local .env file.</p><pre># djpoe/.env<br>DEBUG=True<br>SECRET_KEY=django-insecure-=abcde<br>GOOGLE_AUTH_CLIENT_ID=12345678-abcde.apps.googleusercontent.com<br>GOOGLE_AUTH_CLIENT_SECRET=GOCSPX-ABCdefGHIjkl</pre><p>Start the dev server (poe dev) and navigate to <a href="http://127.0.0.1:8001/accounts/login">http://127.0.0.1:8001/accounts/login</a>. There is a new section for third-party logins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*kAYA3gRi10-KmEwWpVE81A.png" /></figure><p>This link will move you to <a href="http://127.0.0.1:8001/accounts/google/login/?process=login">http://127.0.0.1:8001/accounts/google/login/?process=login</a> and display the Google login page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*S_eDYGirK7P5oyFrjzny5w.png" /></figure><p>Continue will bring you to the standard Google Sign-in page. Everything is working as expected. If the LOGIN_REDIRECT_URL is set to /, the user is redirected to the homepage.</p><p>The database has the details retrieved from Google&#39;s profile. Here, it is translated to JSON.</p><pre>{<br>  &quot;id&quot;: 1,<br>  &quot;provider&quot;: &quot;google&quot;,<br>  &quot;uid&quot;: &quot;123456789012345678&quot;,<br>  &quot;last_login&quot;: &quot;2024-09-15 19:16:17.835128&quot;,<br>  &quot;date_joined&quot;: &quot;2024-09-15 18:53:25.745574&quot;,<br>  &quot;user_id&quot;: 2,<br>  &quot;extra_data&quot;: {<br>    &quot;iss&quot;: &quot;https://accounts.google.com&quot;,<br>    &quot;azp&quot;: &quot;123456789-abcdef.apps.googleusercontent.com&quot;,<br>    &quot;aud&quot;: &quot;123456789-abcdef.apps.googleusercontent.com&quot;,<br>    &quot;sub&quot;: &quot;98765432123456789&quot;,<br>    &quot;email&quot;: &quot;example@gmail.com&quot;,<br>    &quot;email_verified&quot;: true,<br>    &quot;at_hash&quot;: &quot;aBCDEf_vabcdeFG&quot;,<br>    &quot;name&quot;: &quot;Piotr \&quot;zalun\&quot; Zalewa&quot;,<br>    &quot;picture&quot;: &quot;https://lh3.googleusercontent.com/a/ACg8ocL7QdEUBRFS9CcUoMZPUavy7VOAjlquJGIRfrUoIHkeVhhUds_T8g=s96-c&quot;,<br>    &quot;given_name&quot;: &quot;Piotr&quot;,<br>    &quot;family_name&quot;: &quot;Zalewa&quot;,<br>    &quot;iat&quot;: 1726427777,<br>    &quot;exp&quot;: 1726431377<br>  }<br>}</pre><p>django-allauth created the entry in the auth_user as well. The username is the lowercased given_name, in this case “piotr”. If another Piotr would sign up using a social account, a number will be attached. In my case, it was “piotr7”.</p><h4>Tests</h4><p>How can we test it? The signal mechanism has already been tested in django-allauth. We might test the pages in a similar way we did before.</p><p>A small refactoring… We do not need the user fixture. We used it only to get username. I’ll create the username and password fixtures in tests/conftest.py so these can be used in all tests.</p><pre># tests/conftest.py<br>import pytest<br><br><br>@pytest.fixture(name=&quot;username&quot;)<br>def username_fixture():<br>    return &quot;testuser&quot;<br><br><br>@pytest.fixture(name=&quot;password&quot;)<br>def password_fixture():<br>    return &quot;12345&quot;</pre><p>We can now simplify the test_helloworld.py</p><pre># tests/test_helloworld.py<br># ...<br><br>@pytest.mark.django_db<br>def test_homepage_authenticated(client: Client, username: str, password: str):<br>    client.login(username=username, password=password)<br><br>    response = client.get(&quot;/&quot;)<br><br>    assert response.status_code == 200<br>    assert f&quot;Welcome, {username}!&quot; in response.content.decode()<br>    assert &quot;Logout&quot; in response.content.decode()<br>    assert &quot;Login&quot; not in response.content.decode()<br>    assert &quot;Sign Up&quot; not in response.content.decode()</pre><p>And write the test to see if the link to Google appears in the login page.</p><pre># tests/test_accounts_pages.py<br>import pytest<br>from django.test import Client<br>from django.urls import reverse<br><br><br>@pytest.mark.django_db<br>def test_login_page_not_authenticated(client: Client):<br>    response = client.get(reverse(&quot;account_login&quot;))<br><br>    assert response.status_code == 200<br>    assert &quot;third-party&quot; in response.content.decode()<br>    assert &#39;&lt;a title=&quot;Google&quot; href=&quot;/accounts/google/login/?process=login&quot;&gt;Google&lt;/a&gt;&#39; in response.content.decode()</pre><h4>Connect the deployed project</h4><p>Repeat the process of creating credentials. This time, use the https://djpoe-app-kbmxj.ondigitalocean.app instead of http://127.0.0.1:8001. Apply credentials to the App’s Environment Variables setting. Check in the Encrypt option.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2fzTwtqr33Da001M8EdvZw.png" /></figure><p>We will rebase the main branch on part-5-auth one, and push to the repository.</p><pre>djpoe ➜ git checkout main<br>djpoe ➜ git rebase part-5-auth<br>Successfully rebased and updated refs/heads/main.<br>djpoe ➜ git push -f<br>Total 0 (delta 0), reused 0 (delta 0), pack-reused 0<br>To github.com:zalun/djpoe.git<br>   0f026da..18dd529  main -&gt; main</pre><p>After the deployment, navigate to the Console and migrate the database.</p><pre>&gt; python djpoe/manage.py migrate</pre><p>All is done; we’re ready to log in using the Google Oauth!</p><h3>Deployment</h3><p>After a successful build, go into the app console and run:</p><pre>$ python ./djpoe/manage.py migrate<br>$ python ./djpoe/manage.py loaddata homepage</pre><p>Unfortunately, Wagtail is loaded without images. We need to fix this bit.</p><h3>Final words</h3><p>There are plenty of other social accounts to connect using django-allauth. If you’d like to see others, please leave a comment.</p><p>Feel free to follow me here. Do not hesitate to clap, either. Funny, I know, but I enjoy them.</p><p>You will find the code used in the article in the GitHub djpoe project in <a href="https://github.com/zalun/djpoe/tree/part-5-auth">part-5-auth branch</a>.</p><p>This is continued — <a href="https://medium.com/@zalun/base-6-wagtail-cms-for-the-content-f60d8ec7ac9e">Wagtail CMS for the content</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=887cad242a38" width="1" height="1" alt=""><hr><p><a href="https://medium.com/django-unleashed/base-5-3rd-party-authentication-in-django-887cad242a38">Base 5–3rd party authentication in Django</a> was originally published in <a href="https://medium.com/django-unleashed">Django Unleashed</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Base 4 — Deploy Django on DigitalOcean]]></title>
            <link>https://medium.com/django-unleashed/4-deploy-django-on-digitalocean-dc4b713e52c2?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc4b713e52c2</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python-poetry]]></category>
            <category><![CDATA[digitalocean]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Mon, 05 Aug 2024 05:48:00 GMT</pubDate>
            <atom:updated>2024-09-09T05:39:14.684Z</atom:updated>
            <content:encoded><![CDATA[<p>In the last article from the series — <a href="https://medium.com/django-unleashed/3-lint-and-test-with-github-actions-efa80197b303?source=your_stories_page-------------------------------------">Lint and Test with GitHub Actions</a>, we created a boilerplate for a Django project with Poetry that is tested and linted when commits are pushed to the repository. The natural urge is to start adding apps. I want to wait a little and deploy the project to a public server first. That way, clients can monitor and test development early in the process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TEtrwvNvUuV-rhbZ7DgDLQ.jpeg" /><figcaption>MS Designer hallucinating the title of the article</figcaption></figure><p>There are many cloud infrastructure providers. I’m fond of DigitalOcean, as they provided the servers for <a href="https://jsfiddle.net/">JSFiddle</a> when it was making no money. JSFiddle wouldn&#39;t be where it is now if we hadn’t received help from them.</p><p>We will use <a href="https://docs.digitalocean.com/products/app-platform/">DigitalOcean’s App Platform</a> because it’s easier and more cost-effective. Creating a Kubernetes swarm is the next option if the app requires more advanced configuration in the future.</p><h3>Prepare the project</h3><h4>Gunicorn</h4><p>We will use the <a href="https://gunicorn.org/">Gunicorn</a> to serve Django from the server. We need to install it:</p><pre>djpoe ➜ poetry add gunicorn</pre><p>Let’s serve the project using gunicorn.</p><pre>djpoe ➜ cd djpoe<br>djpoe ➜ poetry run gunicorn djpoe.wsgi:application --bind 0.0.0.0:8080 --worker-tmp-dir /tmp<br>[2024-08-04 13:14:40 +0200] [60795] [INFO] Starting gunicorn 22.0.0<br>[2024-08-04 13:14:40 +0200] [60795] [INFO] Listening at: http://0.0.0.0:8080 (60795)<br>[2024-08-04 13:14:40 +0200] [60795] [INFO] Using worker: sync<br>[2024-08-04 13:14:40 +0200] [60846] [INFO] Booting worker with pid: 60846</pre><p>We can now navigate to <a href="http://localhost:8080/">http://localhost:8080</a> and see the site</p><pre>djpoe ➜ lynx http://localhost:8080</pre><pre>   The install worked successfully! Congratulations! (p1 of 2)<br>   django<br><br>   View release notes for Django 5.0<br><br>           The install worked successfully! Congratulations!<br><br>   You are seeing this page because DEBUG=True is in your<br>   settings file and you have not configured any URLs.</pre><h4>requirements.txt</h4><p>DigitalOcean’s App configuration has a project autodetection feature. It uses the requirements.txt file to recognize the project depends on Python and Django. We need to create that file with Poetry. Let’s install poetry-plugin-export in the global environment.</p><pre>djpoe ➜ ~/.venv/bin/pip3 install poetry-plugin-export</pre><p>Export the requirements.txt file and push it to the repository on the main branch</p><pre>djpoe ➜ poetry export --without-hashes --format=requirements.txt &gt; requirements.txt<br>djpoe ➜ git add requirements.txt<br>djpoe ➜ git commit -am &quot;part-4 requirements.txt added&quot;<br>djpoe ➜ git push</pre><h3>DigitalOcean’s Configuration</h3><p>Navigate to the <a href="https://cloud.digitalocean.com/apps">DigitalOcean’s App Platform</a>. Click on the <a href="https://cloud.digitalocean.com/apps/new?source_provider=github">Create App with GitHub as the Source Provider</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/1*iox-OCNvZLdR4qV5jJELVg.png" /></figure><p>The site might mention it has no access to the GitHub repositories. Grant access if needed. After clicking the “Manage Access” button, you will be asked to log in to GitHub and install DigitalOcean on the chosen repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*-cwVZtixzdf2272AY9Ibbw.png" /></figure><p>After the installation, the browser will return to the “Create Resource from Source Code” page. Then, you must choose the repository, specify the branch to auto-deploy from, and click the “Next” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*CqI1ZEJKCy7phmysBUSZcQ.png" /></figure><p>Now, DigitalOcean displays the Resources page. Let’s configure the server.</p><h4>Change “Run Command”:</h4><pre>cd djpoe; gunicorn djpoe.wsgi:application --bind 0.0.0.0:8080 --worker-tmp-dir /dev/shm</pre><h4>Set “Ports” to 8080.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*PJJP-_bIu95Rug9iRLgb7g.png" /></figure><p>We will leave the rest of the settings as they are. We want the most straightforward configuration possible.</p><p>You can edit the name in the “App Info” section. Otherwise, it would stay automated, like walrus-app, or similar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*73e2T8k8QbKKemkXK2QfRg.png" /></figure><p>Django is building the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*ztozkNyHmQYTuz7fibE4pw.png" /></figure><p>Build logs failed on the collectstatic step. This shows us two things.</p><p>First, recognizing the Django project configures the required deployment steps automatically. Cool.</p><p>Second, funny enough, DigitalOcean is using the Heroku commands…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RIFmM67qda9OAXRJwEyV_w.png" /></figure><p>I’ve received an email mentioning it as well:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*L7c0ighQC7yqDcS489s5WA.png" /></figure><p>Let’s replicate the collectstatic error locally.</p><pre>djpoe ➜ poe manage collectstatic --noinput<br>[...]<br>django.core.exceptions.ImproperlyConfigured: You&#39;re using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.</pre><p>We need to modify the djpoe/djpoe/settings.py static files configuration:</p><pre># settings.py<br># [...]<br>STATIC_URL = &#39;/static/&#39;<br>STATIC_ROOT = Path(BASE_DIR).joinpath(&#39;staticfiles&#39;)<br>STATICFILES_DIRS = (Path(BASE_DIR).joinpath(&#39;static&#39;),)</pre><p>We must create the empty djpoe/static directory to prevent the warning.</p><pre>djpoe ➜ mkdir djpoe/static</pre><p>And recheck it:</p><pre>djpoe ➜ poe manage collectstatic --noinput<br>131 static files copied to &#39;/Users/zalun/Projects/djpoe/djpoe/staticfiles&#39;.</pre><p>The collected files should not be committed, so we need to add the djpoe/staticfiles directory to the .gitignore file.</p><pre>djpoe ➜ echo staticfiles &gt;&gt; .gitignore<br>djpoe ➜ git commit -am &quot;part-4 staticfiles configuration&quot;<br>djpoe ➜ git push</pre><p>We can see the app is deployed now in the app dashboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TrCA-7M74y73LOeOrikwTA.png" /></figure><p>We can now navigate to the deployed site on the address provided by DigitalOcean — <a href="https://djpoe-app-kbmxj.ondigitalocean.app/">https://djpoe-app-kbmxj.ondigitalocean.app/</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*LzFTHvpMqj6s3F0YvDONbw.png" /></figure><p>We’re not there yet. We need to get the deployment server out of the DEBUG mode, and make it use the PostgreSQL database.</p><h3>Change the Resource Size.</h3><p>I just realized I’m spending $24 per month on this project without running a database. We can quickly change this in Component Settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*rpenglABeMgMshNDV50d3Q.png" /></figure><h3>Set to the production mode.</h3><p>The DEBUG constant is defined in thesettings.py file. We want it to be True in the local environment, but False if deployed. Let’s set an environment variable to drive it. We will use the <a href="https://pypi.org/project/django-environ/">django-environ</a> package so it will read the values from environment variables or the djpoe/djpoe/.env file.</p><p>In the settings.py we will import the environ module, add the definition of the DEBUG env with setting it to False by default and read the values from .env file.</p><pre># [...]<br>import environ<br><br>env = environ.FileAwareEnv(<br>    DEBUG=(bool, False)<br>)<br>environ.Env.read_env()<br># [...]</pre><p>Now, in the same file, we will change the DEBUG constant value to read from the declared env method:</p><pre>DEBUG = env(&quot;DEBUG&quot;)</pre><p>We can run the dev server with or without DEBUG mode:</p><pre># With DEBUG mode<br>djpoe ➜ DEBUG=1 poe dev<br><br># Without DEBUG mode (default is False)<br>djpoe ➜ poe dev</pre><p>To set the local environment in DEBUG mode by default, we need to create the .env file with the right setting. We will create the djpoe/djpoe/.env.local file, set the DEBUG environment value and copy it to the djpoe/djpoe/.env. This file is already ignored by git.</p><pre>djpoe ➜ echo DEBUG=True djpoe/djpoe/.env.local<br>djpoe ➜ cp djpoe/djpoe/.env.local djpoe/djpoe/.env</pre><p>We can always switch off the DEBUG mode locally by calling:</p><pre>djpoe ➜ DEBUG=0 poe dev</pre><p>Unfortunately, now the server is sending us an 404 error as we have specified no view for the homepage.</p><h3>Create the “helloworld” app.</h3><p>Let’s create the most straightforward ever app:</p><pre>djpoe ➜ mkdir djpoe/helloworld<br>djpoe ➜ touch djpoe/helloworld/__index__.py</pre><p>Now we will create the views.py file in the new helloworld directory:</p><pre># djpoe/helloworld/views.py<br>&quot;&quot;&quot;Views for the helloworld app.&quot;&quot;&quot;<br><br>from django.http import HttpRequest, HttpResponse<br><br>def index(_: HttpRequest) -&gt; HttpResponse:<br>    &quot;&quot;&quot;Return a simple greeting.&quot;&quot;&quot;<br>    return HttpResponse(&quot;Hello, World!&quot;)</pre><p>Create the failing test_helloworld.py:</p><pre># tests/test_helloworld.py<br>import pytest<br>from django.test import Client<br><br>@pytest.mark.django_db()<br>def test_helloworld_page(client: Client):<br>    response = client.get(&quot;/&quot;)<br><br>    assert response.status_code == 200<br>    assert response.content == b&quot;Hello, World!&quot;</pre><p>We will also add the helloworld app to the coverage:</p><pre># pytest.ini<br># [...]<br>addopts =<br>    --cov djpoe/djpoe<br>    --cov djpoe/helloworld<br># [...]</pre><p>Connect the app to the project in settings.py:</p><pre># djpoe/djpoe/settings.py<br># [...]<br>INSTALLED_APPS = [<br>    # [...]<br>    &quot;helloworld&quot;,<br>]<br># [...]</pre><p>And finally, add it to the urls.py:</p><pre># djpoe/djpoe/urls.py<br># [...]<br>from helloworld.views import index<br><br>urlpatterns = [<br>    path(&quot;admin/&quot;, admin.site.urls),<br>    path(&quot;&quot;, index, name=&quot;homepage&quot;),<br>]</pre><p>Let’s test:</p><pre>djpoe ➜ poe test<br>...<br>---------- coverage: platform darwin, python 3.12.4-final-0 ----------<br>Name                           Stmts   Miss Branch BrPart  Cover   Missing<br>--------------------------------------------------------------------------<br>djpoe/djpoe/__init__.py            0      0      0      0   100%<br>djpoe/djpoe/settings.py           23      0      0      0   100%<br>djpoe/djpoe/urls.py                4      0      0      0   100%<br>djpoe/helloworld/__init__.py       0      0      0      0   100%<br>djpoe/helloworld/views.py          3      0      0      0   100%<br>--------------------------------------------------------------------------<br>TOTAL                             30      0      0      0   100%<br><br>============================= 6 passed in 0.89s =============================</pre><p>And “see” the page:</p><pre>djpoe ➜ DEBUG=0 poe dev<br>Starting development server at http://127.0.0.1:8001/</pre><pre>djpoe ➜ curl http://localhost:8001<br>Hello, World!%   </pre><p>Let’s commit the helloworld and push. After the rebuild:</p><pre>djpoe ➜ curl https://djpoe-app-kbmxj.ondigitalocean.app/<br>Hello, World!%</pre><p>We now have a fully working server in production mode. Let’s go with the next step: adding a database.</p><h3>Add dev database.</h3><p>In the djpoe-app dashboard Click on “Create” and choose “Create/Attach Database”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GnXk-Snl41LenZyBSRJ77g.png" /></figure><p>The default database is called just db, I’ve renamed it to djpoe-db, just in case it shows up outside of the App Dashboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*vMPwJrq181PnfMnv2mWbYw.png" /></figure><p>That’s it! Database added.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*qqK82B9EAkmW6PGdWQU41Q.png" /></figure><h4>Check the automatic configuration</h4><p>We need to configure the project to use it. After the app will redeploy itself, the environment variable DATABASE_URL will already be provided by DigitalOcean.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*p359vWfaMhl71yvuBYLoBw.png" /></figure><p>We can print it in the app’s Console:</p><pre>$ echo $DATABASE_URL<br>postgresql://djpoe-db:XXX@app-XXX-do-user-XXX-0.h.db.ondigitalocean.com:12345/djpoe-db?sslmode=require</pre><p>That gives us the ability to detect and use it automatically in our settings.</p><h4>Configure the project to use the created database.</h4><p>Let’s add the psycopg2 package to handle the PostgreSQL connection</p><pre>djpoe ➜ poetry add psycopg2-binary</pre><p>We will also edit the settings.py to add the DATABASE_URL variable from the environment. When we will start using the PostgreSQL locally we will just add the DATABASE_URL to the .env file.</p><pre>env = environ.FileAwareEnv(<br>    DEBUG=(bool, False),<br>    DATABASE_URL=(str, &quot;sqlite&quot;),<br>)</pre><p>And then later in the database configuration section:</p><pre>DATABASE_URL = env(&quot;DATABASE_URL&quot;)<br><br>if DATABASE_URL == &quot;sqlite&quot;:<br>    DATABASES = {<br>        &quot;default&quot;: {<br>            &quot;ENGINE&quot;: &quot;django.db.backends.sqlite3&quot;,<br>            &quot;NAME&quot;: BASE_DIR / &quot;db.sqlite3&quot;,<br>        }<br>    }<br>else:<br>    db_config = urlparse(DATABASE_URL)<br>    DATABASES = {<br>        &quot;default&quot;: {<br>            &quot;ENGINE&quot;: &quot;django.db.backends.postgresql_psycopg2&quot;,<br>            &quot;NAME&quot;: db_config.path[1:],<br>            &quot;USER&quot;: db_config.username,<br>            &quot;PASSWORD&quot;: db_config.password,<br>            &quot;HOST&quot;: db_config.hostname,<br>            &quot;PORT&quot;: db_config.port,<br>            &quot;OPTIONS&quot;: {&quot;sslmode&quot;: &quot;require&quot;},<br>        }<br>    }</pre><p>We can also check if the settings are correctly created. Add to settings.py:</p><pre>print(DATABASES[&quot;default&quot;])</pre><pre>djpoe ➜  DATABASE_URL=postgresql://user:pass@host:123/database?sslmode=require poe dev<br>{<br>  &#39;ENGINE&#39;: &#39;django.db.backends.postgresql_psycopg2&#39;, <br>  &#39;NAME&#39;: &#39;db&#39;, <br>  &#39;USER&#39;: &#39;user&#39;, <br>  &#39;PASSWORD&#39;: &#39;pass&#39;, <br>  &#39;HOST&#39;: &#39;host&#39;, <br>  &#39;PORT&#39;: 123, <br>  &#39;OPTIONS&#39;: {&#39;sslmode&#39;: &#39;require&#39;}<br>}</pre><p>After we commit and push the changes, the project should use the database.</p><pre>djpoe ➜ poetry export --without-hashes --format=requirements.txt &gt; requirements.txt<br>djpoe ➜ git commit -am &quot;part-4 connecting to the database&quot;<br>djpoe ➜ git push</pre><p>In the app’s Console tab, call the migration scripts:</p><pre>$ python djpoe/manage.py migrate        <br>System check identified some issues:<br><br>WARNINGS:<br>?: (staticfiles.W004) The directory &#39;/workspace/djpoe/static&#39; in the STATICFILES_DIRS setting does not exist.<br>Operations to perform:<br>  Apply all migrations: admin, auth, contenttypes, sessions<br>Running migrations:<br>  Applying contenttypes.0001_initial... OK<br>  Applying auth.0001_initial... OK<br>  Applying admin.0001_initial... OK<br>  Applying admin.0002_logentry_remove_auto_add... OK<br>  Applying admin.0003_logentry_add_action_flag_choices... OK<br>  Applying contenttypes.0002_remove_content_type_name... OK<br>  Applying auth.0002_alter_permission_name_max_length... OK<br>  Applying auth.0003_alter_user_email_max_length... OK<br>  Applying auth.0004_alter_user_username_opts... OK<br>  Applying auth.0005_alter_user_last_login_null... OK<br>  Applying auth.0006_require_contenttypes_0002... OK<br>  Applying auth.0007_alter_validators_add_error_messages... OK<br>  Applying auth.0008_alter_user_username_max_length... OK<br>  Applying auth.0009_alter_user_last_name_max_length... OK<br>  Applying auth.0010_alter_group_name_max_length... OK<br>  Applying auth.0011_update_proxy_permissions... OK<br>  Applying auth.0012_alter_user_first_name_max_length... OK<br>  Applying sessions.0001_initial... OK</pre><p>All is fine. We can check the configuration to confirm we’re using the configured database. In the app’s console again:</p><pre>$ python djpoe/manage.py shell<br>(InteractiveConsole)<br>&gt;&gt;&gt; from djpoe.settings import DATABASES<br>&gt;&gt;&gt; print(DATABASES[&quot;default&quot;][&quot;NAME&quot;])<br>djpoe-db</pre><h3>Wrapping Up</h3><p>The app is working on the server with a database. We can consider this a full boilerplate. In the next part, we will play with it a little more. We will add OAuth and, finally, CMS (Wagtail). For now, we’re done.</p><p>You can find the code in the <a href="https://github.com/zalun/djpoe/tree/part-4-deploy-on-digitalocean">part-4-deploy-on-digitalocean</a> branch.</p><h3>Warning</h3><p>For some reason mypy check stopped working in CI. I will fix it and update the code. That, however, showed me an issue I’d need to tackle soon. The deployment proceeded anyway. It’s not an excellent CI/CD process. There needs to be a way to stop updating the server if any of the tests in .github/workflows/check.yml would fail.</p><h3>Errata</h3><ul><li>I needed to fix mypy in the CI. There were multiple issues oriented around adding a not-needed djpoe/__init__.py. I’ve updated the branch.</li><li>The SECRET_KEY environment variable is now required. Having it hardcoded in the code is a security risk.</li><li>The djpoe/djpoe directory is now removed from test coverage. We have implemented logic for optional database settings. I can’t find a way to mock the imports. settings.py is imported before tests are run.</li></ul><h3>Errata 2</h3><p>The database configuration setting can be way simpler. The django-environ has a <a href="https://django-environ.readthedocs.io/en/latest/quickstart.html">Quickstart page</a>, and it’s worth reading.</p><p>We define the default DATABASE_URL as SQLite URL.</p><pre>    env = environ.FileAwareEnv(<br>        DEBUG=(bool, False),<br>        DATABASE_URL=(str, &quot;sqlite:///djpoe/db.sqlite3&quot;),</pre><p>And the entire database configuration can be shrunk significantly.</p><pre>DATABASES = {<br>    &quot;default&quot;: env.db(),<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc4b713e52c2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/django-unleashed/4-deploy-django-on-digitalocean-dc4b713e52c2">Base 4 — Deploy Django on DigitalOcean</a> was originally published in <a href="https://medium.com/django-unleashed">Django Unleashed</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#3 — Lint and test with GitHub Actions]]></title>
            <link>https://medium.com/django-unleashed/3-lint-and-test-with-github-actions-efa80197b303?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/efa80197b303</guid>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Sun, 14 Jul 2024 12:23:08 GMT</pubDate>
            <atom:updated>2024-09-07T06:33:17.262Z</atom:updated>
            <content:encoded><![CDATA[<h3>Base 3 — Lint and test with GitHub Actions</h3><p>In the last two articles (<a href="https://medium.com/@zalun/django-with-poetry-ea95bd5083f7">Django with Poetry</a> and <a href="https://medium.com/@zalun/linting-django-9878c7ed8feb">Linting Django</a>) we’ve created a plain Django project and added linters. We’ve made them run automatically on each commit so as not to pollute the repository with simple errors. Indeed, it’s still possible to publish without running pre-commit hooks. One can disable it or bypass the check with the<br>--no-verify switch. To be sure all merged code is linted correctly, we need to add the checks on the repository side.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jH4C-JyeP3iLhw53n5Us-Q.png" /><figcaption>Microsoft Designer hallucinating the article’s title</figcaption></figure><p>For this, we will utilize <a href="https://docs.github.com/en/actions">GitHub Actions</a>. This feature allows us to run a code on a virtual machine. Let’s create the .github/workflows directory and write our first action.</p><pre>djpoe ➜ mkdir -p .github/workflows</pre><p>We will define our <em>workflow</em> by creating the check.yml file with the following directives:</p><pre># .github/workflows/check.yml<br><br>name: Check project<br><br>on:<br>  - push<br>  - pull_request<br>jobs:<br>  check:<br>    runs-on: ubuntu-latest<br>    steps:<br>      - uses: actions/checkout@v4<br>      - uses: actions/setup-python@v5<br>        with:<br>          python-version: 3.12<br><br>      - name: Install Poetry<br>        uses: snok/install-poetry@v1<br>        with:<br>          virtualenvs-in-project: true<br>          virtualenvs-path: .venv</pre><p>Let’s go through it step by step. I will not discuss the name as it’s self-explanatory.</p><h4>on:</h4><p>It’s a list defining the actions to which this workflow will respond. We use a simple push and pull_request actions, but these can be more sophisticated. This example workflow will respond when someone pushes a tag prefaced with sandbox- :</p><pre>on:<br>  push:<br>    tags:<br>      - sandbox-*</pre><h4>jobs:</h4><p>Defines an object containing one or more jobs to be run. Each job runs in a separate environment. One can communicate between jobs by using artifacts or variables.</p><p>We’ve created one job called check. Each job requires a definition of the system to boot for it.</p><pre>runs-on: ubuntu-latest</pre><p>Then, we define the steps. For now, we only use actions that have already been prepared.</p><pre>- uses: actions/checkout@v4</pre><p>Pulls the code from the repository. Each pre-build action has its <a href="https://github.com/actions/checkout">documentation</a>.</p><pre>- uses: actions/setup-python@v5<br>  with:<br>    python-version: 3.12</pre><p>Installs python in version 3.12. Here, we pass the parameters to the action using the with object. You can check other parameters in the <a href="https://github.com/actions/setup-python/">README file</a>.</p><pre>- name: Install Poetry<br>  uses: snok/install-poetry@v1<br>  with:<br>    version: 1.8.3<br>    virtualenvs-create: true<br>    virtualenvs-in-project: true<br>    installer-parallel: true</pre><p>Installs Poetry. We will force it to create the virtual environment in the project’s directory.</p><p>Let’s commit, push, and see <a href="https://github.com/zalun/djpoe/actions">GitHub Actions</a>:</p><pre>djpoe ➜ git commit -m &quot;GHA install poetry&quot; --set-upstream origin part-3-check-with-github-actions<br>djpoe ➜ git push</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iQ1hQClr-yE0r1ePgeb5Fw.png" /><figcaption>GitHub Actions page displaying last actions</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xnSt0F88_VU3oQoa9W0zEg.png" /><figcaption>The check.yml workflow pagee</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZWDWxZ4BjkCGcnfuC5eSAg.png" /><figcaption>The check job with expanded Intall Poetry step</figcaption></figure><h4>Ruff</h4><p>We now have the environment ready to do some real action. Let’s install dependencies and check the code with ruff.</p><pre>- name: Install dependencies<br>  run: poetry install --no-interaction --no-root<br><br>- name: Ruff<br>  run: poetry run ruff check .</pre><p>Here, we’re using run to define commands executed on the virtual server. We can run multiline commands using the pipe character |</p><pre>- name: Ruff<br>  run: |<br>    source .venv/bin/activate<br>    ruff check .</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r41p6TjR1_ftSkE_I5fGpw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*8tWhgMMPfFPagaFcaJZUvw.png" /><figcaption>Successful run of Ruff</figcaption></figure><p>Let’s make a failing test:</p><pre># manage.py<br><br>import doesnotexist</pre><pre>djpoe ➜ git add djpoe manage.py<br>djpoe ➜ git commit -m &quot;GHA Test failing ruff&quot; --no-verify<br>djpoe ➜ git push<br>djpoe ➜ git reset --hard HEAD~1</pre><p>We’ve committed a file with a broken import, pushed it to the repository, and deleted the commit to reclaim the clean repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1BU0ARjXwL32NRF77Mvang.png" /><figcaption>Workflow runs with a failed ruff test</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NRY-oWtlFi7TxuzxE6wfnA.png" /><figcaption>Failed Ruff job</figcaption></figure><h3>Mypy</h3><p>New step definition:</p><pre>- name: Mypy<br>  run: poetry run mypy --install-types --check-untyped-defs --non-interactive .</pre><pre>djpoe ➜ git commit -m &quot;GHA Mypy configuration&quot;<br>djpoe ➜ git push --force</pre><p>We need to push with--force as we’ve deleted a pushed commit.</p><h3>Pytest</h3><p>We want to run pytest as well. This might be omitted in pre-commit as in some projects it takes a long time.</p><pre>- name: Pytest<br>  run: poetry run pytest --showlocals --tb=auto -ra --cov-branch --cov-report=term-missing</pre><p>It runs perfectly as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U1TMGe-9bqWTff07fYzrow.png" /></figure><p>We see the coverage at 72%. It’s way too low, but looking at files that aren’t tested, it’s not as bad. We can disable them later, for now let’s define our minimal test coverage using--cov-fail-under=95.</p><pre>- name: Pytest<br>  run: poetry run pytest --showlocals --tb=auto -ra --cov-branch --cov-report=term-missing --cov-fail-under=95</pre><pre>FAIL Required test coverage of 95% not reached. Total coverage: 72.41%</pre><p>Let’s omit not tested files in pyproject.toml file.</p><pre>[tool.coverage.run]<br>omit = [<br>    # Omit asgi and wsgi files.<br>    &quot;djpoe/djpoe/asgi.py&quot;,<br>    &quot;djpoe/djpoe/wsgi.py&quot;,<br>]</pre><pre>Required test coverage of 95% reached. Total coverage: 100.00%</pre><h3>Caching</h3><p>Looking at the check job, we see some steps took more time than the others.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UQHQXjGw31f2q6cSpnUguw.png" /></figure><p>Overall it took 39s. It’s not much, but we have no actual code yet. The significant time consuming actions are Install Poetry, Mypy, and Install dependencies. Install Poetry downloads a small installation script from GitHub so that caching wouldn’t help.</p><p>We don’t have many dependencies as we haven’t started coding yet. Installing dependencies already takes 4s. We can easily cache them following the action’s <a href="https://github.com/snok/install-poetry?tab=readme-ov-file#testing">documentation</a>.</p><pre>- uses: actions/setup-python@v5<br>  id: setup-python<br>  with:<br>    python-version: 3.12<br><br>- name: Install Poetry<br>  # [...]<br>  # no change here, removed in article for readability<br><br>- name: Load cached venv<br>  id: cached-poetry-dependencies<br>  uses: actions/cache@v4<br>  with:<br>    path: .venv<br>    key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles(&#39;**/poetry.lock&#39;) }<br><br>- name: Install dependencies<br>  if: steps.cached-poetry-dependencies.outputs.cache-hit != &#39;true&#39;<br>  run: poetry install --no-interaction --no-root</pre><p>Here, we need to change two steps and add one.</p><p>We’ve added id to the setup Python step. It’s needed as we use the output from it later on.</p><p>Load cached venv is using the actions/cache action. It caches the .venv path. It will update the cache each time the key changes. The key is built from runner.os that depends on the check job setting, the python version received from the setup-python step’s output, and a hash created from the poetry.lock file.</p><p>We’ve added an if condition to the Install dependencies step. It will be run only if the cache-hit output of the cached-poetry-dependencies is not &quot;true&quot;.</p><p>After running it, we see the Cache not found message, which is expected, but also, the Mypy step is failing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i3Ldjtaf_01LdxDXEz4OUw.png" /></figure><p>After <a href="https://github.com/python/mypy/issues/10863">some digging</a>, we can add the workaround to the mypy command and try again.</p><pre>- name: Mypy<br>  run: poetry run mypy --install-types --check-untyped-defs --non-interactive  --cache-dir=.mypy_cache/ .</pre><p>Unfortunately, the Cache not found message appears again. We can look into the Install poetry step, and we see a not wanted message:</p><pre>Run snok/install-poetry@v1<br>  with:<br>    version: 1.8.3<br>    virtualenvs-create: true<br>    virtualenvs-in-project: true<br>    installer-parallel: true<br>    virtualenvs-path: {cache-dir}/virtualenvs</pre><p>The poetry install command was not using the .venv directory. It might be an error in the action’s documentation. Let’s <a href="https://github.com/snok/install-poetry/issues/151">create an issue</a>, the fix is quite simple, so I’ve attached <a href="https://github.com/snok/install-poetry/pull/152">the pull request</a> as well. We need to either change the cached directory or the virtualenvs-path. Let’s do the latter.</p><pre>- name: Install Poetry<br>  uses: snok/install-poetry@v1<br>  with:<br>    version: 1.8.3<br>    virtualenvs-create: true<br>    virtualenvs-in-project: true<br>    installer-parallel: true<br>    virtualenvs-path: .venv</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5WX19bY8E8km6h1QT8YXzA.png" /></figure><p>The cache is now restored, and we skimmed a precious 1s as Load cache venv took 3s and Install dependencies was omitted. This will change in the future after some dependencies will be installed. We can also see the key containing Linux, 3.12.4, and a hash string.</p><p>To fix the Mypy step, we’ve set the cache directory, and it no longer installs the types every time. Overall, we shaved about 5s. It changes from run to run.</p><p>You can find the code in the <a href="https://github.com/zalun/djpoe/tree/part-3-check-with-github-actions">public repository</a>.</p><p>Follow me for more parts. I want to create the blueprint for a deployable Django with CMS by <a href="https://wagtail.org/">Wagtail</a>.</p><h3>Update 1</h3><p>The <a href="https://github.com/snok/install-poetry/pull/152">pull request</a> to snok/install-poetry got merged \o/</p><h3>Update 2</h3><p>The next part got published: <a href="https://medium.com/@zalun/4-deploy-django-on-digitalocean-dc4b713e52c2">#4 Deploy on DigitalOcean</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efa80197b303" width="1" height="1" alt=""><hr><p><a href="https://medium.com/django-unleashed/3-lint-and-test-with-github-actions-efa80197b303">#3 — Lint and test with GitHub Actions</a> was originally published in <a href="https://medium.com/django-unleashed">Django Unleashed</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Base 2 — Linting Django]]></title>
            <link>https://medium.com/django-unleashed/linting-django-9878c7ed8feb?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/9878c7ed8feb</guid>
            <category><![CDATA[pylint]]></category>
            <category><![CDATA[mypy]]></category>
            <category><![CDATA[typing]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Thu, 27 Jun 2024 21:47:28 GMT</pubDate>
            <atom:updated>2024-08-16T09:02:22.187Z</atom:updated>
            <content:encoded><![CDATA[<p>I enjoy the beauty of code, so I’ve chosen Python. The way the code looks is part of the syntax. Writing ugly code in Python is possible, but it’s harder than in other languages. Linting has been an essential part of code development for several years now. Having a team follow one chosen standard increases the readability and mobility of the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fx9SYBdGzL6BBa-XshHz7w.png" /><figcaption>Linting Django hallucinated by Dall-E3</figcaption></figure><p>This is the second article from the series. Please look at <a href="https://medium.com/@zalun/django-with-poetry-ea95bd5083f7">Django with Poetry</a> for the first one. We’re starting from the branch <a href="https://medium.com/github.com/zalun/djpoe/tree/part-1">part-1</a> of <a href="http://github.com/zalun/djpoe/">the </a><a href="http://github.com/zalun/djpoe/">djpoe repository</a>. This article will install and configure tools - <a href="https://docs.astral.sh/ruff/">Ruff</a>, <a href="https://pylint.readthedocs.io/en/stable/">Pylint</a>, <a href="https://www.mypy-lang.org/">Mypy</a>, and <a href="https://pre-commit.com/">pre-commit</a>. In my opinion, these tools are essential to each more immense code repository. We will also install <a href="https://poethepoet.natn.io/">PoeThePoet</a>, which simplifies running management commands.</p><h3>Ruff</h3><p>Until recently, I’ve been using a bunch of tools to lint code (flake8, black), keep imports in order (isort) and take care of docstrings (pydocstring). It takes some effort to know them all and to configure them in a way that works well. Then came <a href="https://docs.astral.sh/ruff/linter/">ruff</a>. “One tool to rule them all.”</p><p>Let’s install ruff with Poetry</p><pre>djpoe ➜ poetry add --group dev ruff<br>...<br>  - Installing ruff (0.4.10)<br>...</pre><p>Ruff’s main command is check . <a href="https://docs.astral.sh/ruff/">The documentation</a> mentions the following usage:</p><pre>ruff check                  # Lint all files in the current directory.<br>ruff check --fix            # Lint all files in the current directory, and fix any fixable errors.<br>ruff check --watch          # Lint all files in the current directory, and re-lint on change.<br>ruff check path/to/code/    # Lint all files in `path/to/code` (and any subdirectories).</pre><p>We will manually use ruff check. Linting-specific files will be used in pre-commit.</p><pre>djpoe ➜ poetry run ruff check<br>All checks passed!</pre><p>If we create an error in the files, it will be reported. I’ve added an unused import to the manage.py . We can see the error and fix it with these two commands.</p><pre>djpoe ➜ poetry run ruff check                <br>djpoe/manage.py:6:8: F401 [*] `pytest` imported but unused<br>Found 1 error.<br>[*] 1 fixable with the `--fix` option.<br>djpoe ➜ poetry run ruff check --fix<br>Found 1 error (1 fixed, 0 remaining).</pre><p>The Ruff’s format command is used to format the code. One can configure the VSCode to use it.</p><pre>djpoe ➜ poetry run ruff format     <br>1 file reformatted, 9 files left unchanged</pre><p>The formatted file is urls.py. One line was added after the module’s docstring.</p><h4>Configuration</h4><p>We will configure Ruff in our main pyproject.toml file. The full list could look like this:</p><pre># pyproject.toml<br>...<br><br>[tool.ruff]<br>exclude = [<br>    &quot;.direnv&quot;,<br>    &quot;.git&quot;,<br>    &quot;.git-rewrite&quot;,<br>    &quot;.mypy_cache&quot;,<br>    &quot;.pytest_cache&quot;,<br>    &quot;.ruff_cache&quot;,<br>    &quot;.venv&quot;,<br>    &quot;.vscode&quot;,<br>    &quot;node_modules&quot;,<br>]<br>line-length = 120<br>indent-width = 4<br>target-version = &quot;py312&quot;<br><br>[tool.ruff.lint]<br>ignore = [&quot;ISC001&quot;, &quot;D203&quot;, &quot;D213&quot;]<br>select = [<br>    &quot;ANN&quot;, # flake8-annotations<br>    &quot;ARG&quot;, # flake8-arguments<br>    &quot;B&quot;,   # flake8-bugbear<br>    &quot;C4&quot;,  # flake8-comprehensions<br>    &quot;C90&quot;, # mccabe complexity<br>    &quot;D&quot;,   # pydocstyle<br>    &quot;DJ&quot;,  # flake8-django<br>    &quot;DTZ&quot;, # flake8-datetimez<br>    &quot;E&quot;,   # pycodestyle<br>    &quot;EM&quot;,  # flake8-errmsg<br>    &quot;F&quot;,   # flake8<br>    &quot;G&quot;,   # flake8-logging-format<br>    &quot;I&quot;,   # isort<br>    &quot;ISC&quot;, # flake8-implicit-str-concat<br>    &quot;LOG&quot;, # flake8-logging<br>    &quot;N&quot;,   # pep8-naming<br>    &quot;PIE&quot;, # flake8-pie<br>    &quot;PT&quot;,  # flake8-pytest-style<br>    &quot;PTH&quot;, # flake8-use-pathlib<br>    &quot;RET&quot;, # flake8-return<br>    &quot;RUF&quot;, # ruff-specific rules<br>    &quot;Q&quot;,   # flake8-quotes<br>    &quot;SIM&quot;, # flake8-simplify<br>    &quot;T10&quot;, # flake8-print<br>    &quot;TCH&quot;, # flake8-type-checking<br>    &quot;TRY&quot;, # tryceratops<br>    &quot;UP&quot;,  # pyupgrade<br>]<br>fixable = [&quot;ALL&quot;]<br>unfixable = []<br># Allow unused variables when underscore-prefixed.<br>dummy-variable-rgx = &quot;^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$&quot;<br><br>[tool.ruff.format]<br>quote-style = &quot;double&quot;<br>indent-style = &quot;space&quot;<br>skip-magic-trailing-comma = false<br>line-ending = &quot;auto&quot;<br>docstring-code-format = true<br>docstring-code-line-length = &quot;dynamic&quot;</pre><p>Most of the settings are self-explanatory. The select list is essential. It defines the tools to be used to lint the code. You can decide to use all of the possible linters. It would be enough to replace that list with [&quot;ALL&quot;]. The ignore list removes conflicting rules.</p><p>Without any code change, we’ve got:</p><pre>djpoe ➜ poetry run ruff format<br>1 file reformatted, 9 files left unchanged</pre><p>The formatted file is tests/test_admin_page.py. The code fits in one line as we allow lines to be 120 characters long now.</p><p>Running the linters is more worrying…</p><pre>djpoe ➜ poetry run ruff check <br>djpoe/djpoe/__init__.py:1:1: D104 Missing docstring in public package<br>djpoe/djpoe/asgi.py:1:1: D212 [*] Multi-line docstring summary should start at the first line<br>djpoe/djpoe/settings.py:1:1: D212 [*] Multi-line docstring summary should start at the first line<br>djpoe/djpoe/settings.py:90:89: E501 Line too long (91 &gt; 88)<br>djpoe/djpoe/urls.py:1:1: D212 [*] Multi-line docstring summary should start at the first line<br>djpoe/djpoe/urls.py:6:1: D413 [*] Missing blank line after last section (&quot;Examples&quot;)<br>djpoe/djpoe/urls.py:6:1: D411 [*] Missing blank line before section (&quot;Examples&quot;)<br>djpoe/djpoe/urls.py:6:1: D407 [*] Missing dashed underline after section (&quot;Examples&quot;)<br>djpoe/djpoe/urls.py:6:1: D406 [*] Section name should end with a newline (&quot;Examples&quot;)<br>djpoe/djpoe/wsgi.py:1:1: D212 [*] Multi-line docstring summary should start at the first line<br>djpoe/manage.py:8:5: ANN201 Missing return type annotation for public function `main`<br>djpoe/manage.py:14:15: TRY003 Avoid specifying long messages outside the exception class<br>djpoe/manage.py:15:13: EM101 Exception must not use a string literal, assign to variable first<br>tests/__init__.py:1:1: D104 Missing docstring in public package<br>tests/test_admin_page.py:1:1: D100 Missing docstring in public module<br>tests/test_admin_page.py:8:1: PT023 [*] Use `@pytest.mark.django_db()` over `@pytest.mark.django_db`<br>tests/test_admin_page.py:9:5: ANN201 Missing return type annotation for public function `test_render_admin_login`<br>tests/test_admin_page.py:9:5: D103 Missing docstring in public function<br>tests/test_admin_page.py:17:1: PT023 [*] Use `@pytest.mark.django_db()` over `@pytest.mark.django_db`<br>tests/test_admin_page.py:18:5: ANN201 Missing return type annotation for public function `test_admin_redirect_if_not_superuseer`<br>tests/test_admin_page.py:18:5: D103 Missing docstring in public function<br>tests/test_admin_page.py:30:1: PT023 [*] Use `@pytest.mark.django_db()` over `@pytest.mark.django_db`<br>tests/test_admin_page.py:31:5: ANN201 Missing return type annotation for public function `test_admin_success_for_superuser`<br>tests/test_admin_page.py:31:5: D103 Missing docstring in public function<br>tests/test_auth_models_user.py:1:1: D100 Missing docstring in public module<br>tests/test_auth_models_user.py:6:1: PT023 [*] Use `@pytest.mark.django_db()` over `@pytest.mark.django_db`<br>tests/test_auth_models_user.py:7:5: ANN201 Missing return type annotation for public function `test_user_create`<br>tests/test_auth_models_user.py:7:5: D103 Missing docstring in public function<br>tests/test_dummy.py:1:1: D100 Missing docstring in public module<br>tests/test_dummy.py:1:5: ANN201 Missing return type annotation for public function `test_trivia`<br>tests/test_dummy.py:1:5: D103 Missing docstring in public function<br>Found 31 errors.<br>[*] 12 fixable with the `--fix` option (6 hidden fixes can be enabled with the `--unsafe-fixes` option).</pre><p>We don’t want some of these errors. We should disable them per file</p><ul><li>Weather __index__.py has a module docstring or not in a web application doesn’t matter.</li><li>There is no need to lint docstring or force type definitions in the tests directory.</li></ul><pre>ignore = [&quot;ISC001&quot;, &quot;D203&quot;, &quot;D213&quot;, &quot;TRY003&quot;, &quot;EM101&quot;]<br><br>[tool.ruff.lint.per-file-ignores]<br>&quot;tests/*&quot; = [&quot;D&quot;, &quot;ANN&quot;]<br>&quot;__init__.py&quot; = [&quot;D&quot;]</pre><pre>djpoe ➜ poetry run ruff check<br>djpoe/djpoe/asgi.py:1:1: D212 [*] Multi-line docstring summary should start at the first line<br>djpoe/djpoe/settings.py:1:1: D212 [*] Multi-line docstring summary should start at the first line<br>djpoe/djpoe/urls.py:1:1: D212 [*] Multi-line docstring summary should start at the first line<br>djpoe/djpoe/urls.py:6:1: D413 [*] Missing blank line after last section (&quot;Examples&quot;)<br>djpoe/djpoe/urls.py:6:1: D411 [*] Missing blank line before section (&quot;Examples&quot;)<br>djpoe/djpoe/urls.py:6:1: D407 [*] Missing dashed underline after section (&quot;Examples&quot;)<br>djpoe/djpoe/urls.py:6:1: D406 [*] Section name should end with a newline (&quot;Examples&quot;)<br>djpoe/djpoe/wsgi.py:1:1: D212 [*] Multi-line docstring summary should start at the first line<br>djpoe/manage.py:8:5: ANN201 Missing return type annotation for public function `main`<br>tests/test_admin_page.py:8:1: PT023 [*] Use `@pytest.mark.django_db()` over `@pytest.mark.django_db`<br>tests/test_admin_page.py:17:1: PT023 [*] Use `@pytest.mark.django_db()` over `@pytest.mark.django_db`<br>tests/test_admin_page.py:30:1: PT023 [*] Use `@pytest.mark.django_db()` over `@pytest.mark.django_db`<br>tests/test_auth_models_user.py:6:1: PT023 [*] Use `@pytest.mark.django_db()` over `@pytest.mark.django_db`<br>Found 13 errors.<br>[*] 12 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).</pre><p>We see 13 errors, and all are fixable.</p><pre>djpoe ➜ poetry run ruff check --fix<br>djpoe/manage.py:8:5: ANN201 Missing return type annotation for public function `main`<br>Found 13 errors (12 fixed, 1 remaining). (1 hidden fix can be enabled with the `--unsafe-fixes` option).<br>djpoe ➜ poetry run ruff check --fix --unsafe-fixes <br>Found 1 error (1 fixed, 0 remaining).<br>djpoe ➜ poetry run ruff check<br>All checks passed!</pre><h3>Pylint</h3><p>Ruff and Pylint are partly overlapping. I like to use pylint, as it’s importing the modules, and we’ll be able to find issues with missing imports.</p><pre>djpoe ➜ poetry add --group dev pylint pylint-django pylint-per-file-ignores<br>Using version ^3.2.4 for pylint<br>Using version ^2.5.5 for pylint-django<br>Using version ^1.3.2 for pylint-per-file-ignores<br><br>Updating dependencies<br>Resolving dependencies... (0.6s)<br><br>Package operations: 9 installs, 0 updates, 0 removals<br>  - ...<br>  - Installing pylint (3.2.4)<br>  - Installing pylint-django (2.5.5)<br>  - Installing pylint-per-file-ignores (1.3.2)<br><br>Writing lock file</pre><p>We need to configure Pylint.</p><pre># pyproject.toml<br>...<br><br>[tool.pylint.&#39;MESSAGES CONTROL&#39;]<br>max-line-length = 120<br>disable = [&quot;fixme&quot;, &quot;too-many-arguments&quot;]<br>load-plugins = [&quot;pylint_per_file_ignores&quot;, &quot;pylint_django&quot;]<br>django-settings-module = &quot;djpoe.settings&quot;<br>per-file-ignores = &quot;&quot;&quot;<br>    /tests/:missing-function-docstring,assignment-from-no-return,not-context-manager,django-not-configured,imported-auth-user,missing-module-docstring<br>    /migrations/:invalid-name,missing-class-docstring,wrong-import-order<br>    models.py:too-many-ancestors<br>    manage.py:import-outside-toplevel<br>&quot;&quot;&quot;</pre><p>We’ve disabled checks that allow us to write # FIXME and use methods with several arguments. We’re also ignoring some checks on models.py module, and tests and migrations directory. The import-outside-toplevel is ignored in manage.py as execute_from_command_line is imported inside the try/except block.</p><pre>djpoe ➜ poetry run pylint tests djpoe/djpoe<br><br>-------------------------------------------------------------------<br>Your code has been rated at 10.00/10</pre><p>Adding a non-existing import would be reported as:</p><pre># tests/test_auth_models_user.py<br>import nonexistingmodule</pre><pre>djpoe ➜ poetry run pylint tests djpoe<br>************* Module tests.test_auth_models_user<br>tests/test_auth_models_user.py:2:0: E0401: Unable to import &#39;nonexistingmodule&#39; (import-error)<br>tests/test_auth_models_user.py:2:0: W0611: Unused import nonexistingmodule (unused-import)<br><br>-------------------------------------------------------------------<br>Your code has been rated at 9.21/10 (previous run: 10.00/10, -0.79)</pre><h3>Mypy</h3><p>Each program I’ve written in the past few years is typed. Typing in programming is crucial as it improves code readability and maintainability and helps catch errors early by ensuring that variables and function signatures adhere to specified types.</p><pre>djpoe ➜ poetry add --group dev mypy django-stubs</pre><h4>Mypy configuration</h4><p>Configuring Mypy for Django under Poetry is not so straightforward. Thedjango-stubs module needs to know the location of the settings module. Without change, it would be djpoe.djpoe.settings, unfortunately, that would bite us later during the development of an app. We need to make it django.settings. The simplest way is to modify the PYTHONPATH environment variable to contain the djpoe directory. Poetry has a poetry-dotenv-plugin that reads from the .env file.</p><pre>djpoe ➜ poetry self add poetry-dotenv-plugin<br>Using version ^0.2.0 for poetry-dotenv-plugin<br>...</pre><pre># .env<br>PYTHONPATH=$(pwd)/djpoe:$PYTHONPATH</pre><p>We also need to add Mypy configuration to pyproject.toml</p><pre># pyproject.toml<br><br>[tool.mypy]<br>plugins = &quot;mypy_django_plugin.main&quot;<br>mypy_path = &quot;./djpoe&quot;<br><br>[tool.django-stubs]<br>django_settings_module = &quot;djpoe.settings&quot;<br>ignore_missing_model_attributes = true</pre><p>We’re connecting the plugin for Django, specifying the path, and configuring the django-stubs.</p><h4>Running Mypy</h4><pre>djpoe ➜ poetry run mypy --install-types --check-untyped-defs . <br>djpoe/djpoe/settings.py:27: error: Need type annotation for &quot;ALLOWED_HOSTS&quot; (hint: &quot;ALLOWED_HOSTS: list[&lt;type&gt;] = ...&quot;)  [var-annotated]<br>tests/test_admin_page.py:27: error: &quot;_MonkeyPatchedWSGIResponse&quot; has no attribute &quot;url&quot;  [attr-defined]<br>Found 2 errors in 2 files (checked 10 source files)<br><br>Installing missing stub packages:<br>[...]djpoe/.venv/bin/python -m pip install types-Pygments types-colorama types-setuptools</pre><p>We directed mypy to install missing types, and it asked if it could do it. You can bypass this question with a -y switch.</p><p>It found untyped ALLOWED_HOSTS in settings.py and an error in tests. Let’s fix the settings.py by replacing line nr 27 with:</p><pre>ALLOWED_HOSTS: list[str] = []</pre><p>We will ignore the error in the tests/test_admin_page.py file. Also, accidentally, in line 27.</p><pre>assert response.url == &quot;/admin/login/?next=/admin/&quot;  # type: ignore[attr-defined]</pre><pre>djpoe ➜ poetry run mypy --install-types --check-untyped-defs .<br>Success: no issues found in 10 source files</pre><h3>PoeThePoet</h3><p>We have some commands to run now. It’s difficult to remember each one, especially if the project is left for a month or so. Let’s help ourselves with this helpful tool. It needs to be installed like Poetry—outside of the project’s virtual environment.</p><pre>djpoe ➜ ~/.venv/bin/pip3 install poethepoet<br>djpoe ➜ ln -fs ~/.venv/bin/poe ~/.local/bin/poe</pre><p>Let’s add Mypy command:</p><pre># pyproject.toml<br><br>[tool.poe.tasks.mypy]<br>cmd = &quot;mypy --install-types --check-untyped-defs .&quot;</pre><p>And call it</p><pre>djpoe ➜ poe mypy<br>Poe =&gt; mypy --install-types --check-untyped-defs .<br>Success: no issues found in 10 source files</pre><p>I also create shortcuts for standard Django calls to manage.py. It might look like this:</p><pre># pyproject.toml<br><br>[tool.poe.tasks.test]<br>help = &quot;Pytest.&quot;<br>cmd = &quot;pytest --showlocals --tb=auto -ra --cov-branch --cov-report=term-missing&quot;<br><br>[tool.poe.tasks.mypy]<br>cmd = &quot;mypy --install-types --check-untyped-defs .&quot;<br><br>[tool.poe.tasks.pylint]<br>cmd = &quot;pylint tests djpoe/djpoe&quot;<br><br>[tool.poe.tasks.ruff]<br>cmd = &quot;ruff check .&quot;<br><br>[tool.poe.tasks.check]<br>sequence = [&quot;mypy&quot;, &quot;pylint&quot;, &quot;ruff&quot;]<br><br>[tool.poe.tasks.format]<br>cmd = &quot;ruff format .&quot;<br><br>[tool.poe.tasks.dev]<br>help = &quot;Run development server.&quot;<br>cmd = &quot;python ./djpoe/manage.py runserver 127.0.0.1:8001&quot;<br><br>[tool.poe.tasks.makemigrations]<br>help = &quot;Generate new migrations.&quot;<br>cmd = &quot;python ./djpoe/manage.py makemigrations&quot;<br><br>[tool.poe.tasks.migrate]<br>help = &quot;Migrate existing migrations.&quot;<br>cmd = &quot;python ./djpoe/manage.py migrate&quot;<br><br>[tool.poe.tasks.manage]<br>cmd = &quot;python ./djpoe/manage.py&quot;</pre><p>All checks run in sequence:</p><pre>djpoe ➜ poe check<br>Poe =&gt; mypy --install-types --check-untyped-defs .<br>Success: no issues found in 10 source files<br>Poe =&gt; pylint tests djpoe/djpoe<br><br>--------------------------------------------------------------------<br>Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)<br><br>Poe =&gt; ruff check .<br>All checks passed!</pre><h3>pre-commit</h3><p>We can use the pre-commit application to ensure nothing is committed without check. It will hook to the git commit command and run linters before the commit happens.</p><pre>djpoe ➜ brew install pre-commit</pre><p>The configuration has to be written in the .pre-commit-config.yaml file. It would be great to use one of the existing hooks. Unfortunately, these would run outside of the virtual environment. We will use the language: system style.</p><pre>fail_fast: true<br>repos:<br>  - repo: local<br>    hooks:<br>      - id: ruff<br>        name: ruff<br>        language: system<br>        files: &quot;^(?:djpoe|tests)/&quot;<br>        exclude: &quot;migrations&quot;<br>        types: [python]<br>        entry: poetry<br>        args: [&quot;run&quot;, &quot;ruff&quot;, &quot;check&quot;]<br><br>      - id: pylint<br>        name: pylint<br>        language: system<br>        files: &quot;^(?:djpoe|tests)/&quot;<br>        exclude: &quot;migrations&quot;<br>        types: [python]<br>        entry: poetry<br>        args: [&quot;run&quot;, &quot;pylint&quot;, &quot;-sn&quot;]</pre><p>Installing the hooks</p><pre>djpoe ➜ pre-commit install <br>pre-commit installed at .git/hooks/pre-commit</pre><p>Committing a yaml file did not trigger any hooks:</p><pre>djpoe ➜ git commit -am &quot;pre-commit script&quot;<br>ruff.................................................(no files to check)Skipped<br>pylint...............................................(no files to check)Skipped<br>[part-2-linting-and-ci 3025957] pre-commit script<br> 1 file changed, 21 insertions(+)<br> create mode 100644 .pre-commit-config.yaml</pre><p>An attempt to commit a “broken” python file would fail:</p><pre>djpoe ➜ git commit -a                     <br>ruff.....................................................................Failed<br>- hook id: ruff<br>- exit code: 1<br><br>djpoe/manage.py:7:8: F401 [*] `notexistingmodule` imported but unused<br>Found 1 error.<br>[*] 1 fixable with the `--fix` option.</pre><p>You can find the <a href="https://medium.com/p/9878c7ed8feb/github.com/zalun/djpoe/tree/part-2-linting-and-ci">current code on GitHub</a>. The next article is about <a href="https://medium.com/@zalun/3-lint-and-test-with-github-actions-efa80197b303">running the linters using GitHub Actions</a>.</p><h3>Errata</h3><p>Added the pylint-django configuration setting:</p><pre>django-settings-module = &quot;djpoe.settings&quot;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9878c7ed8feb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/django-unleashed/linting-django-9878c7ed8feb">Base 2 — Linting Django</a> was originally published in <a href="https://medium.com/django-unleashed">Django Unleashed</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Base 1 — Django with Poetry]]></title>
            <link>https://medium.com/@zalun/django-with-poetry-ea95bd5083f7?source=rss-c4608c2c6f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea95bd5083f7</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[pytest]]></category>
            <dc:creator><![CDATA[Piotr Zalewa]]></dc:creator>
            <pubDate>Sun, 23 Jun 2024 12:34:54 GMT</pubDate>
            <atom:updated>2024-08-06T14:29:38.929Z</atom:updated>
            <content:encoded><![CDATA[<p>I like <a href="https://python-poetry.org/">Poetry</a>. It gives me confidence in maintaining the dependencies. My next side project will be done in <a href="https://www.djangoproject.com/">Django</a>. It’s going to be a bumpy road, as the last time I called django-admin startproject and finished a project, it was still in the version 1.x. Let’s look into it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tTcHq5jb1UTGZyceCRXm5A.jpeg" /><figcaption>Django with Poetry by Dall-E 3</figcaption></figure><h3>The goal</h3><p>We will create a minimal Django project installed with Poetry and running from the manage.py runserver, using the built-in Sqlite database. We will also install <a href="https://docs.pytest.org/">Pytest</a> and configure it to test Django applications.</p><p>You can clone <a href="https://github.com/zalun/djpoe/tree/part-1">the final project</a> from GitHub: <br>https://github.com/zalun/djpoe/tree/part-1</p><h3>Poetry</h3><p>We can install Poetry globally with pip or with a package manager. I prefer to keep all my global python in ~/.venv. (Just run pip3 install poetry if it’s not your style)</p><pre>Projects ➜ ~/.venv/bin/pip3 install poetry<br>Projects ➜ ~/.venv/bin/poetry --version<br>Poetry (version 1.8.3)<br>Projects ➜ ln -fs ~/.venv/bin/poetry ~/.local/bin/poetry</pre><p>Please create a new project, let’s name it djpoe, in the current directory.</p><pre>Projects ➜ poetry new djpoe<br>Created package djpoe in djpoe<br>Projects ➜ cd djpoe<br>djpoe ➜ tree<br>.<br>├── README.md<br>├── djpoe<br>│   └── __init__.py<br>├── pyproject.toml<br>└── tests<br>    └── __init__.py</pre><p>I like Poetry to create the virtual environment in .venv the project’s directory</p><pre>djpoe ➜ poetry config virtualenvs.in-project true --local</pre><h4>A few words about pyproject.toml</h4><p>pyproject.toml is the <a href="https://www.python.org/dev/peps/pep-0518/">unified Python project settings file</a>. It has a similar purpose as setup.py. For now, it contains only the basic information:</p><pre>[tool.poetry]<br>name = &quot;djpoe&quot;<br>version = &quot;0.1.0&quot;<br>description = &quot;&quot;<br>authors = [&quot;Piotr Zalewa &lt;zaloon@gmail.com&gt;&quot;]<br>readme = &quot;README.md&quot;<br><br>[tool.poetry.dependencies]<br>python = &quot;^3.12&quot;<br><br>[build-system]<br>requires = [&quot;poetry-core&quot;]<br>build-backend = &quot;poetry.core.masonry.api&quot;</pre><blockquote>The [build-system] table is used to store build-related data. Initially only one key of the table will be valid and is mandatory for the table: requires. This key must have a value of a list of strings representing <a href="https://peps.python.org/pep-0508/">PEP 508</a> dependencies required to execute the build system (currently that means what dependencies are required to execute a setup.py file).</blockquote><p>[tool.poetry] section defines the package’s details. You can read about it in the <a href="https://python-poetry.org/docs/pyproject/">Poetry documentation</a>.</p><p>[tool.poetry.dependencies] is the section containing the project’s dependencies.</p><h4>Basic Poetry commands</h4><p>poetry install installs all dependencies in the virtual environment.</p><pre>djpoe ➜ poetry install<br>Creating virtualenv djpoe in /Users/piotrzalewa/Projects/TEST/djpoe/.venv<br>Updating dependencies<br>Resolving dependencies... (0.1s)<br><br>Writing lock file<br><br>Installing the current project: djpoe (0.1.0)<br>djpoe ➜ ls .venv<br>bin        lib        pyvenv.cfg</pre><p>poetry run allows us to run commands in the package’s virtual environment.</p><pre>djpoe ➜ poetry run python --version<br>Python 3.12.1<br>djpoe ➜ poetry run which python<br>/Users/piotrzalewa/Projects/djpoe/.venv/bin/python</pre><p>poetry add [package-name] installs a package and adds it to the [tool.poetry.dependencies] section. If you want to install a developer dependency, you need to add the --group dev switch.</p><h4>`poetry.lock` file</h4><p>This file is similar to the requirements.txt with hashes. It is to be sure that the dependencies installed on the server will be the same as those on the local machine. It grows dramatically with each installed package.</p><pre>[metadata]<br>lock-version = &quot;2.0&quot;<br>python-versions = &quot;^3.12&quot;<br>content-hash = &quot;f1352aa895873733ae7b28a33579a57f6e0eb147ae0ace7ffaaaa97485835892&quot;</pre><h3>Installing Pytest</h3><p>As with all of the packages, we will use the add command:</p><pre>djpoe ➜ poetry add --group dev pytest<br>Using version ^8.2.2 for pytest<br><br>Updating dependencies<br>Resolving dependencies... (0.4s)<br><br>Package operations: 4 installs, 0 updates, 0 removals<br><br>  - Installing iniconfig (2.0.0)<br>  - Installing packaging (24.1)<br>  - Installing pluggy (1.5.0)<br>  - Installing pytest (8.2.2)<br><br>Writing lock file</pre><p>We’ve got a new section in pyproject.toml</p><pre>[tool.poetry.group.dev.dependencies]<br>pytest = &quot;^8.2.2&quot;</pre><p>Let’s create a dummy test and run Pytest.</p><pre># tests/test_dummy.py<br>def test_trivia():<br>    assert True</pre><pre>djpoe ➜ poetry run pytest <br>============================= test session starts ==============================<br>platform darwin -- Python 3.12.1, pytest-8.2.2, pluggy-1.5.0<br>rootdir: /Users/piotrzalewa/Projects/djpoe<br>configfile: pyproject.toml<br>collected 1 item                                                               <br><br>tests/test_dummy.py .                                                    [100%]<br><br>============================== 1 passed in 0.01s ===============================</pre><p>You can (and should) change the test to assert False to check with a failing test if Pytest is really working.</p><h3>Installing Django</h3><p>Finally.</p><pre>djpoe ➜ poetry add django<br>Using version ^5.0.6 for django<br><br>Updating dependencies<br>Resolving dependencies... (0.2s)<br><br>Package operations: 3 installs, 0 updates, 0 removals<br><br>  - Installing asgiref (3.8.1)<br>  - Installing sqlparse (0.5.0)<br>  - Installing django (5.0.6)<br><br>Writing lock file</pre><p>I like to make use of the typing library. Django does not provide stubs out of the box. We need to install them by hand</p><pre>djpoe ➜ poetry add django-types django-stubs-ext</pre><p>Our pyproject.toml has extended the dependencies section:</p><pre>[tool.poetry.dependencies]<br>python = &quot;^3.12&quot;<br>django = &quot;^5.0.6&quot;<br>django-stubs-ext = &quot;^5.0.2&quot;<br>django-types = &quot;^0.19.1&quot;</pre><p>Let’s now start a new Django project. We’ll call it djpoe, so we first need to delete the existing djpoe/ directory. Then, we will call django-admin to migrate the database and create the administrator account.</p><pre>djpoe ➜ rm -rf djpoe<br>djpoe ➜ poetry run django-admin startproject djpoe<br>djpoe ➜ tree djpoe <br>djpoe<br>├── djpoe<br>│   ├── __init__.py<br>│   ├── asgi.py<br>│   ├── settings.py<br>│   ├── urls.py<br>│   └── wsgi.py<br>└── manage.py</pre><pre>djpoe ➜ poetry run python  djpoe/manage.py migrate<br>Operations to perform:<br>  Apply all migrations: admin, auth, contenttypes, sessions<br>Running migrations:<br>  Applying contenttypes.0001_initial... OK<br>  Applying auth.0001_initial... OK<br>  Applying admin.0001_initial... OK<br>  Applying admin.0002_logentry_remove_auto_add... OK<br>  Applying admin.0003_logentry_add_action_flag_choices... OK<br>  Applying contenttypes.0002_remove_content_type_name... OK<br>  Applying auth.0002_alter_permission_name_max_length... OK<br>  Applying auth.0003_alter_user_email_max_length... OK<br>  Applying auth.0004_alter_user_username_opts... OK<br>  Applying auth.0005_alter_user_last_login_null... OK<br>  Applying auth.0006_require_contenttypes_0002... OK<br>  Applying auth.0007_alter_validators_add_error_messages... OK<br>  Applying auth.0008_alter_user_username_max_length... OK<br>  Applying auth.0009_alter_user_last_name_max_length... OK<br>  Applying auth.0010_alter_group_name_max_length... OK<br>  Applying auth.0011_update_proxy_permissions... OK<br>  Applying auth.0012_alter_user_first_name_max_length... OK<br>  Applying sessions.0001_initial... OK</pre><pre>djpoe ➜ poetry run python djpoe/manage.py createsuperuser <br>Username (leave blank to use &#39;piotrzalewa&#39;): zalun<br>Email address: x@xx.xx<br>Password: <br>Password (again): <br>Superuser created successfully.</pre><p>Let’s see if the development server is running</p><pre>djpoe ➜ poetry run python djpoe/manage.py runserver   <br>Watching for file changes with StatReloader<br>Performing system checks...<br><br>System check identified no issues (0 silenced).<br>June 22, 2024 - 18:17:39<br>Django version 5.0.6, using settings &#39;djpoe.settings&#39;<br>Starting development server at http://127.0.0.1:8000/<br>Quit the server with CONTROL-C.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zEACGEdTdRLTiuCHEkyGYQ.png" /><figcaption>First view of Django site on http://127.0.0.1:8000</figcaption></figure><p>You can navigate to <a href="http://127.0.0.1:8000/admin">http://127.0.0.1:8000/admin</a> login with the provided credentials and configure it further.</p><h3>Testing Django</h3><p>Django provides extensions for pytest. It includes fixtures allowing to render pages and an easy way to mock directory access.</p><pre>djpoe ➜ poetry add --group dev pytest-django<br>Using version ^4.8.0 for pytest-django</pre><p>We need to configure Pytest and provide the path to the Django settings. We use djpoe.djpoe, because the tests directory is on the same level as the Django project.</p><pre># pytest.ini<br>[pytest]<br>DJANGO_SETTINGS_MODULE = djpoe.djpoe.settings<br>FAIL_INVALID_TEMPLATE_VARS = True<br>django_debug_mode = false<br>django_find_project = false</pre><h4>Testing models</h4><p>Our site isn’t doing a thing. The first easy thing to check is creating a user. We can do it using the User class:</p><pre># tests/test_auth_models_user.py<br>import pytest<br>from django.contrib.auth.models import User<br><br><br>@pytest.mark.django_db<br>def test_user_create():<br>    User.objects.create_user(username=&quot;john&quot;, password=&quot;pass1234&quot;)<br><br>    assert User.objects.count() == 1<br>    user = User.objects.first()<br>    assert user.username == &quot;john&quot;<br>    assert user.is_active is True<br>    assert user.is_staff is False</pre><pre>djpoe poetry run pytest<br>============================= test session starts ==============================<br>platform darwin -- Python 3.12.1, pytest-8.2.2, pluggy-1.5.0<br>django: version: 5.0.6, settings: djpoe.djpoe.settings (from ini)<br>rootdir: /Users/piotrzalewa/Projects/TEST/djpoe<br>configfile: pytest.ini<br>plugins: django-4.8.0<br>collected 2 items                                                              <br><br>tests/test_auth_models_user.py .                                         [ 50%]<br>tests/test_dummy.py .                                                    [100%]<br><br>============================== 2 passed in 0.93s ===============================</pre><p>First, we import pytest module and the User class. Then, we decorate the test with a <a href="https://pytest-django.readthedocs.io/en/latest/database.html">marker providing the Django database</a> @pytest.mark.django_db. This decorator creates a database in memory and deletes it for each test. We can create the next test, create a user there, and the User.objects.count() will return 1 as in the first test.</p><p>A user account is created with User.objects.create_user() method. It is then tested.</p><p>This isn’t the best option. The User class might not be used for the user account in our app. pytest-django provides us with a fixture called django_user_model. It returns the model defined in the djpoe/djpoe/settings.py with AUTH_USER_MODEL.</p><p>Navigate to the <a href="https://pytest-django.readthedocs.io/en/latest/helpers.html#fixtures">pytest-django documentation</a> to see other fixtures.</p><pre># tests/test_auth_models_user.py<br>import pytest<br><br><br>@pytest.mark.django_db<br>def test_user_create(django_user_model):<br>    django_user_model.objects.create_user(username=&quot;john&quot;, password=&quot;pass1234&quot;)<br><br>    assert django_user_model.objects.count() == 1<br>    user = django_user_model.objects.first()<br>    assert user.username == &quot;john&quot;<br>    assert user.is_active is True<br>    assert user.is_staff is False</pre><h4>Using `client` to test rendered pages</h4><p>We can also test the rendering of the admin login page.</p><pre># tests/test_admin_page.py<br>import pytest<br>from django.test import Client<br>from django.urls import reverse<br><br><br>@pytest.mark.django_db<br>def test_render_admin_login(client: Client):<br>    url = reverse(&quot;admin:login&quot;)<br><br>    response = client.get(url)<br><br>    assert response.status_code == 200</pre><p>We use here the client fixture. It is an object pretending to be a browser. Unfortunately, Pytest fails with an error:</p><pre>FAILED tests/test_admin_page.py::test_render_admin_login -<br>ModuleNotFoundError: No module named &#39;djpoe.urls&#39;</pre><p>This happens because using djpoe.djpoe.settings is more a workaround than a solution. Pytest doesn’t fully understand the project structure and fails on some of the imports. We need to reconfigure Python paths</p><pre># pytest.ini<br>[pytest]<br>DJANGO_SETTINGS_MODULE = djpoe.settings<br>django_debug_mode = false<br>pythonpath = . djpoe</pre><p>We’ve added . and djpoe to the pythonpath for Pytest only.</p><pre>djpoe ➜ poetry run pytest                         <br>============================= test session starts ==============================<br>...<br><br>tests/test_admin_page.py .                                               [ 33%]<br>tests/test_auth_models_user.py .                                         [ 66%]<br>tests/test_dummy.py .                                                    [100%]<br><br>============================== 3 passed in 1.41s ===============================</pre><p>Now we can check if we can use the admin page as a regular user.</p><p>Let’s add these two methods to the tests/test_admin_page.py</p><pre><br>@pytest.mark.django_db<br>def test_admin_redirect_if_not_superuser(client: Client, django_user_model: User):<br>    user = django_user_model.objects.create_user(username=&quot;john&quot;, password=&quot;pass1234&quot;)<br>    client.force_login(user)<br>    url = reverse(&quot;admin:index&quot;)<br><br>    response = client.get(url)<br><br>    assert response.status_code == 302<br>    assert response.content == b&quot;&quot;<br>    assert response.url == &quot;/admin/login/?next=/admin/&quot;<br><br><br>@pytest.mark.django_db<br>def test_admin_success_for_superuser(client: Client, django_user_model: User):<br>    user = django_user_model.objects.create_user(<br>        username=&quot;john&quot;, password=&quot;pass1234&quot;, is_superuser=True, is_staff=True<br>    )<br>    client.force_login(user)<br>    url = reverse(&quot;admin:index&quot;)<br><br>    response = client.get(url)<br><br>    assert response.status_code == 200<br>    assert b&quot;Django site admin&quot; in response.content</pre><p>In the first test, we log in as the standard user. Attempting to load the admin index page ends with a redirect to the login page.</p><p>In the second test, we create the superuser and check if the admin index page is loaded properly.</p><p>We achieved the goal. We can test the code. The <a href="https://medium.com/@zalun/linting-django-9878c7ed8feb">next part</a> is about linting.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea95bd5083f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>