Posts

Showing posts from May, 2021

step3

 1. adding user to the tweets   update your models.py bu adding user column as   from   django . conf   import   settings User=settings.AUTH_USER_MODEL class   Tweet ( models . Model ):      user=models.ForeignKey(User,on_delete=models.CASCADE)      content  =  models . TextField ()      image  =  models . ImageField ( upload_to  = 'images/' , blank = True , null = True )   #null is not required in db blank is not req in django one user have many other things also but user will be the one 2. open shell and test for the filter      from tweets.models import Tweet   obj=Tweet.objects.filter(content="hello")     obj     (output:<QuerySet [<Tweet: Tweet object (71)>]>)i.e in 71st obj content is hello bj=Tweet.objects.filter(user=1)  bj ...

step2:

1. add form validation :   if you want your tweet must be no longer than 400 characters than you can add function in models.py as     class   TweetForm ( forms . ModelForm ):      class   Meta :          model = Tweet          fields =[ 'content' ]             labels ={ 'content' : 'enter your content' }          help_text ={ 'content' : 'enter your content' }           error_messages ={ 'content' :{ 'required' : 'name is compulsory' }}   #you can give extra error messages giving comma          widgets ={ 'name' : forms . TextInput ( attrs ={ 'class' : 'content' , 'placeholder' : 'enter content' })}  ...

steps

 1. making virtual environment and creating django project -twittercloneproject 2.creating tweet app for tweet homepage(same dir. of manage.py)-tweets 3.inside tweets->models.py : creating models for tweet         class Tweet 4. opening manage.py shell and creating some objects of model       i. python manage.py shell      ii. from tweets.models import Tweet    iii.  or you can create this by creating superuser and going to admin page: but you must register model in admin page 5.register tweets app in settings.py 6.in  urls.py       from  tweets.views  import   collect_all_data_from_models_and_render_it_as_json_format  , path ( 'tweet/' ,collect_all_data_from_models_and_render_it_as_json_format ),        7.in views.py from  . models   import   Tweet from   django . http . response   import   JsonResponse def ...