Yêu cầu python nhấp vào nút gửi

Trong loạt bài hướng dẫn gồm bốn phần này, bạn đang xây dựng một mạng xã hội với Django mà bạn có thể giới thiệu trong danh mục đầu tư của mình. Dự án này đang củng cố và thể hiện sự hiểu biết của bạn về mối quan hệ giữa các mô hình Django và chỉ cho bạn cách sử dụng các biểu mẫu để người dùng có thể tương tác với ứng dụng của bạn và với nhau. Bạn cũng đang làm cho trang web của mình trông đẹp mắt bằng cách sử dụng khung CSS Bulma

Trong phần trước của loạt bài hướng dẫn này, bạn đã xây dựng các mẫu và dạng xem để hiển thị danh sách tất cả các hồ sơ cũng như các trang hồ sơ cá nhân. Bạn cũng đã học cách áp dụng các quy tắc kiểu do khung CSS Bulma xác định để làm cho các trang của bạn trông đẹp mắt

Trong phần thứ ba của loạt bài hướng dẫn này, bạn sẽ học cách

  • Tạo giao diện front-end để cho phép người dùng theo dõi và hủy theo dõi hồ sơ
  • Gửi và xử lý yêu cầu POST ở Django bằng các nút
  • Thiết lập mô hình cho nội dung dựa trên văn bản của bạn
  • Xây dựng các mẫu theo kiểu để hiển thị nội dung trên giao diện người dùng
  • Sử dụng các mối quan hệ mô hình phức tạp trong mã mẫu

Khi kết thúc phần này, mạng xã hội của bạn sẽ có tất cả các trang mà bạn mong đợi và bạn sẽ có thể tạo các tin nhắn ở mặt sau và hiển thị chúng ở mặt trước. Người dùng của bạn sẽ có thể khám phá và theo dõi những người dùng khác cũng như đọc nội dung của các hồ sơ mà họ theo dõi. Họ cũng có thể nhấp vào nút gửi yêu cầu HTTP POST do Django xử lý để hủy theo dõi hồ sơ nếu họ muốn ngừng đọc nội dung của một người dùng nhất định

Bạn có thể tải xuống mã mà bạn sẽ cần để bắt đầu phần thứ ba của dự án này bằng cách nhấp vào liên kết bên dưới và chuyển đến thư mục



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
6

Nhận mã nguồn. Nhấp vào đây để lấy mã nguồn mà bạn sẽ sử dụng để xây dựng và xử lý các yêu cầu POST với Django

Thử nghiệm

Trong loạt bài hướng dẫn gồm bốn phần này, bạn đang xây dựng một mạng xã hội nhỏ cho phép người dùng đăng các tin nhắn văn bản ngắn. Người dùng ứng dụng của bạn cũng có thể theo dõi hồ sơ người dùng khác để xem bài đăng của những người dùng đó hoặc hủy theo dõi họ để ngừng xem bài đăng dựa trên văn bản của họ

Bạn cũng đang học cách sử dụng khung CSS Bulma để cung cấp cho ứng dụng của bạn giao diện thân thiện với người dùng và biến nó thành một dự án danh mục đầu tư mà bạn có thể tự hào khoe

Trong phần thứ ba của loạt bài hướng dẫn này, bạn sẽ tiếp tục làm việc với các mẫu theo phong cách Bulma và bạn sẽ tạo mẫu cho nội dung tin nhắn dựa trên văn bản của mình. Bạn cũng sẽ thiết lập và xử lý các lượt gửi yêu cầu HTTP POST trên giao diện người dùng của ứng dụng Django, điều này sẽ cho phép người dùng theo dõi hoặc hủy theo dõi lẫn nhau bằng cách nhấp vào nút

Khi kết thúc hướng dẫn này, mỗi người dùng sẽ có thể truy cập trang bảng điều khiển mới để truy cập nguồn cấp dữ liệu cá nhân dựa trên hồ sơ họ theo dõi. Họ cũng có thể theo dõi và hủy theo dõi những người dùng khác. Đó là một bước nhảy vọt cho mạng xã hội Django của bạn

Loại bỏ các quảng cáo

Tổng quan dự án

Trong phần này, bạn sẽ có cái nhìn tổng quan về những chủ đề mà bạn sẽ trình bày trong phần thứ ba của loạt bài hướng dẫn. Bạn cũng sẽ có cơ hội xem lại toàn bộ các bước triển khai dự án, trong trường hợp bạn cần quay lại bước trước đó từ phần trước của loạt bài hoặc nếu bạn muốn xem những gì còn ở phía trước

Tại thời điểm này, bạn đã hoàn thành phần một và hai của loạt bài hướng dẫn này. Nếu bạn đã làm như vậy, thì bây giờ bạn đã sẵn sàng để tiếp tục với các bước tiếp theo của loạt bài hướng dẫn này. Các bước này tập trung vào logic mã để theo dõi và hủy theo dõi các cấu hình, cũng như cách thiết lập các bộ lưu trữ

Bước 7Theo dõi và hủy theo dõi các cấu hình khácBước 8Tạo logic back-end cho DweetsBước 9Hiển thị Dweets trên giao diện người dùng

Sau khi hoàn thành tất cả các bước trong phần thứ ba của loạt bài hướng dẫn, bạn có thể tiếp tục với phần bốn

Để có được ý tưởng cấp cao về cách bạn đang làm việc qua cả bốn phần của loạt bài hướng dẫn xây dựng mạng xã hội Django của mình, bạn có thể mở rộng phần có thể thu gọn bên dưới

Các bước triển khai dự án đầy đủHiển thị/Ẩn

Bạn đang triển khai dự án theo một số bước trải rộng qua nhiều hướng dẫn riêng biệt trong loạt bài này. Có rất nhiều thứ để đề cập và bạn sẽ đi vào chi tiết trong quá trình thực hiện

✅ Phần 1. Mô hình và mối quan hệ

  • Bước 1. Thiết lập dự án cơ sở
  • Bước 2. Mở rộng Mô hình Người dùng Django
  • Bước 3. Thực hiện một Post-Save Hook

✅ Phần 2. Mẫu và kiểu dáng Front-End

  • Bước 4. Tạo một mẫu cơ sở với Bulma
  • Bước 5. Liệt kê tất cả hồ sơ người dùng
  • Bước 6. Truy cập trang hồ sơ cá nhân

📍 Phần 3. Theo dõi và Dweets

  • Bước 7. Theo dõi và hủy theo dõi các hồ sơ khác
  • Bước 8. Tạo logic Back-End cho Dweets
  • Bước 9. Hiển thị Dweets trên giao diện người dùng

