Tweet Me has basic features of "Tweeter". This project is to learn and play around with Python Django framework, jQuery, and Bootstrap. This is my first Django project. Thus, below, I am attaching my daily project report so that I can share problems that I confronted and things that I learned.
-
Django setup and setting
virtualenv -p python3 tweetme && cd tweetme source bin/activate mkdir src cd src pip freeze pip install django==1.10.3 pip freeze django-admin.py startproject tweetme .
- Where manage.py is located is the root of django project
- https://github.com/codingforentrepreneurs/Guides/blob/master/all/Create_a_Local_Django_Project.md
-
Django settings
- Created settings folder under tweetme and added init, base, local, and production files
- local and production file basically overloads base file
- setting.py from tweetme is copied and pasted to those newly created files (+ edit directory address) and deleted setting.py
- add in init file. -> now runnable.
-
Serve static files
- https://docs.djangoproject.com/en/1.8/howto/static-files/
- created static-storage in the src folder
- created static-serve file with src folder
- edited static file setting in local.py
python manage.py collectstatic You have requested to collect static files at the destination location as specified in your settings: /Users/PatriciaChun/Desktop/folder/code/tweetme/static-serve
- Inside static-serve file, you can see admin files created.
- when you collectstatic, it moves static files to static-serve
-
View function and templates
- create views.py -> view convention. rendering home page.
- change url.py and create home.html to render from views.py
-
Integrate Bootstrap
- css, js files are static files. so we need to take care of them.
- files can be cached. CDN This is why storing static files is important.
- after making changes in static files, python manage.py collect static
- {% load static %}
-
Build a TweetMe app.
- django - easy to create user. Built-in user app.
python manage.py startapp tweets
- build our own model models.py in tweets.
- add this model ‘tweets’ in local.py (adding installed apps(installed components))
python manage.py makemigrations python manage.py migrate
- Now you can find tweet model created in the admin with blank content fields.
-
Change fields in models
- https://docs.djangoproject.com/en/1.11/ref/models/fields/#field-types
- setting length limit or change model fields…etc
- Why make migrations? because we changed models.py
-
Associate a user to a tweet with foreign keys
- Foreign key connects different models together
- https://docs.djangoproject.com/en/1.11/ref/models/fields/#field-types -> find relationship fields
-
CRUD & first views
- CRUD: create, retrieve, update, delete
- create views for Retrieve in views.py and render with newly created html files
-
Query the database & context
- In views.py
- Create databases. One returns a list and one returns a single item.
- render function combines the request, the template, and the context.
- Context is really up to us.
- context printed in the rendered html file using double curly brackets. {{}}
-
Template context basics
- We are using function-based views.
- However, class-based view is more convenient and takes out a lot of work we have to do in function-based views.
-
Class-based views
- https://docs.djangoproject.com/en/1.11/ref/class-based-views/
- In this video, we are replacing the function-based views with class-based views.
- Class-based views accomplish same things.
-
Dynamic URL routing
- This video talks about regular expression.
- $: end of the expression
- regular expression match a pattern or some way of how code is going to be work.
- Regular expressions for URL
- https://github.com/codingforentrepreneurs/Guides/blob/master/all/common_url_regex.md
- function-based views doesn’t lead to 404 error if page not found.
-
Validation
- Cleaning a specific field attribute
- https://docs.djangoproject.com/en/1.10/ref/contrib/admin/
-
Create view
- CreateView & FormView
- always test in incognito tab as well
- form.user does not work. -> form.instance.user works.
- User needed & login required mixins
- The loginrequired mixin
- FormUserNeededMixin -> when not logged in, submitting a post will pop error that says you are not logged in
- LoginRequiredMixin -> redirect to login page
- Update view
- Update view.
- Delete view
- Reverse URLs
- reverse
- If there is no success_url, it automatically goes to absolute_url method in model.py
- Advanced searching
- complex lookups with q objects
- Temperate inheritance
- {% extends "base.html" %} simply mean parent template.
- {% block title %}Hello There{% endblock title %}
- {{ block.super}}
- Bootstrap media object for list view
- Media heading
- CSS forms
- Bootstrap navbar
- Bootstrap containers & columns
*
<div class='row'> <div class ='col-sm-1' style='background-color:red;’>
- 12 columns
- Tweet from Homepage
- Django crispy forms
- http://django-crispy-forms.readthedocs.io/en/latest/install.html
*
pip install --upgrade django-crispy-forms
- add installed app in base.py, local.py, and production.py so that they work at least similarly
- add
CRISPY_TEMPLATE_PACK = ‘bootstrap3’ in local.py
python manage.py migrate
- http://django-crispy-forms.readthedocs.io/en/latest/install.html
*
- Django Tests
- test.py and then
*
python manage.py test
- joincfe.com/projects/django-test-unleashed
- test.py and then
*
- API with Django rest framework
- We want to make it more dynamic!
- Ajax = asynchronized javascript request
- for Ajax, set up APIs.
- In our app, we have forms with validation. view that handles from validation and act as a storage.
- we will create serializer and another view for actual API.
- Install django rest framework - 3rd party package. specially for API.
- joincfe.com/projects/django-rest-framework
- Our goal here: we tweet, and we get the list of tweets.
- install Django rest framework
*
pip install djangorestframework
- Add ‘rest_framework’ in installed app in local.py, base.py, production.py
- API serializer & view
- ModelSerializer
- just like forms.
- but it helps us to display the data we need. also can validate the data.
- GenericViews->ListAPIView
- User display serializer
- Now we have Tweet list API at api/tweet/ which is JSON data
- goal here: create a new app
- python manage.py startapp accounts
- under accounts, create init.py and serializers.py
- Filling out serializers.py, add accounts app in setting files. (user serializers)
- And then add new serializers to tweet’s serializers.
- Then, we find updated tweet list api. (account info specified. )
-
Ajax list view
- Jquery is a library of Javascript
- selector
-
jQuery search
-
Ajax & create API view
-
Date display
- Now we want to change ordering. -> newest tweet on the top
- Model manager is another way you can do.
-
Attach Tweet & Empty form
-
Counting characters
- selector
- monitor the edit.
- depending on condition, we change the class.
- validation
- max length.
-
Auto search
- timeout. last thing you type, it will release.
-
Pagination & Ajax load more
- disadvantage now: this list. all the tweets come up. we one small set. and ;let it load. -> pagination
- multiple pages - pagination.
- now you can check count variable.
- updated fetch tweet function to go based off of url.
- whatever the url in the results from the database
- once we grab url, we do by actual button #(‘loadmore') and
-
User detail view
- build user profile. 1. user name follow button and unfollow button
- get_slug_field()
-
Profile URL and API Update
- We are going to use UserDisplayializer to include url
- we already did follower_count one
-
User Profile & Followers
- We are going to create user profile. it will include users that we are following.
-
Model Manager for Following
- we have to actually update
-
Toggle Follow
-
Model Manager for Follow Toggle
- Update model manager to have a few new method. one is checking if user is following other users.second one is doing the toggle with actual process.
- The reason why we created toggle_follow() and is_following() is that later we can use these in any view.
-
Signals
- need signal that if user is created, check user profile is created.
- post_save: signal that is sent at the end of save method.
-
Signal Tests
- We want to specifically test UserProfile function.
- python manage.py test
-
Display Following Tweets
-
jQuery Hashtag
- We have to update our tweet list to include our own tweet. Tweets of the user is missing currently.
-
Hashtags App
- we are going to create a new app for hashtag
- python manage.py startapp hashtags
- python manage.py makemigrations
- python manage.py migrate
- Retweet Part 1
- we are gonna do retweet. copy from original. 1 top tweet and a bunch of tweet below it that are coming from the top tweet.
- after changing model, we need to make migration and migrate.
- create is_retweet
-
Retweet Part 3
- prevent retweet happening if this already happened.
- not allowing to retweet today. but after that day, you can retweet everyday.
- pip install pytz
-
Retweet Part 4 with jQuery
-
Parse Username & Hashtag with Python Regex
- Highlighting user with @.
- Python regular expression.
- Custom Django Signal
-
Tweet List Loading Everywhere
- update Tweet list to have our javascript located in different place.so that we can user it anywhere.
-
User Tweets API Endpoint
-
Like Tweet
- like button on out tweet
- create the click function for it.
-
Display Likes & Verb
- update serializer to show the liked account to a given model.
-
jQuery Attach Tweet Refactor
- create reply tweet
- Display Reply Tweet & Fix Likes
bug with migration………fixed.
* https://stackoverflow.com/questions/36053014/no-such-column-after-adding-a-field-to-the-model
2. A Form in a Bootstrap Modal
* Modals - live demo
* http://getbootstrap.com/javascript/#modals
* from id to class =>
-
Search Tweets & Users
- update search. render some sort of list.
-
Hashtag Tweet List
-
Recommended Users & Custom Template Tags
-
Registration & Auth