美文网首页
Try DJANGO Tutorial -- Part II

Try DJANGO Tutorial -- Part II

作者: c8ac4dad76db | 来源:发表于2018-09-08 22:53 被阅读11次

    11. Change a Model

    class Product(models.Model):
        title = models.CharField(max_length=120)
        description = models.TextField(blank=True, null=True)
        price = models.DecimalField(decimal_places=2, max_digits=10000)
        summary = models.TextField(null=False, blank=False)
        featured = models.BooleanField(default=False)
    

    12. Default Homepage to Custom Homepage

    python manage.py startapp pages
    

    pages/views.py:

    from django.http import HttpResponse
    
    
    def home_view(*args, **kwargs):
        return HttpResponse("<h1>Hello world</h1>")
    

    trydjango/urls.py:

    from django.contrib import admin
    from django.urls import path
    
    from pages.views import home_view
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', home_view, name="home"),
    ]
    

    13. URL Routing and Requests

    pages/views.py:

    from django.http import HttpResponse
    
    
    def home_view(request, *args, **kwargs):
        print(args, kwargs)
        print(request.user)
        return HttpResponse("<h1>Hello world</h1>")
    
    
    def contact_view(request, *args, **kwargs):
        return HttpResponse("<h1>Contact page</h1>")
    
    
    def about_view(request, *args, **kwargs):
        return HttpResponse("<h1>About page</h1>")
    
    
    def social_view(request, *args, **kwargs):
        return HttpResponse("<h1>Social page</h1>")
    

    trydjango/urls.py:

    from django.contrib import admin
    from django.urls import path
    
    from pages.views import home_view, contact_view, about_view, social_view
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', home_view, name="home"),
        path('contact', contact_view),
        path('about', about_view),
        path('social', social_view),
    ]
    

    14. Django Templates

    trydjango/settings.py:

    TEMPLATES = [
        {
            # ...
            'DIRS': [os.path.join(BASE_DIR, "templates")],
            # ...
        },
    ]
    

    pages/views.py:

    from django.http import HttpResponse
    from django.shortcuts import render
    
    
    def home_view(request, *args, **kwargs):
        print(args, kwargs)
        print(request.user)
        return render(request, 'home.html')
    
    
    def contact_view(request, *args, **kwargs):
        return render(request, 'contact.html')
    
    
    def about_view(request, *args, **kwargs):
        return render(request, 'about.html')
    

    templates/home.html

    <h1>Home page</h>
    

    15. Django Templating Engine Basics

    templates/base.html:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Coding for Entrepreneurs is doing Try Django</title>
        </head>
        <body>
            <h1>Try Django</h1>
            {% block content %}
            {% endblock %}
        </body>
    </html>
    

    templates/home.html:

    {% extends 'base.html' %}
    
    {% block content %}
        <h2>Home page</h>
        <p>This is a template</p>
    {% endblock %}
    

    16. Include Template Tag

    templates/navbar.html:

    <nav>
        <ul>
            <li>Brand</li>
            <li>Contact</li>
            <li>About</li>
        </ul>
    </nav>
    

    templates/base.html

    <!-- ... -->
        <body>
            {% include 'navbar.html' %}
            <h1>Try Django</h1>
            {% block content %}
            {% endblock %}
        </body>
    <!-- ... -->
    

    17. Rendering Context in a Template

    pages/views.py:

    def about_view(request, *args, **kwargs):
        my_context = {
            "my_text": "This is about us",
            "my_number": 123,
            "my_list": ['abc', 'def', 'ghijk']
        }
        return render(request, 'about.html', my_context)
    

    templates/about.html:

    {% extends 'base.html' %}
    
    {% block content %}
    
        <h1>About us</h1>
        <p>This is about page.</p>
    
        <p>{{ my_text }},{{ my_number }},{{ my_list }}</p>
    
    {% endblock %}
    

    18. For Loop in a Template

    templates/about.html:

        <ul>
            {% for item in my_list %}
                <li>{{ forloop.counter }} - {{ item }}</li>
            {% endfor %}
        </ul>
    

    19. Using Conditions in a Template

    templates/about.html:

        <ul>
            {% for item in my_list %}
                {% if item == 123 %}
                    <li>{{ forloop.counter }} - {{ item | add:22 }}</li>
                {% elif item == "Abc" %}
                    <li>This is not the network</li>
                {% else %}
                    <li>{{ forloop.counter }} - {{ item }}</li>
                {% endif %}
            {% endfor %}
        </ul>
    

    20. Template Tags and Filters

    相关文章

      网友评论

          本文标题:Try DJANGO Tutorial -- Part II

          本文链接:https://www.haomeiwen.com/subject/sbbkgftx.html