First, I have model (Entry), where one field, 'prompt', is where I want to store text, "You suck bro!", in my django admin database. The second field, 'body', is a text editor I want to show on my html page.
In my views file, I have a function: def indexx(request), which renders text from the admin database, field 'prompt' on to my html page.
views.py
def indexx(request):
entry = Entry.objects.all()
return render(request, "entries/entries.html", {"ents" : entry})
urls.py
from django.urls import path
from .views import indexx
urlpatterns = [
path("", indexx)
]
This only renders text from django models: field ('prompt').
Next, in my views file, I have a class, class EditEntryView(CreateView), where "(CreateView)", from "django.views.generic import CreateView". This allows me to render the text editor on my html page. The vies and urls are under the same python files as well.
views.py
class EditEntryView(CreateView):
model = Entry
# form_class = PostEntry
fields = ["body" ]
template_name = "entries/entries.html"
urls.py
from django.urls import path
from .views import EditEntryView
urlpatterns = [
path("", EditEntryView.as_view(), name="entries")
]
This class renders the text editor.
My goal is to render the two separate get objects, (def index(request), EditEntryView(CreeateView) on to the same html page. I want to be able to loop through data in my database while the text editor remains static on the html page. The text, "You suck bro!" should be placed right above the text editor.
Any help would be helpful!
I have tried putting my def indexx(request) function, inside of the class EditEnrtyView(CreateView). That does not seem to work.
class EditEntryView(CreateView):
model = Entry
# form_class = PostEntry
fields = ["body" ]
template_name = "entries/entries.html"
def indexx(request):
entry = Entry.objects.all()
return render(request, "entries/entries.html", {"ents" : entry})
With my url pathing as:
path("", EditEntryView.as_view(), names="entries")
However, the only object that is rendered is the text editor:
CodePudding user response:
You can't do it like this.
What indexx
does is render the template entries/entries.html
with the context {"ents" : entry}
and return that as an HttpResponse object.
It's basically a ListView.
I'm not sure from the description what you want to accomplish. A CreateView is creating a new object. If you want to display all objects of that type including the new one after it is created, you would add to the CreateView a get_success_url method to redirect to the list view:
def get_success_url(self):
return reverse( 'app:indexx_url_name')
or since this doesn't have any args or kwaygs it can be simplified to
success_url = reverse_lazy( 'app:indexx_url_name')
(If you use plain reverse, you are likely to get horrible circular import errors, quite possible later when you edit something with no obvious connection to this cause)
The other thing you might do if you want a display of a list of objects which do exist while creating the new one, is to render the list in the template of the create view. You'll need to pass the list of objects as ents
to the create view's context:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['ents'] = Entry.objects.all()
return context
and then you can do the same in the CreateView as you did in indexx's template using Django template language.