Blog with Django (Codemy.com) - 17 How to restrict who can make blog posts (Java Script)

이다연·2021년 3월 31일
0

Django

목록 보기
24/33

Problem

When we post, dropdown bar shows available uers, anyone can pick anyone.

Because frontend knows user.id and who logged in, but backend doesn't know it yet.

in order to forms.py to go to the frontend and figure out the user and comeback

solution: use Java Sript

1. forms.py

no more select drowdown bar, change it to testinput and add id in the widgets

forms.py

class PostForm(forms.ModelForm): #inherit forms. ModelForm allows us to create form fields for our Post model.
    class Meta:
        model = Post
        fields = ('title', 'title_tag', 'author', 'category', 'body')

    #add styling: widget dictionary
        widgets = {
            'author': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'username', 'id': 'cats'} ),}

2. add_post.html

view the source code to identify id

since we added id as 'users_id' in the forms.py, it's now showing it in the html code.

Add JS script down below.
variable 'name'
user.id, user.username, user.first_name, etc

<script>
    var name = "{{ user.id }}";
    document.getElementById("users_id").value
             = name;

</script>

Now it's showing user_id in the form.
we want input box to be value not a placeholder. Leave value as empty as JS will put the value in it.

'author': forms.TextInput(attrs={'class': 'form-control','value': '', 'id': 'users_id'} ),

make the field hidden so user cannot edit their username.

'author': forms.TextInput(attrs={'class': 'form-control','value': '', 'id': 'users_id', 'type':'hidden'} ),

value is empty but JS put id in there. We just can't see it.

profile
Dayeon Lee | Django & Python Web Developer

5개의 댓글

comment-user-thumbnail
2022년 5월 11일

I couldn't observe you're completely positioned up as I get bored very short however notable to see a person chatting about gadgets like this.
http://bangaloreescortsgirls.com/
http://www.bangalorecallgirls.in/
http://www.kalpanachawla.in/
http://www.priyasenbangaloreescorts.co.in/
http://www.thebangaloreescorts.com/
http://www.priyaoberoi.in/

답글 달기
comment-user-thumbnail
2022년 5월 11일

Make tremendous you could pay whoever you're borrowing from decrease returned in a sincere quantity of time, without the troubles.
http://riyasingh.in/
http://www.komalsharma.in/
http://www.shilpiescort.in/
http://www.bangaloreescortagency.co.in/
http://www.blrescorts.in/

답글 달기
comment-user-thumbnail
2022년 5월 11일

Very terrific article precious creator, I significantly liked I want to percentage this publish to my Facebook or myspace or Twitter. Thanks
http://www.hotbangaloreescorts.in/
http://www.perfectbabes.in/
http://www.callgirlsinbengaluru.com/
http://www.bangalorehotcallgirls.in/
http://www.bangalorehotcallgirls.com/
http://www.escortsbangalore.co.in/

답글 달기
comment-user-thumbnail
2022년 5월 11일

I virtually bear in mind you've got were given stated a few very thrilling info, thankyou for the position up.
http://www.bangalorehotescorts.co.in/
http://www.bangaloreescortsgirls.in/
http://www.bangaloreprincess.in/
http://www.miky.in/
http://www.bangalore-escort.org/

답글 달기
comment-user-thumbnail
2023년 12월 29일

KD SOFTWARE is a software and website design in Agra firm with offices in AGRA, Mainpuri, Mathura, Firozabad, Etah, and Shahjahanpur, Uttar Pradesh. KD SOFTWARE developed customized software based on platforms and technologies such. Net, PHP, HTML, JavaScript, and CSS and in compliance with market demands. The developers at Team KD SOFTWARE have experience creating complicated, sizable websites with a range of design approaches. KD SOFTWARE provides a wide range of services, including website design and development. PHP programming, software development, digital marketing, SEO, and net programming in Agra
https://kdsoftware.in
https://websitedesigningnoida.com

답글 달기