⏭ Phần 4. Biểu mẫu và Đệ trình

  • Bước 10. Gửi Dweets thông qua biểu mẫu Django
  • Bước 11. Ngăn chặn đệ trình trùng lặp và xử lý lỗi
  • Bước 12. Cải thiện trải nghiệm người dùng Front-End

Mỗi bước này sẽ cung cấp liên kết đến bất kỳ tài nguyên cần thiết nào. Bằng cách tiếp cận từng bước một, bạn sẽ có cơ hội tạm dừng và quay lại sau trong trường hợp bạn muốn nghỉ giải lao

Với cấu trúc cấp cao của loạt bài hướng dẫn này, bạn đã biết rõ mình đang ở đâu và các bước triển khai nào bạn sẽ xử lý trong phần cuối

Trước khi bắt đầu với bước tiếp theo, hãy xem nhanh các điều kiện tiên quyết để lướt qua mọi liên kết đến các tài nguyên khác có thể hữu ích trong quá trình thực hiện

điều kiện tiên quyết

Để thực hiện thành công phần này của dự án, bạn cần phải hoàn thành phần đầu tiên về mô hình và mối quan hệ và phần thứ hai về mẫu và kiểu dáng, đồng thời bạn nên xác nhận rằng dự án của mình đang hoạt động như được mô tả ở đó. Sẽ là tốt nhất nếu bạn cũng cảm thấy thoải mái với các khái niệm sau

  • Sử dụng lập trình hướng đối tượng trong Python
  • Thiết lập một dự án Django cơ bản
  • Quản lý định tuyến và chuyển hướng, xem chức năng, mẫu, mô hình và di chuyển trong Django
  • Sử dụng và tùy chỉnh giao diện quản trị Django
  • Đọc và viết HTML với các thuộc tính lớp

Hãy nhớ đảm bảo rằng bạn đã hoàn thành hai phần đầu tiên của loạt bài hướng dẫn này. Phần thứ ba này sẽ bắt đầu ngay tại nơi bạn đã dừng lại ở phần cuối của phần thứ hai

Ghi chú. Bạn sẽ không thể theo dõi phần này của loạt bài hướng dẫn nếu bạn chưa có sẵn dự án đang hoạt động từ các phần trước

Bạn cũng có thể tải xuống mã mà bạn sẽ cần để bắt đầu phần thứ ba của dự án này bằng cách nhấp vào liên kết bên dưới và chuyển đến thư mục



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
6

Nhận mã nguồn. Nhấp vào đây để lấy mã nguồn mà bạn sẽ sử dụng để xây dựng và xử lý các yêu cầu POST với Django

Để biết các yêu cầu bổ sung và các liên kết khác, hãy xem các điều kiện tiên quyết được đề cập trong phần đầu tiên của loạt bài hướng dẫn này về cách xây dựng một mạng xã hội cơ bản trong Django

Bước 7. Theo dõi và hủy theo dõi các hồ sơ khác

Tại thời điểm này, bạn có thể truy cập tất cả hồ sơ trên trang danh sách hồ sơ và bạn cũng có thể truy cập trang hồ sơ của người dùng. Ngoài ra, bạn có thể điều hướng giữa các chế độ xem này ở giao diện người dùng bằng các liên kết động. Khi kết thúc bước này, bạn sẽ có thể theo dõi và hủy theo dõi hồ sơ thông qua các nút trên trang hồ sơ

Khi bạn đã thêm khả năng theo dõi và hủy theo dõi một hồ sơ, bạn sẽ tạo lại chức năng phụ trợ mà giao diện quản trị Django của bạn cung cấp cho bạn khi bạn thiết lập nó trong phần đầu tiên của loạt bài hướng dẫn này

Loại bỏ các quảng cáo

Thêm các nút vào hồ sơ của bạn

Thêm mã HTML với kiểu nút Bulma để tạo hai nút để tương tác với hồ sơ

<div class="buttons has-addons">
    <button class="button is-success">Followbutton>
    <button class="button is-danger">Unfollowbutton>
div>

Một vị trí tốt cho các nút theo dõi và hủy theo dõi người dùng có thể nằm ngay bên dưới tên hồ sơ của họ. Tìm hiểu vị trí của mã mẫu của riêng bạn, sau đó thêm HTML được hiển thị ở trên

Nếu bạn đang sử dụng HTML từ mẫu



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
8, thì bạn có thể thêm nó như được hiển thị trong phần có thể thu gọn bên dưới

dwitter/mẫu/dwitter/hồ sơ. htmlHiện/Ẩn



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}

Sau khi thêm đoạn mã HTML này vào



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
8, bạn sẽ thấy hai nút hiển thị dưới tên hồ sơ của người dùng

Ví dụ: nếu bạn truy cập

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
0, bạn có thể đảm bảo rằng máy chủ phát triển của Django đang chạy và bạn có thể truy cập trang hồ sơ trên máy chủ cục bộ của mình. URL này hiển thị cho bạn trang hồ sơ của hồ sơ người dùng có ID
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
1 và bao gồm các nút mới

Yêu cầu python nhấp vào nút gửi

Các nút nhìn tốt. Tuy nhiên, có vẻ như bạn có thể nhấp để theo dõi một hồ sơ ngay cả khi bạn đã theo dõi hồ sơ đó

Để tạo sự khác biệt ở đó, bạn có thể tô xám nút không liên quan để hành động liên quan sẽ rõ ràng hơn đối với người dùng của bạn. Bulma sẽ hiển thị các nút của bạn chuyển sang màu xám nếu bạn thêm một lớp HTML có tên là

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
2. Bạn có thể áp dụng lớp này tùy thuộc vào việc người dùng đã đăng nhập có theo dõi hồ sơ mà họ đang xem hay không

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>

Bạn đang làm việc với biến

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
3 mà Django gửi với mọi yêu cầu GET và POST. Lưu ý rằng bạn thậm chí không cần chuyển biến này một cách rõ ràng vào từ điển ngữ cảnh của mình trong
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
4. Biến
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
3 chứa thông tin về người dùng hiện đang đăng nhập

  • Dòng 2. Trong câu lệnh có điều kiện này, bạn đang kiểm tra xem hồ sơ mà bạn hiện đang xem có nằm trong bộ truy vấn của tất cả các hồ sơ mà người dùng đã đăng nhập theo dõi hay không

  • Dòng 3 đến 4. Nếu người dùng đã theo dõi hồ sơ mà họ đang xem thì nút Theo dõi sẽ chuyển sang màu xám bằng cách thêm lớp

     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    2. Nút Hủy theo dõi sẽ có màu và có thể nhấp được

  • Dòng 6 đến 7. Nếu người dùng không theo dõi hồ sơ mà họ đang xem thì nút Theo dõi sẽ hiển thị có thể nhấp được và nút Bỏ theo dõi sẽ chuyển sang màu xám

  • dòng 8. Trong dòng này, bạn đang kết thúc câu lệnh điều kiện

