Hello Guys,
I am building a blog application using django and bootstrap (extends base.html) as well as using class based views. As you can see in the image the bootstrap is not taking effect on the add post in the image. As well as in forms.py ‘category’: forms.Select(attrs={‘class’:‘form-control’}) but it is still showing as text box not a drop down box. I am not able to understand what I am missing. could anyone please help me understand what is the issue
Thank you.
forms.py
from django import forms
from .models import Post
class PostForms(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'author','category', 'body')
Widgets = {
'title' : forms.TextInput(attrs={'class':'form-control'}),
'author' : forms.Select(attrs={'class':'form-control'}),
'body': forms.Textarea(attrs={'class': 'form-control'}),
'category': forms.Select(attrs={'class':'form-control'}),
}
class EditForms(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'author','category', 'body')
Widgets = {
'title' : forms.TextInput(attrs={'class':'form-control'}),
'author' : forms.Select(attrs={'class':'form-control'}),
'body': forms.Textarea(attrs={'class': 'form-control'}),
'category': forms.Select(attrs={'class':'form-control'}),
}
views.py
from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, TemplateView, CreateView, UpdateView, DeleteView
from cavertech.models import Post
from cavertech.forms import PostForms,EditForms
# Create your views here.
#def home(request):
# return render(request, 'home1.html',{})
class Home_view(TemplateView): # Hoeme page view
template_name = 'home.html'
class Blog_view(ListView): # Blog list view
model = Post
template_name = 'blog_list.html'
ordering = ['-date']
class Article_view(DetailView): # Blog detail view
model = Post
template_name = 'article_view.html'
class Create_post_view(CreateView): # Create Blog
model = Post
form_class = PostForms
template_name = 'add_post.html'
#fields = '__all__'
class Update_post_view(UpdateView): # update Blog
model = Post
form_class = EditForms
template_name = 'update_post.html'
#fields = ['title', 'author', 'body']
class Delete_post_view(DeleteView):
model = Post
template_name = 'delete_post.html'
success_url = reverse_lazy('blogview')
Hello @KenWhitesell thank you for guiding at the right direction, I figured the choice issue by adding the following code. As I update the category class model with new data it don’t update dynamic, How do I active that?
Note that choices can be any sequence object – not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you’re probably better off using a proper database table with a ForeignKey. choices is meant for static data that doesn’t change much, if ever.
Whenever I’m looking for a django package that does something, I generally start with searching on djangopackages.org.
In this case, searching for MFA will show you a couple of different options for doing this.
Even if you don’t want to use one of those packages, you can at least look at them to get some ideas.
Hello I am trying to get the current login user as I would like to have it as my author name in the blog post. Right now i am using js to get a user id.
add_post.html
<script>
var name = "{{user.id}}";
document.getElementById("current_user").value = name;
</script>
forms.py
class PostForms(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'author', 'category','snippet', 'body')
widgets = {
'title' : forms.TextInput(attrs={'class':'form-control'}),
'author' : forms.TextInput(attrs={'class':'form-control', 'value': '','id':'current_user','type':'hidden'}),