Change Class Base to Function Base

Submitted 3 years, 9 months ago
Ticket #48
Views 258
Language/Framework Python
Priority Medium
Status Closed

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)
Submitted on Jul 28, 20
add a comment

2 Answers

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"**

Submitted 3 years, 9 months ago

@Scot...can you please help me modify it to function base view? The views.py

- oghomwenogunmwonyi 3 years, 9 months ago

i 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 3 years, 9 months ago

Okay I will try that

- oghomwenogunmwonyi 3 years, 9 months ago

@Oghomwenogunmwonyi how its going ?. let us know if you stuck.

- Vengat 3 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 3 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 3 years, 9 months ago

@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 3 years, 9 months ago

hi,
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 3 years, 9 months ago

@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 3 years, 9 months ago

Vengat 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 3 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

Submitted 3 years, 9 months ago

@Vengat... Thanks its all I want.

- oghomwenogunmwonyi 3 years, 9 months ago

@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 3 years, 9 months ago


Latest Blogs