Bằng cách thêm một chút logic mã thông qua các thẻ mẫu và kiểu dáng của Django, bạn đã làm rõ hơn cho người dùng rằng họ chỉ có thể theo dõi các hồ sơ mà họ hiện không theo dõi và họ chỉ có thể bỏ theo dõi những hồ sơ mà họ đã theo dõi

Điều hướng đến một URL trang hồ sơ chẳng hạn như

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
0 hoặc, nếu bạn vẫn ở đó, thì chỉ cần tải lại trang để xem các thay đổi

Yêu cầu python nhấp vào nút gửi

Tuy nhiên, mặc dù thông tin hiện được hiển thị theo cách thân thiện hơn với người dùng, nhưng việc nhấp vào nút có thể nhấp không thực sự làm được gì. Đối với điều này, bạn sẽ cần tạo một biểu mẫu HTML và xử lý việc gửi biểu mẫu trong logic mã của bạn

Xử lý các yêu cầu POST trong Django Code Logic

Bước tiếp theo của bạn là triển khai logic về điều sẽ xảy ra khi ai đó nhấp vào một trong các nút này. Đối với điều này, bạn sẽ cần một biểu mẫu HTML để bạn có thể gửi dữ liệu và xử lý dữ liệu đó trong chế độ xem của mình



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
5

