12.2 Show the followers in the data page

Source: Internet
Author: User
I. Modification of the |-app/templates/user.html

We would like to add several columns to the user profile page as follows:

The corresponding features are illustrated as follows:

If a logged-on user A (with the right of concern) accesses another User B's page:

If a does not pay attention to B, displays a follow button, clicks attention B, if a already attention B, displays a unfollow button, clicks cancels the attention B;

Followers list the number of followers (fans) of B, and click on the list of followers showing B;

Followed lists the number of followers (idols) of B, and clicks on the list of the followers who show B;

If B pays attention to a, then displays a Followes you tag.

Modify the code as follows:

<p> {% if Current_user.can (permission.follow) and current_user!= user%} {% if not CURRENT_USER.IS_FO llowing (user)%} <a href= "{{url_for (' Main.follow ', Username=user.username)}}" class= "Btn btn-primary" ; follow</a> {% Else%} <a href= "{{url_for (' Main.unfollow ', Username=user.username)}}" Clas s= "Btn Btn-default" >Unfollow</a> {% endif%} {% endif%} <a href= ' {url_for (' Main.follower S ', Username=user.username)}} ">followers: <span class=" badge ">{{user.followers.count ()}}</span> </a> <a href= "{{url_for (' main.followed_by ', Username=user.username)}}" >followed: <span class= "badge" >{{User.followed.count ()}}</span></a> {% if current_user.is_authenticated and Current_User u

         Ser and user.is_following (current_user)%} <span class= "label Label-default" >follows {% ENDIF%} </p> 
two. Modify the routing function

1. Main.follow

@main. Route ('/follow/<username> ')

@login_required

@permission_required (permission.follow)

def follow (username):

    user = User.query.filter_by (username=username).

    If not User:

        Flash (' Invalid user. ')

        Return Redirect (Url_for ('. index '))

    if current_user.is_following (user):

        Flash (' Your are already following this User. ')

        Return Redirect (Url_for ('. User ', username=username))

    current_user.follow (user)

    Flash (' Your are now Following%s '% username) return

    redirect (Url_for ('. User ', username=username))

Code Explanation:

As we said before, only if you have logged in (1), User A with the concern (2), access to another user (3) B's page, will be concerned about different situations or take off B;

So we use the IF statement, only meet the above three conditions when the page will show attention and take off the button, the same we use the modifier, only the first two conditions to meet, we can access the attempted function. To prevent the user from accessing the link directly due to a direct access to the attention link. 2. Main.unfollow

@main. Route ('/unfollow/<username> ')
@login_required
@permission_required (permission.follow)
def unfollow (username):
    user = User.query.filter_by (username=username).
    If not User:
        Flash (' Invalid user. ')
        Return Redirect (Url_for ('. index '))
    if not current_user.is_following (user):
        Flash (' Your never follow this user. ') return
        Redirect (Url_for ('. User ', username))
    current_user.unfollow (user)
    Flash (' have already unfollowed%s. '% username ' return
    redirect (Url_for ('. User ', username=username))
3. Main.followers

When the user clicks on the followers link, the View function processes the request and returns the username of the corresponding user.

@main. Route ('/followers/<username> ')

def followers (username):

    user = User.query.filter_by (username= username).

    If not User:

        Flash (' Invalid user. '

        ) Return Redirect (Url_for ('. index '))

    page = request.args.get (' page ', 1, type=int)

    pagination = User.followers.paginate (page, per_page=current_app.config[' Flasky_followers_per_page '), Error_out=false)

    follows = [{' User ': Item.follower, ' timestamp ': Item.timstamp} for item in Pagination.items] return

    render_tempalte ( ' Followers.html ', User=user, title= "followers of", Follows=follows, endpoint= ". Followers", pagination=pagination)

Code Explanation:

Pagination: Divides all the records into Per_page pages, returns page objects on the page, and the Items property gets all the records on page. Page is obtained by the query parameter page, which defaults to 1.

follows the page through all the records, getting all the attention and attention timestamps.
4. Main.followed_by

When the user clicks on the followed link, the view function processes the request, returns the parameter username the corresponding user's followers.

@main. Route ('/followed_by/<username> ')
def followed_by (username):
    user = User.query.filter_by ( Username=username).
    If not User:
        Flash (' Invalid user. '
        ) Return Redirect (Url_for ('. index '))
    page = request.args.get (' page ', 1, type=int)
    pagination = User.followed.paginate (page, per_page=current_app[' Flasky_followers_per_page '], error_out=false)
    followeds = [ {' user ': item.followed, ' timestamp ': Item.timestamp} for item in Pagination.items] return
    render_template (' Followers.html ', User=user, title= "Followeds of", endpoint= '. Followed_by ', pagination=pagination, followeds= Followeds)
5.followers.html Templates

Let's look at the final return statement for the Main.followers view function:

Return Render_tempalte (' followers.html ', User=user, title= "followers of", Follows=follows, endpoint= ". Followers", Pagination=pagination)

We return the first two parameters for the title of the followers page, the third parameter is all of the user's followers, displayed in a table, the last two parameters for paging use, we click the followers button when we want such a page:



{% extends ' base.html '%}


{% import ' _micros.html ' as Micros%} {% block title%} Flasky-{{title}}{{user.username}}{% endblock%} {% block page_content%} <div class= ' Page-header ' >

            ; h1>{{title}}{{user.username}} 
6. Supplementing 

Followers.html the macros in the page, the code is as follows, the macro **kwargs parameters are automatically passed:

{% Macro Pagination_widget (pagination, endpoint)%} <ul class= "pagination" > <li {% if not Pagination.has_prev %} class= "disabled" {% endif%}> <a href= "{% if Pagination.has_prev%} {url_for (endpoint, page=pagination . Page-1, **kwargs)}} {% else%} # {% endif%} ' >«</a> </li> {% to P in Pagina
                Tion.iter_pages ()%} {% if p%} {% if p = = pagination.page%} <li class= ' active ' >
            <a href= "{{url_for (endpoint, page=p, **kwargs)}}" >{{P}}</a> </li> {% Else%} <li> <a href= ' {{url_for (endpoint, page=p, **kwargs)}} ' >{{P}}&lt ;/a> </li> {% endif%} {% Else%} <li class= ' disabled ' ><a href= ' # ">...</a></li> {% endif%} {% ENDfor%} <li {% if not pagination.has_next%} class=" Disab
   Led "{% endif%}>     <a href= ' {% if Pagination.has_next%} {{url_for (endpoint, page=pagination.page+1)}} {% else%} # {% en Dif%} ">»</a> </li> </ul> {% ENDMACRO%}







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.