Can someone help me change class base to function base, I am better of function base, class base is quite confusing for me.
Url:
path("inbox", InboxView.as_view(), name='inbox'),
re_path(r"^messages/(?P<username>[\w.@+-]+)", ThreadView.as_view(), name="messages"),
Views.py:
class InboxView(LoginRequiredMixin, ListView):
template_name = 'inbox.html'
def get_queryset(self):
return Thread.objects.by_user(self.request.user)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['chat_list'] = Thread.objects.filter(Q(first=self.request.user)| Q(second=self.request.user))
#Online Users
context['online_user'] = Profile.objects.filter(friends__user=self.request.user, is_online=True)
return context
class ThreadView(LoginRequiredMixin, FormMixin, DetailView):
template_name = 'thread.html'
form_class = ComposeForm
success_url = './'
def get_queryset(self):
return Thread.objects.by_user(self.request.user)
def get_object(self):
other_username = self.kwargs.get("username")
obj, created = Thread.objects.get_or_new(self.request.user, other_username)
if obj == None:
raise Http404
return obj
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = self.get_form()
context['chat_msg'] = ChatMessage.objects.all().order_by('-timestamp')
context['chat_list'] = Thread.objects.filter(Q(first=self.request.user)| Q(second=self.request.user))
context['online_user'] = Profile.objects.filter(friends__user=self.request.user, is_online=True)
return context
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
thread = self.get_object()
user = self.request.user
message = form.cleaned_data.get("message")
ChatMessage.objects.create(user=user, thread=thread, message=message)
return super().form_valid(form)
@Scot...can you please help me modify it to function base view? The views.py
- oghomwenogunmwonyi 4 years, 9 months agoi would suggest you start with the above option i have provided, if you get stuck somewhere i can help you extending the code as per your requirement.
- scott 4 years, 9 months ago@Vengat...it is confusing and I am getting errors. I was able to do the Inbox View but I was not able to do the ThreadView... Please some one help me out, this is delaying my project. I will be glad if someone can do this for me. Thanks
- oghomwenogunmwonyi 4 years, 9 months ago@Oghomwenogunmwonyi , Yes I can help you with the code my friend. But problem is, you may not able to learn. Idea behind this platform is guide developers to right direction with right resource. Just send me your code whatever you have so far, i will take a look on it. @Teckiy, I need more points for this :P
- Vengat@Vengat.. Sure you will get points if you are able to do this. I just want to change class base to function base, i do not know much about class base. This is my code in link: https://www.ppaste.org/oDSVhlXbE
- oghomwenogunmwonyi 4 years, 9 months agohi,
Find below the code at high level , you can start extending it accordingly,
def threadview(request):
if request.method == 'GET:
obj, created = Thread.objects.get_or_new(request.user, other_username)
if request.method == 'POST':
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
form = required-form
if form.is_valid():
do the logic
return something
you can extend the code accordingly.
@Vengat.. Can you please format your code accordingly by using ppaste.org, then you send me the link, just as I did mine. Thanks
- oghomwenogunmwonyi 4 years, 9 months agoVengat has given the sample fbv below. I hope this ticket would help you take it forward from there. we are requesting the ticket can be closed from your end.
- scott 4 years, 9 months ago
@oghomwenogunmwonyi , Check this script,
def threadview(request):
if request.method == 'GET:
obj, created = Thread.objects.get_or_new(request.user, other_username)
if request.method == 'POST':
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
form = required-form
if form.is_valid():
do the logic
return something
@Oghomwenogunmwonyi . Thanks. Kindly show your support on our platform and share with your circle. This would help developers community and also I would request you to kindly start helping other developers here based on any open ticket here. Thanks in advance.
- scott 4 years, 9 months ago
Its quite simple , **get_query_set** is nothing but ```Model.objects.filter(id)```. And **get_context_data** => adding few more Queryset along with other logic. Lastly **post** => **request.method == "POST"**