Bạn đã áp dụng một vài thay đổi cần thiết cho mẫu của mình bằng cách cập nhật mã này

  • Dòng 1 và 16. Bạn đã bọc hai nút của mình trong phần tử HTML

     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    8 và thêm thuộc tính HTML
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    9 với giá trị
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    50 để làm rõ rằng bạn sẽ gửi dữ liệu bằng biểu mẫu này

  • Dòng 2. Bạn đã thêm mã thông báo CSRF thông qua thẻ mẫu tiện lợi mà Django cung cấp. Bạn cần thêm phần này vì lý do bảo mật nếu bạn muốn cho phép người dùng gửi biểu mẫu trong ứng dụng Django của mình

  • Dòng 6 và 10. Bạn đã thêm hai thuộc tính HTML vào cả hai phần tử

    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    51

    1. 
      
      {% extends 'base.html' %}
      
      {% block content %}
      
      <div class="column">
      
          <div class="block">
          <h1 class="title is-1">
              {{profile.user.username|upper}}'s Dweets
          h1>
          div>
          <div class="buttons has-addons">
              <button class="button is-success">Followbutton>
              <button class="button is-danger">Unfollowbutton>
          div>
      div>
      
      <div class="column is-one-third">
      
          <div class="block">
              <a href="{% url 'dwitter:profile_list' %}">
                  <button class="button is-dark is-outlined is-fullwidth">
                      All Profiles
                  button>
              a>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} follows:
              h3>
              <div class="content">
                  <ul>
                  {% for following in profile.follows.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' following.id %}">
                              {{ following }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} is followed by:
              h3>
              <div class="content">
                  <ul>
                  {% for follower in profile.followed_by.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' follower.id %}">
                              {{ follower }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
      div>
      
      {% endblock content %}
      
      52 xác định khóa nào bạn sẽ sử dụng để truy cập giá trị trong chức năng xem của mình. Bạn đặt phím này thành
      
      
      {% extends 'base.html' %}
      
      {% block content %}
      
      <div class="column">
      
          <div class="block">
          <h1 class="title is-1">
              {{profile.user.username|upper}}'s Dweets
          h1>
          div>
          <div class="buttons has-addons">
              <button class="button is-success">Followbutton>
              <button class="button is-danger">Unfollowbutton>
          div>
      div>
      
      <div class="column is-one-third">
      
          <div class="block">
              <a href="{% url 'dwitter:profile_list' %}">
                  <button class="button is-dark is-outlined is-fullwidth">
                      All Profiles
                  button>
              a>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} follows:
              h3>
              <div class="content">
                  <ul>
                  {% for following in profile.follows.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' following.id %}">
                              {{ following }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} is followed by:
              h3>
              <div class="content">
                  <ul>
                  {% for follower in profile.followed_by.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' follower.id %}">
                              {{ follower }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
      div>
      
      {% endblock content %}
      
      53 cho cả hai nút

    2. 
      
      {% extends 'base.html' %}
      
      {% block content %}
      
      <div class="column">
      
          <div class="block">
          <h1 class="title is-1">
              {{profile.user.username|upper}}'s Dweets
          h1>
          div>
          <div class="buttons has-addons">
              <button class="button is-success">Followbutton>
              <button class="button is-danger">Unfollowbutton>
          div>
      div>
      
      <div class="column is-one-third">
      
          <div class="block">
              <a href="{% url 'dwitter:profile_list' %}">
                  <button class="button is-dark is-outlined is-fullwidth">
                      All Profiles
                  button>
              a>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} follows:
              h3>
              <div class="content">
                  <ul>
                  {% for following in profile.follows.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' following.id %}">
                              {{ following }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} is followed by:
              h3>
              <div class="content">
                  <ul>
                  {% for follower in profile.followed_by.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' follower.id %}">
                              {{ follower }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
      div>
      
      {% endblock content %}
      
      54 xác định giá trị mà biểu mẫu sẽ gửi đến chức năng xem của bạn dưới tên khóa được xác định trong
      
      
      {% extends 'base.html' %}
      
      {% block content %}
      
      <div class="column">
      
          <div class="block">
          <h1 class="title is-1">
              {{profile.user.username|upper}}'s Dweets
          h1>
          div>
          <div class="buttons has-addons">
              <button class="button is-success">Followbutton>
              <button class="button is-danger">Unfollowbutton>
          div>
      div>
      
      <div class="column is-one-third">
      
          <div class="block">
              <a href="{% url 'dwitter:profile_list' %}">
                  <button class="button is-dark is-outlined is-fullwidth">
                      All Profiles
                  button>
              a>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} follows:
              h3>
              <div class="content">
                  <ul>
                  {% for following in profile.follows.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' following.id %}">
                              {{ following }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} is followed by:
              h3>
              <div class="content">
                  <ul>
                  {% for follower in profile.followed_by.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' follower.id %}">
                              {{ follower }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
      div>
      
      {% endblock content %}
      
      52 khi bạn nhấn một trong các nút. Bạn đặt giá trị của nút Theo dõi thành
      
      
      {% extends 'base.html' %}
      
      {% block content %}
      
      <div class="column">
      
          <div class="block">
          <h1 class="title is-1">
              {{profile.user.username|upper}}'s Dweets
          h1>
          div>
          <div class="buttons has-addons">
              <button class="button is-success">Followbutton>
              <button class="button is-danger">Unfollowbutton>
          div>
      div>
      
      <div class="column is-one-third">
      
          <div class="block">
              <a href="{% url 'dwitter:profile_list' %}">
                  <button class="button is-dark is-outlined is-fullwidth">
                      All Profiles
                  button>
              a>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} follows:
              h3>
              <div class="content">
                  <ul>
                  {% for following in profile.follows.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' following.id %}">
                              {{ following }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} is followed by:
              h3>
              <div class="content">
                  <ul>
                  {% for follower in profile.followed_by.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' follower.id %}">
                              {{ follower }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
      div>
      
      {% endblock content %}
      
      53 và giá trị của nút Bỏ theo dõi thành
      
      
      {% extends 'base.html' %}
      
      {% block content %}
      
      <div class="column">
      
          <div class="block">
          <h1 class="title is-1">
              {{profile.user.username|upper}}'s Dweets
          h1>
          div>
          <div class="buttons has-addons">
              <button class="button is-success">Followbutton>
              <button class="button is-danger">Unfollowbutton>
          div>
      div>
      
      <div class="column is-one-third">
      
          <div class="block">
              <a href="{% url 'dwitter:profile_list' %}">
                  <button class="button is-dark is-outlined is-fullwidth">
                      All Profiles
                  button>
              a>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} follows:
              h3>
              <div class="content">
                  <ul>
                  {% for following in profile.follows.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' following.id %}">
                              {{ following }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
          <div class="block">
              <h3 class="title is-4">
                  {{profile.user.username}} is followed by:
              h3>
              <div class="content">
                  <ul>
                  {% for follower in profile.followed_by.all %}
                      <li>
                          <a href="{% url 'dwitter:profile' follower.id %}">
                              {{ follower }}
                          a>
                      li>
                  {% endfor %}
                  ul>
              div>
          div>
      
      div>
      
      {% endblock content %}
      
      57

Bạn sẽ không thấy bất kỳ thay đổi nào ở mặt trước của ứng dụng web của mình sau khi thêm mã này, nhưng những thay đổi này là cần thiết để Django có thể chuyển tiếp dữ liệu do người dùng gửi tới logic xem của bạn. Bạn cũng cần các giá trị thuộc tính được xác định trong dòng 6 và 10 để phân biệt nút nào người dùng đã nhấn ở mặt sau

Với tất cả logic mặt trước được thiết lập trong



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
8, bạn có thể chuyển sang
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
4, nơi bạn có thể nhận và xử lý dữ liệu do người dùng gửi

<div class="buttons has-addons">
    <button class="button is-success">Followbutton>
    <button class="button is-danger">Unfollowbutton>
div>
8

Do cách bạn thiết lập biểu mẫu trong



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
8, biểu mẫu sẽ chuyển hướng trở lại cùng một trang, điều đó có nghĩa là Django sẽ nhận yêu cầu với
<div class="buttons has-addons">
    <button class="button is-success">Followbutton>
    <button class="button is-danger">Unfollowbutton>
div>
81. Nó hoạt động theo cách này do cách bạn thiết lập cấu hình URL của mình. Tuy nhiên, lần này, Django gửi một yêu cầu HTTP POST chứa dữ liệu do người dùng gửi và bạn có thể giải quyết vấn đề này trong bản cập nhật mã của mình

  • dòng 7. Bạn giới thiệu một kiểm tra có điều kiện để xem liệu yêu cầu đến chức năng xem Django của bạn có phải là yêu cầu HTTP POST hay không. Điều này sẽ chỉ xảy ra nếu ai đó gửi biểu mẫu vào ngày

    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    8 bằng cách nhấp vào nút Theo dõi hoặc Bỏ theo dõi

  • dòng 8. Bạn sử dụng thuộc tính

     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    3 từ đối tượng
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
    
    84 của Django, dùng để chỉ người dùng hiện đang đăng nhập, để truy cập đối tượng
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
    
    85 của người dùng đó và gán nó cho
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
    
    86. Sau này, bạn sẽ cần quyền truy cập vào hồ sơ của người dùng này để thay đổi người mà họ đang theo dõi

  • Dòng 9. Bạn lấy dữ liệu do người dùng gửi từ từ điển

    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
    
    87 và lưu trữ nó trong
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
    
    88. Django đặt dữ liệu vào
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
    
    87 khi người dùng gửi biểu mẫu có thuộc tính
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    90

  • Dòng 10. Bạn truy xuất giá trị đã gửi bằng cách truy cập dữ liệu tại khóa

    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    53 mà bạn đã xác định trong mẫu của mình bằng thuộc tính HTML
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    52 trên các phần tử
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    51 của bạn

  • Dòng 11 đến 14. Bạn thiết lập một câu lệnh có điều kiện để thêm hoặc xóa hồ sơ người dùng khỏi

    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    94 của người dùng hiện đang đăng nhập, tùy thuộc vào nút họ đã nhấn. Hai nút gửi các giá trị khác nhau cho khóa dữ liệu
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    53 mà bạn sử dụng để phân biệt giữa hai nút

  • dòng 15. Bạn sử dụng

    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    96 trên
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
    
    86 để truyền các thay đổi đối với
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    94 trở lại cơ sở dữ liệu

Cuối cùng, sau tất cả hành động có điều kiện này—mà Django chỉ thực thi khi trang được gọi với yêu cầu POST từ một lần gửi biểu mẫu—bạn kết xuất lại cùng một mẫu.



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99 sẽ tải lại trang và hiển thị chính xác danh sách người theo dõi đã thay đổi sau khi nhấp vào nút

Ghi chú. Trong trường hợp bạn chưa tạo hồ sơ cho bạn và những người dùng hiện tại của bạn, bạn có thể gặp phải lỗi



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
90 khi thực hiện yêu cầu POST. Để tránh lỗi này, bạn có thể xác minh rằng người dùng của bạn có hồ sơ trong chế độ xem


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
91 của bạn



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
9

Khi bạn gọi chế độ xem



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
91, trước tiên bạn kiểm tra xem


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
93 có chứa


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
91 với


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
95 không. Nếu hồ sơ bị thiếu, thì bạn tạo hồ sơ cho người dùng của mình trước khi tiếp tục

Giờ đây, bạn có thể theo dõi và hủy theo dõi hồ sơ của những người dùng khác trên mạng xã hội của mình. Xác nhận rằng điều này hoạt động như mong đợi bằng cách khám phá các hồ sơ trong danh sách hồ sơ của bạn và theo dõi và hủy theo dõi chúng một cách có chọn lọc. Bạn sẽ thấy danh sách trong bản cập nhật thanh bên của mình tương ứng

Với điều này, bạn đã hoàn toàn kết nối logic theo dõi và hủy theo dõi của back-end với giao diện người dùng. Bạn đã thêm một phần tử HTML

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
8 và hai nút trong


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
8. Bạn cũng đã triển khai logic mã trong
<div class="buttons has-addons">
    <button class="button is-success">Followbutton>
    <button class="button is-danger">Unfollowbutton>
div>
81 để dịch các lần nhấn nút thành các thay đổi ảnh hưởng đến cơ sở dữ liệu của bạn

Loại bỏ các quảng cáo

Bước 8. Tạo Back-End Logic cho Dweets

Tại thời điểm này, bạn có thể chọn theo dõi và hủy theo dõi hồ sơ người dùng thông qua giao diện người dùng của ứng dụng. Tuy nhiên, ngay cả khi bạn theo dõi một hồ sơ khác, thì cũng không có gì để xem. Tại thời điểm này, người dùng mạng xã hội của bạn không thể tạo bất kỳ nội dung nào. Thay đổi điều này sẽ là thách thức tiếp theo của bạn

Ở cuối bước này, bạn sẽ tạo phần phụ trợ cho nội dung mạng xã hội của mình—các tin nhắn dựa trên văn bản ngắn được gọi là dweets—mà người dùng có thể đăng lên ứng dụng của bạn. Bạn cũng có thể tạo các trang lưu trữ cho người dùng thông qua quản trị viên Django

Giống như bạn đã động não trong tổng quan dự án ở phần một, mạng xã hội của bạn sẽ tập trung vào các tin nhắn văn bản dạng ngắn. Để tưởng nhớ Twitter trường học cũ, bạn sẽ đặt giới hạn ký tự thành 140 ký tự

Ghi chú. Cho đến nay, loại nội dung mà người dùng đăng có thể là bất cứ thứ gì. Trọng tâm của bạn chỉ là kết nối giữa những người dùng ứng dụng của bạn

Bây giờ bạn đang nhận được cụ thể. Nếu bạn muốn cho phép một dạng nội dung khác trong mạng xã hội của mình, thì bạn cần rẽ sang một hướng khác vào thời điểm này

Bạn đã lên kế hoạch tạo một mô hình tập trung vào các nguyên tắc cơ bản về nội dung mà người dùng của bạn sẽ chia sẻ trên mạng xã hội của bạn. Bạn cũng đã quyết định rằng bạn sẽ liên kết các tệp tin với tài khoản người dùng và ngoài nội dung văn bản, điều duy nhất mà các tệp tin cần ghi lại là thời gian gửi

Yêu cầu python nhấp vào nút gửi

Sơ đồ mối quan hệ thực thể (ER) cho thấy mô hình



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99 của bạn sẽ có mối quan hệ khóa ngoại với mô hình
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
00 tích hợp sẵn của Django. Mỗi người dùng mạng xã hội của bạn sẽ có thể tạo nhiều dweets được liên kết với tài khoản của họ

làm người mẫu

Bạn cần một cách để lưu trữ các tin nhắn dựa trên văn bản do người dùng tạo trong cơ sở dữ liệu của mình. Để làm được điều đó, bạn sẽ cần tạo một mô hình mới trong

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
01



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
9

Mô hình



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99 của bạn chỉ cần ba trường

  1.  1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    3. Trường này thiết lập mối quan hệ mô hình với mô hình
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    00 tích hợp của Django. Bạn xác định rằng đó sẽ là mối quan hệ khóa ngoại, nghĩa là mỗi dweet sẽ được liên kết với một người dùng. Bạn cũng chuyển
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    05 đến
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    06, cho phép bạn truy cập các đối tượng dweet từ phía người dùng của mối quan hệ thông qua
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    07. Cuối cùng, bạn chỉ định rằng các dweets mồ côi sẽ tồn tại xung quanh bằng cách đặt
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    08 thành
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    09

  2.  1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    10. Trường này xác định loại nội dung của bạn. Bạn đặt thành trường ký tự có độ dài tối đa là 140 ký tự

  3.  1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    11. Trường cuối cùng của mô hình mới của bạn ghi lại ngày và giờ gửi tin nhắn văn bản. Đặt
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    12 thành
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    13 trên đối tượng
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    14 đảm bảo rằng giá trị này được tự động thêm vào khi người dùng gửi một tin nhắn

Và với ba trường này được thiết lập, bạn đã hoàn thành việc tạo mô hình



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99 của mình. Tiếp theo, bạn cần áp dụng các thay đổi cho cơ sở dữ liệu của mình bằng cách chạy các lệnh quản lý
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
16 và
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
17

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
0

Sau khi chạy cả hai lệnh này, những thay đổi của bạn trong

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
18 đã được áp dụng cho cơ sở dữ liệu của bạn. Phần cuối của bạn đã được thiết lập để ghi lại các tin nhắn, nhưng cho đến nay, bạn không có cách nào để gửi chúng qua ứng dụng web của mình

Thêm Dweets thông qua giao diện quản trị

Cuối cùng, bạn muốn người dùng mạng xã hội của mình gửi tin nhắn qua giao diện người dùng của ứng dụng web của bạn. Tuy nhiên, trước tiên, bạn phải đảm bảo rằng mọi thứ bạn đã xây dựng cho đến nay đều hoạt động như mong đợi. Bạn sẽ bắt đầu bằng cách thêm chức năng tạo các trang lưu trữ thông qua giao diện quản trị của mình

Để có quyền truy cập vào mô hình



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99 mới của bạn trong giao diện quản trị, bạn cần phải đăng ký nó

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
1

Nếu bạn đi tới giao diện quản trị của mình sau khi thực hiện việc này, bạn sẽ thấy tùy chọn Dwitter → Dweets hiện hiển thị. Nhấp vào nút + Thêm bên cạnh và tạo một dweet mới

Yêu cầu python nhấp vào nút gửi

Lưu ý rằng bạn cần gán một đối tượng người dùng hiện có cho dweet mà bạn muốn tạo. Bạn có thể chọn một từ danh sách thả xuống. Bạn cũng cần thêm văn bản cho nội dung của dweet. Bạn không cần thêm ngày khi tạo vì Django sẽ tự động xử lý việc đó sau khi bạn nhấp vào Lưu

Bằng cách cung cấp người dùng và nội dung văn bản, bạn có thể tạo một trang mới thông qua giao diện quản trị của mình

Yêu cầu python nhấp vào nút gửi

Tuy nhiên, đối tượng Dweet (1) không phải là một tên quá mô tả cho dweet đã gửi. Nó sẽ thân thiện với người dùng hơn nếu bạn có thể biết ai đã viết nó, khi họ viết nó và ít nhất là phần đầu của văn bản. Bạn có thể thay đổi cách Django hiển thị đối tượng



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99 bằng cách ghi đè
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
41 trong định nghĩa lớp của


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
4

Bằng cách thêm phương thức

 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
41 tùy chỉnh trả về chuỗi f Python bao gồm tên người dùng, ngày tạo và ba mươi ký tự đầu tiên của nội dung thư, bạn đã cải thiện cách Django hiển thị các tệp tin trên giao diện quản trị

Yêu cầu python nhấp vào nút gửi

Với thay đổi này được áp dụng, trang tổng quan về dweet trong giao diện quản trị Django sẽ vẫn hữu ích và có thể đọc được ngay cả khi người dùng của bạn đã thêm nhiều dweet bổ sung

Ghi chú. Để chuẩn bị cho các bước sau, bạn nên tạo một vài tệp tin và chỉ định chúng cho những người dùng khác nhau trong ứng dụng của bạn. Đảm bảo rằng bạn có ít nhất ba người dùng và tất cả họ đều có một vài ví dụ về tin nhắn để bạn có thể xem tin nhắn nào hiển thị khi bạn theo dõi hoặc hủy theo dõi hồ sơ

Trong bước này, bạn đã tạo một mô hình mới cho nội dung văn bản của mạng xã hội Django, đăng ký nó trong giao diện quản trị và cải thiện cách Django hiển thị các đối tượng. Trong phần tiếp theo, bạn sẽ thêm mã để yêu cầu Django hiển thị các tệp tin này ở mặt trước của ứng dụng web của bạn

Loại bỏ các quảng cáo

Bước 9. Hiển thị Dweets trên giao diện người dùng

Tại thời điểm này, bạn đã thiết lập các cấu hình và bạn có thể tạo các tệp lưu trữ thông qua giao diện quản trị Django của mình. Khi kết thúc bước này, bạn sẽ có thể xem các bản ghi thông qua giao diện người dùng và bạn sẽ có thể hiển thị các bản ghi của hồ sơ bạn theo dõi trên trang bảng điều khiển

Nếu bạn chưa tạo bất kỳ tệp tin nào thông qua giao diện quản trị, thì hãy quay lại đó, tạo một số tệp tin và gán chúng cho những người dùng khác nhau. Có một số trang để kiểm tra sẽ hữu ích khi bạn đã thiết lập trang bảng điều khiển để hiển thị các trang của hồ sơ bạn theo dõi

Hiển thị Dweets cá nhân trên mỗi trang hồ sơ

Đầu tiên, bạn sẽ quay trở lại



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
8, nơi bạn sẽ thêm mã để hiển thị tất cả các tệp tin của người dùng được liên kết với hồ sơ này. Sau đó, bạn sẽ tạo một trang web khác sẽ hiển thị tất cả các trang web của những người bạn theo dõi

Trên trang hồ sơ, bạn có quyền truy cập vào



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
91. Làm cách nào bạn có thể lặp lại các tin nhắn của người dùng theo cách tương tự như cách bạn đã làm với những người theo dõi hồ sơ?

Nếu bạn nhớ lại cách viết



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99, bạn có thể nhớ rằng bạn đã đặt
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
06 thành
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
05. Tên bạn đã xác định ở đó cho phép bạn truy cập ngược vào các đối tượng


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
99 được liên kết thông qua mô hình
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
00. Hơn nữa, bạn có thể truy cập một phiên bản
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
00 được liên kết thông qua đối tượng


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
02 mà bạn đang chuyển vào từ điển ngữ cảnh



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
0

Tốt đẹp. Với mã này, bạn đang tham gia một chuyến tham quan hoàn chỉnh qua tất cả các mối quan hệ mô hình mà bạn đã thiết lập trong dự án này. Hãy tiếp tục và sử dụng nó trong



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
8 để bạn có thể xem các tin nhắn của từng người dùng trên trang hồ sơ của họ



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
1

Thêm đoạn mã HTML này, bao gồm kiểu dáng Bulma, ngay sau khi đóng



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
04

dwitter/mẫu/dwitter/hồ sơ. htmlHiện/Ẩn



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
2

Đây là thủ thuật. Quá trình lặp lại hoạt động sau khi duyệt qua các mối quan hệ mô hình của bạn và bạn có thể xem các bản ghi của người dùng hiện tại một cách dễ chịu nhờ biểu định kiểu của Bulma

Yêu cầu python nhấp vào nút gửi

Kiểm tra hồ sơ người dùng khác bằng cách nhấp vào một trong các liên kết mà bạn đã thiết lập trước đó. Tất cả các trang hồ sơ của bạn đang hoạt động như mong đợi và chúng trông rất đẹp. Giờ đây, bạn có thể khám phá những người theo dõi của mọi người, những người họ đang theo dõi và những tin nhắn của họ. Bạn cũng có thể theo dõi hoặc hủy theo dõi từng hồ sơ chỉ bằng một nút bấm. Đó chính xác là những gì mà hầu hết người dùng sẽ thấy trên trang hồ sơ của họ

Bạn vẫn đang thiếu một cách để hiển thị nguồn cấp dữ liệu của tất cả các trang web của tất cả các hồ sơ bạn theo dõi. Bạn sẽ triển khai chức năng này bằng cách cập nhật chế độ xem trang tổng quan của mình

Tạo Chế độ xem Bảng điều khiển

Bạn đã sớm thiết lập chế độ xem bảng điều khiển giữ chỗ trong phần hai của loạt bài này, điều đó có nghĩa là định tuyến thích hợp đã được xác định và Django sẽ gọi



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
05 khi bạn điều hướng đến URL cơ sở. Cho đến nay, không có nhiều thứ để xem vì nó chỉ hiển thị mẫu cơ sở của bạn. Bây giờ, bạn sẽ cập nhật điểm cuối này để làm cho nó hữu ích hơn và hiển thị trang bảng điều khiển của bạn. Trên bảng điều khiển của bạn, bạn muốn có thể làm hai việc

  1. Đọc tất cả các tin nhắn từ hồ sơ bạn theo dõi
  2. Gửi một tin nhắn

Bạn sẽ giải quyết nhiệm vụ thứ hai trong phần tiếp theo của loạt bài này. Hiện tại, bạn sẽ tập trung vào việc hiển thị các trang web của tất cả các hồ sơ bạn theo dõi. Cập nhật



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
05 trong
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
4 để trỏ nó tới một mẫu khác



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
3

Tất nhiên, mẫu đó chưa tồn tại. Để Django không phàn nàn, bạn hãy truy cập vào



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
08 và tạo một mẫu mới có tên là


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
09 và thêm một số logic mã nháp để xác nhận rằng nó hoạt động như mong đợi



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
4

Sau khi thêm đoạn mã này vào



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
09 và điều hướng đến URL cơ sở tại


{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
11, bạn sẽ thấy một danh sách chưa được định dạng chứa tất cả các tệp của tất cả các hồ sơ bạn theo dõi được trộn lẫn với nhau

Ghi chú. Nếu không có gì mới xuất hiện, hãy đảm bảo rằng người dùng quản trị của bạn theo dõi chính họ và ít nhất hai hồ sơ khác. Bạn cũng có thể kiểm tra xem những người dùng này có một số tệp tin hiện có trong cơ sở dữ liệu của bạn không

Trước khi bạn làm cho danh sách này trông đẹp hơn với phong cách của Bulma, trước tiên bạn nên xem lại các mối quan hệ mô hình mà bạn đang kết nối với nhau trong mẫu Django của mình

  • dòng 3. Bạn đi sâu vào đối tượng

     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    3 mà Django gửi với mọi yêu cầu POST hoặc GET. Nó đề cập đến người dùng đã đăng nhập, hiện tại là người dùng quản trị của bạn. Với
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
    
    85, bạn truy cập hồ sơ của người dùng quản trị mà bạn đã thiết lập khi mở rộng mô hình Django
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    00. Hồ sơ có một thuộc tính được gọi là
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    94 chứa một tập hợp tất cả các hồ sơ người dùng mà hồ sơ này theo dõi. Cuối cùng, với
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    16, bạn truy cập vào một lần lặp của bộ sưu tập đó

  • dòng 4. Bạn lồng một vòng lặp

    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    17 khác vào vòng lặp trước đó. Tại đây, bạn truy cập vào
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    18 để vào từng hồ sơ người dùng mà người dùng hiện đang đăng nhập theo dõi. Sau đó, bạn bước qua đối tượng
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    19 và
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    06,
     1<div class="buttons has-addons">
     2    {% if profile in user.profile.follows.all %}
     3        <button class="button is-success is-static">Followbutton>
     4        <button class="button is-danger">Unfollowbutton>
     5    {% else %}
     6        <button class="button is-success">Followbutton>
     7        <button class="button is-danger is-static">Unfollowbutton>
     8    {% endif %}
     9div>
    
    07 của nó, để truy cập tất cả các tin nhắn mà người dùng này đã viết. Với
    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    16, bạn lại truy cập vào một lần lặp của bộ sưu tập đó

  • dòng 5. Bây giờ bạn có quyền truy cập vào từng dweet trong

    
    
    {% extends 'base.html' %}
    
    {% block content %}
    
    <div class="column">
    
        <div class="block">
        <h1 class="title is-1">
            {{profile.user.username|upper}}'s Dweets
        h1>
        div>
        <div class="buttons has-addons">
            <button class="button is-success">Followbutton>
            <button class="button is-danger">Unfollowbutton>
        div>
    div>
    
    <div class="column is-one-third">
    
        <div class="block">
            <a href="{% url 'dwitter:profile_list' %}">
                <button class="button is-dark is-outlined is-fullwidth">
                    All Profiles
                button>
            a>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} follows:
            h3>
            <div class="content">
                <ul>
                {% for following in profile.follows.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' following.id %}">
                            {{ following }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
        <div class="block">
            <h3 class="title is-4">
                {{profile.user.username}} is followed by:
            h3>
            <div class="content">
                <ul>
                {% for follower in profile.followed_by.all %}
                    <li>
                        <a href="{% url 'dwitter:profile' follower.id %}">
                            {{ follower }}
                        a>
                    li>
                {% endfor %}
                ul>
            div>
        div>
    
    div>
    
    {% endblock content %}
    
    23. Bạn sử dụng thông tin đó để chọn thông tin bạn muốn từ mỗi tin nhắn dựa trên văn bản và tập hợp thông tin này vào một mục danh sách HTML duy nhất

Cách bạn có thể sử dụng các mối quan hệ mô hình của Django để tìm chính xác thông tin mà bạn đang tìm kiếm là khá hữu ích, ngay cả khi nó yêu cầu bạn phải thực hiện một loạt các bước khác nhau. Hãy nhớ rằng nó hoạt động theo cách này do cách bạn thiết lập mô hình của mình ở các bước trước đó

Yêu cầu python nhấp vào nút gửi

Bạn có thể lấy bút chì và lấy bản nháp cơ sở dữ liệu mà bạn đã tạo trong phần một. Cố gắng suy nghĩ về cách các mô hình của bạn được kết nối và đảm bảo rằng bạn hiểu cách thức và lý do bạn có thể truy cập vào các bộ lưu trữ theo cách bạn đã làm ở trên trước khi tiếp tục

Bây giờ bạn có thể nâng cấp



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
09 để nó phù hợp với thiết kế của phần còn lại của mạng xã hội của bạn



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
5

Bằng cách mở rộng mẫu cơ sở của bạn và thêm một số kiểu dáng thông qua các lớp CSS của Bulma, bạn đã tạo một trang bảng điều khiển hấp dẫn hiển thị nguồn cấp dữ liệu của bạn hiển thị tất cả các trang web của tất cả các hồ sơ bạn theo dõi

Yêu cầu python nhấp vào nút gửi

Mỗi người dùng sẽ thấy nguồn cấp dữ liệu cá nhân của riêng họ, dựa trên hồ sơ họ theo dõi. Đó là một bước nhảy vọt cho mạng xã hội Django của bạn

Trong bước này, bạn đã thêm một mẫu bảng điều khiển mới hiển thị nguồn cấp dữ liệu cá nhân của các tin nhắn và bạn cũng đã thêm mã vào



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
8 để hiển thị các tin nhắn của mỗi người dùng trên trang hồ sơ của họ. Trong bước tiếp theo, bạn sẽ thêm biểu mẫu Django cho phép người dùng gửi tin nhắn từ trang tổng quan của họ

Loại bỏ các quảng cáo

Sự kết luận

Trong phần này của loạt bài hướng dẫn, bạn đã tiếp tục xây dựng giao diện người dùng và mặt sau của mạng xã hội nhỏ của mình bằng Django. Giờ đây, người dùng ứng dụng của bạn có thể theo dõi và hủy theo dõi các hồ sơ người dùng khác và xem các tin nhắn dựa trên văn bản của các hồ sơ mà họ theo dõi

Trong quá trình xây dựng dự án này, bạn đã học được cách

  • Tạo giao diện front-end để theo dõi và hủy theo dõi hồ sơ
  • Gửi và xử lý yêu cầu POST ở Django bằng các nút
  • Thiết lập mô hình cho nội dung dựa trên văn bản của bạn
  • Xây dựng các mẫu theo kiểu để hiển thị nội dung trên giao diện người dùng
  • Sử dụng các mối quan hệ mô hình phức tạp trong mã mẫu

Trong phần tiếp theo và cũng là phần cuối cùng của loạt bài hướng dẫn này, bạn sẽ tạo một biểu mẫu Django cho phép người dùng của bạn gửi các bản tin mới thông qua giao diện người dùng. Bạn cũng sẽ đảm bảo rằng nội dung gửi là hợp lệ. Cuối cùng, bạn sẽ cải thiện trải nghiệm người dùng bằng cách thêm điều hướng và sắp xếp các tin nhắn để hiển thị các tin nhắn mới nhất trước tiên

Bạn có thể tải xuống mã mà bạn nên có vào cuối phần này của dự án bằng cách nhấp vào liên kết bên dưới và chuyển đến thư mục



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
26

Nhận mã nguồn. Nhấp vào đây để lấy mã nguồn mà bạn sẽ sử dụng để xây dựng và xử lý các yêu cầu POST với Django

Các bước tiếp theo cho mạng xã hội cơ bản của bạn với Django

Bây giờ bạn đã hoàn thành phần thứ ba của loạt bài hướng dẫn này, bạn có thể tiếp tục đến phần cuối cùng, nơi bạn sẽ tạo và gửi các biểu mẫu HTML bằng Django

Trong phần sắp tới của loạt bài hướng dẫn này, bạn sẽ thêm logic mã cho phép người dùng của bạn tạo nội dung trên ứng dụng web của bạn bằng cách sử dụng một biểu mẫu. Bạn sẽ tạo một biểu mẫu Django trong



{% extends 'base.html' %}

{% block content %}

<div class="column">

    <div class="block">
    <h1 class="title is-1">
        {{profile.user.username|upper}}'s Dweets
    h1>
    div>
    <div class="buttons has-addons">
        <button class="button is-success">Followbutton>
        <button class="button is-danger">Unfollowbutton>
    div>
div>

<div class="column is-one-third">

    <div class="block">
        <a href="{% url 'dwitter:profile_list' %}">
            <button class="button is-dark is-outlined is-fullwidth">
                All Profiles
            button>
        a>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} follows:
        h3>
        <div class="content">
            <ul>
            {% for following in profile.follows.all %}
                <li>
                    <a href="{% url 'dwitter:profile' following.id %}">
                        {{ following }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

    <div class="block">
        <h3 class="title is-4">
            {{profile.user.username}} is followed by:
        h3>
        <div class="content">
            <ul>
            {% for follower in profile.followed_by.all %}
                <li>
                    <a href="{% url 'dwitter:profile' follower.id %}">
                        {{ follower }}
                    a>
                li>
            {% endfor %}
            ul>
        div>
    div>

div>

{% endblock content %}
27, kết nối nó với logic mã trong
 1<div class="buttons has-addons">
 2    {% if profile in user.profile.follows.all %}
 3        <button class="button is-success is-static">Followbutton>
 4        <button class="button is-danger">Unfollowbutton>
 5    {% else %}
 6        <button class="button is-success">Followbutton>
 7        <button class="button is-danger is-static">Unfollowbutton>
 8    {% endif %}
 9div>
4 và hiển thị nó trong một mẫu để người dùng của bạn có thể gửi các tệp tin từ giao diện người dùng

Bạn sẽ xây dựng trang bảng điều khiển làm trang chính của mạng xã hội của mình. Sau đó, người dùng có thể gửi nội dung của họ để nội dung đó sẽ hiển thị trên nền tảng cùng với các trang web của hồ sơ mà họ theo dõi

Hãy nhớ rằng bạn có thể tiếp tục tham khảo lại các bước trước đó khi thực hiện dự án này. Ví dụ: có thể hữu ích khi tham khảo kế hoạch mà bạn đã soạn thảo trong phần tổng quan dự án của phần đầu tiên của loạt bài hướng dẫn và cập nhật kế hoạch của bạn khi bạn thực hiện các bước còn lại

" Phần 2

Phần 3

Phần 4 »

Đánh dấu là đã hoàn thành

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Yêu cầu python nhấp vào nút gửi

Gửi cho tôi thủ thuật Python »

Giới thiệu về Martin Breuss

Yêu cầu python nhấp vào nút gửi
Yêu cầu python nhấp vào nút gửi

Martin thích tự động hóa, trò đùa ngớ ngẩn và rắn, tất cả đều phù hợp với cộng đồng Python. Anh ấy thích học hỏi và khám phá và cũng sẵn sàng nói về nó. Anh ấy viết và ghi nội dung cho Real Python và CodingNomads

» Thông tin thêm về Martin


Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Yêu cầu python nhấp vào nút gửi

Aldren

Yêu cầu python nhấp vào nút gửi

Bartosz

Yêu cầu python nhấp vào nút gửi

David

Yêu cầu python nhấp vào nút gửi

Geir Arne

Yêu cầu python nhấp vào nút gửi

kate

Yêu cầu python nhấp vào nút gửi

Philipp

Yêu cầu python nhấp vào nút gửi

Sadie

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Yêu cầu python nhấp vào nút gửi

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi