Re: Data Science and sql problem

2020-07-26 Thread Yamen Gamal Eldin
Try sql alchemy, if you need a full access with many functionality. Other
than that, u can connect directly to the database through python. The exact
implementation will depend on the database type

Le dim. 26 juil. 2020 à 18:15, Gurmeet Kaur 
a écrit :

> you could try using connection and cursor to connect nd then pandas to
> read csv using readcsv function
>
> On Sun, Jul 26, 2020, 11:59 AM Prashant Singh 
> wrote:
>
>> How can i connect database with help of function in jupyter notebook and
>> then conver data into csv to build model ?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/dd90d45a-dfbf-47a6-86b2-a4e600d4a9c9n%40googlegroups.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAEbKJVi%3DPfRdjN7AxWiEb-BuUZ_cjwinBpwrbQ9-iTi7e_hyrQ%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV68PecSp_MB-%2B23-tEVzPUjdij69qKR5omgAOuLgnMYSJg%40mail.gmail.com.


Re: help me

2020-07-26 Thread Yamen Gamal Eldin
Premièrement tu peux copier l'erreur dans Google, c'est un import erreur
dans votre virtual environment du python.
He pense que la solution serra quelque chose comme "pip install "


Le lun. 27 juil. 2020 à 06:00, MUGOYA DIHFAHSIH  a
écrit :

> if you could translate the error in Enl=glish, maybe we could be of help
>
> On Mon, 27 Jul 2020 at 03:08, Deborah  wrote:
>
>> bonsoir
>>
>> je suis bloquée avec -ça je ne sais pas quoi faire
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/e10f80b0-3110-4108-8cbe-9852f16a3205o%40googlegroups.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAP%3DJD9zJdx3dSPmewVs2JqPif_QiwKXtUxpVNGOGqquuMHAeSA%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV69MBSW-ETBSN4H1WbXA7RAO%3Df25j44LbEsxHOyQhcWb6w%40mail.gmail.com.


Re: Get Personal and maching Horoscope

2020-07-22 Thread Yamen Gamal Eldin
Even though that question don't belong here.
but i guess you should think abt three things:
1 - get and show the data you need to/after processing " birthdays -
matchers"
2 - condition on matching "Which horoscope matches with the other"
3 - how to store the data and the business rule " where to store those
birthdays and in what format - where to store the information of the
matching rules"

That's how i am going to think abt it anyways.
Good day.

On Wed, Jul 22, 2020 at 1:39 PM Tobi DEGNON  wrote:

> This is not really Django related, you will better off doing a Google
> search.
>
> Le mer. 22 juill. 2020 11 h 13, Ashutosh Mishra <
> ashutoshmishra...@gmail.com> a écrit :
>
>> I want to create api's to get personal horoscope and the matching
>> horoscope by providing
>> date of birth for girl and boy,some one plz guide how  can we do this
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/76f166af-28e2-4c9e-b847-528773f55e31n%40googlegroups.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CACeK5bmywn54wB8JdBgo%2Bt03mTb5kU3SxbX3EvOzOzPu3i_S9Q%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV6-PHD9j5%2BtOAk3k9ZC3JLHhuUHkXdwddMxcNmM-NRFF-A%40mail.gmail.com.


Re: How to display a line graph in Django?

2020-07-01 Thread Yamen Gamal Eldin
There's plotting library in Django.
But if I were u, I would send the data needs plotting to the template, and
plot it using a JavaScript library like chart js for small plots or d3 J's
for more professional ones.

Le mer. 1 juil. 2020 à 06:44, ratnadeep ray  a écrit :

> Hi all,
>
> I have wrote a python program to display the line graph as follows:
>
> import matplotlib.pyplot as plt
>
> x = [1,2,3]
> y = [2,4,1]
>
>
> plt.plot(x, y)
>
>
> plt.xlabel('x - axis')
> plt.ylabel('y - axis')
>
>
> plt.title('My first graph!')
>
> plt.show()
>
>
> Now I need to display the same via Django. So I think I need to write the
> above code in the view and then redirect the same to the html template
> file. Am I right?
>
> In that case, how to send the request objects related to the line chart to
> the template?
>
> Normally, we send in the following format:
>
> def view1(request)
>
>
>  return render(request, 'display_report.html', { 'var1': "abc", ...
>  ... })
>
> So if we are trying to display the line graph generated by the above code,
> how to do so ?
>
> Please share your inputs.
>
> Thanks.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/827e0d0c-6bbb-4511-af73-9fa379df699do%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV6_cc-UbjQkSv6fRG_MbQxOWCL1goM%2BjuyBkD0vbghn2ig%40mail.gmail.com.


Re: Two models one form, or use generic CreateView

2020-06-09 Thread Yamen Gamal Eldin
It's pretty straight forward, just search about overload save function for
update view, that will make u create a save function in your form, do the
necessary actions on your instance, and voula

Le mar. 9 juin 2020 à 16:24, The Sha  a écrit :

> Ok intressant, i cant find any good exemplen. Thank you for the reply. Im
> new to django can you maybe be more specific with som examples or point me
> in the right direktion? thank you
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c484bbef-d743-48c4-b13f-1a1e5f122976o%40googlegroups.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV6-xNyap9KJ6YirraEmwZYSR-S%3DTrQn7-LrJzHa_BB-cQA%40mail.gmail.com.


Re: Two models one form, or use generic CreateView

2020-06-09 Thread Yamen Gamal Eldin
There's many ways to solve it. My favorable approach will be to override
the save method in the targeted form.

Le mar. 9 juin 2020 à 14:35, The Sha  a écrit :

> Hi
>
> I hava a problem in django, i have to models one is a CustomUser Model and
> the other is a Address model.
>
> The problem is that when i create a generic CreateView for the Address
> model and use a pk: url the address form wont get the instance of the users
> PK for adding a new address.
>
> my models look like this, Even though i pass the PK in the URL the
> CreateView does not understand that i want to add a address to user with
> id: 42 in this example. How can i solve this problem?
>
>
>
>
>
>
>
> The URL
>  path('/address', views.AddressCreateView.as_view(),
> name='Address-add'),
>
>
> *This is my view*
> class AddressCreateView(CreateView):
> model = Address
> template_name = 'members/address_form.html'
> success_url = reverse_lazy('MemberIndex')
> fields = ('CustomUser', 'address', 'zip_code', 'city', 'state',
> 'active')
>
>
> *The model*
> class CustomUser(AbstractBaseUser, PermissionsMixin):
> email = models.EmailField(_('email address'), unique=True)
> personal_Id = models.CharField(max_length=12, blank=False)
> first_name = models.CharField(_('first name'), max_length=30, blank=
> True)
> middle_name = models.CharField('middle name', max_length=30, blank=
> True)
> last_name = models.CharField(_('last name'), max_length=30, blank=True
> )
> is_staff = models.BooleanField(default=False)
> is_active = models.BooleanField(default=True)
> date_joined = models.DateTimeField(default=timezone.now)
> avatar = models.ImageField(upload_to='images', blank=True)
>
> def __str__(self):
> return self.email
>
> def full_name(self):
> return '%s %s %s' % (self.first_name, self.middle_name, self
> .last_name)
>
> USERNAME_FIELD = 'email'
> REQUIRED_FIELDS = []
>
>
> objects = CustomUserManager()
>
> class Address(models.Model):
> CustomUser = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
> address = models.CharField(max_length=60, blank=False)
> zip_code = models.CharField(max_length=8, blank=False)
> city = models.CharField(max_length=30, blank=False)
> state = models.CharField(max_length=30, blank=False)
> active = models.BooleanField(name='active', default=True, editable=
> True)
> address_reg_date = models.DateTimeField(default=timezone.now)
> class Meta:
> verbose_name_plural = "Address"
>
> def __str__(self):
>  return self.address +', '+ str(self.zip_code) + ', ' + self.city
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d0d3596f-6c0e-4928-8e57-7eb5476821d3o%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV69AZvtkpK8g%3Dvbo16Lri%3Dh83MkD6Uch8e66sOio6OdB-w%40mail.gmail.com.


Re: How to retrieve dynamically the data ,submitted by a form in an html page, in a python function pointing to another html page in views.py?

2020-06-08 Thread Yamen Gamal Eldin
You can have both forms in a formset and handle both of them in a single
url, and you can handle your initial validations using HTML and javascript.
docs.djangoproject.com/en/3.0/topics/forms/formsets/

Le lun. 8 juin 2020 à 14:24, Rupesh Dahal  a écrit :

> You can save the data to a session but I would recommend you to store data
> in js in the browser itself.
>
> On Monday, June 8, 2020 at 1:54:53 AM UTC+5:45, Pierre Jutard wrote:
>>
>> I have 2 html pages A and B. The user fills the AForm in the 'A' HTML
>> page(URL: A) then he will go to the 'B' html page (url: A/B) but i would
>> like to keep the AForm data of the 'A'HTML page in the python code in order
>> to use it after in the python function pointing to the 'B' HTML page.
>>
>> My idea was to retrieve the form data in parameter of the function
>> pointing to the 'B' HTML page.
>>
>> First:
>> I have tried to point the form data to the 'B' HTML page via this line in
>> the 'A' HTML page code:
>> 
>>
>> This is the function pointing to the A HTML page:
>>
>> def A_HTML_Function_view(request):
>> AForm = A_Form()
>> if request.method == 'POST':
>> AForm_result = A_Form(request.POST)
>> BForm
>> return render(request, 'AppName/B.html',{'Bform':Bform})
>> else:
>> AForm = A_Form()
>> return render(request, 'AppName/A.html',{'AForm':AForm})
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/97798c99-102b-47d1-ab1d-e2020f93c0c1o%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV6_nfn-0SkdFbutUAT2ohu0HPxjrqF%2Bi76RubK7MkvuewQ%40mail.gmail.com.


Re: Help Me!!

2020-06-08 Thread Yamen Gamal Eldin
That can be achieved by an onclick listener on your button using
JavaScript, that's not a Django related question.

Le lun. 8 juin 2020 à 20:12, _M_A_Y_A_N_K_  a
écrit :

> Do you already have a python code for StopWatch...? If so then you can
> directly use that python code in your django view and send it to the HTML
> page.
> Playing with Button as Start / Stop can be done using HTML and (or)
> JavaScript.
>
> Let me know if you need help in python code for StopWatch.
>
> Hope this helps.
>
> Thanks & Regards,
> -
> Mayank Tripathi
>
> On Mon, Jun 8, 2020 at 12:26 PM meera gangani 
> wrote:
>
>> Hello,
>>
>>   I want to create Stopwatch in django where, we provide One start
>> button and that turned into stop button,
>>it means when we click the start button that turns
>> into a stop button.
>>
>> Please help me out!!
>>
>>
>> Thanks in advance
>> -Meera Gangani
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CANaPPPJr5o9CYHEYfgHuS-t9mMoGJ3K1tjg5FMwQa%3DWbir9QTQ%40mail.gmail.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CALQo0B-UkS4hj2TmtzSw0Z0N9NJ14fMeW%2BhfZY5SBmBoAuyosQ%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV69vG8mCDNFrV8qeEgeA2c682Hi7p-9VSHD5w-7gwOL8%3DA%40mail.gmail.com.


Re: Help me!

2020-06-08 Thread Yamen Gamal Eldin
Django channels, they have a great tutorial within the docs.

Le lun. 8 juin 2020 à 09:53, Agnese Camellini 
a écrit :

> it's enough that you google django chat system that everything will come
> to you magically.
>
> On Mon, 8 Jun 2020 at 08:24, Tobi DEGNON  wrote:
>
>> Make research about Django channels
>>
>> Le lun. 8 juin 2020 07 h 18, meera gangani  a
>> écrit :
>>
>>> I want to create personal chat in django!!
>>>
>>> What Can I Do?
>>> And What are the Libraries I install?
>>> Please Help Me Out!!
>>>
>>> Thanks in advance
>>> -Meera Gangani
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CANaPPPKWVMZhwmfTndhjVAmPB-cGYK07_ptdp%2BfdsymAe-b3%2BQ%40mail.gmail.com
>>> 
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CACeK5bn77jdGU%3Dp4gJsn4wzpdjQ72hdfBcC%3DNefCJqLNK0i7iw%40mail.gmail.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CACXuh-R7vp-GtAi1MJgg_pA5CiUtsrJXxxW-mH_BsUCMe4%3Dr2g%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV69uB2T_9C%3DMKGrhXqwSJfrN-P7shiFWKDt7i3gjPZB0zg%40mail.gmail.com.


Re: Required Python Django developers

2020-06-08 Thread Yamen Gamal Eldin
Interested

Le sam. 6 juin 2020 à 17:09, maninder singh Kumar <
maninder.s.ku...@gmail.com> a écrit :

> Dear all,
>
> We require python django developers for a HR project.  Please answer to
> maninder.s.ku...@gmail.com if interested.
>
> regards
> Willy
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CABOHK3R1pdwbzavYP48KA_g_pLm_5kkqNSxwhGdcRf4mfetw6w%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV6-g_woHWY1S-A%2B0AQbTJ7yMEixKp112-d1J2%2BqhUT1sxQ%40mail.gmail.com.


Re: Comment system in class based view in django showing error

2020-06-07 Thread Yamen Gamal Eldin
in CBVs the request is an attribute of the main view class you are
inheriting from it.
so to fix your issue, change this :
- request.method == "POST"
- self.request.method == "POST"

another thing i've noticed in those lines:

post=Post.objects.all()
comments=Comment.objects.filter(*post=post*)

You are trying to filter with a foreign key of type post using a value with
a queryset type. That will mostly give you an error somewhere down the road.

On Sun, Jun 7, 2020 at 9:01 PM Ali Ahammad  wrote:

> the basic source files are here :
> https://pastebin.pl/view/a0a1fc38  and i am trying to do it in class
> based view for further upgrade. My main project is written in class based
> view and it will be easy to integrate for me.
>
> views.py codes are give below:
>
> from .models import Post,Comment
> from .forms import CommentForm
> from django.shortcuts import render, get_object_or_404
> from django.views.generic import ListView,DetailView
> from django.shortcuts import redirect,render, get_object_or_404
> from django.http import HttpResponseRedirect
>
> class PostList(ListView):
> model=Post
> template_name='home.html'
> context_object_name='post_list'
> queryset=Post.objects.all()
>
> class PostDetail(DetailView):
> model=Post
>
> def get_queryset(self):
> queryset = super(PostDetail, self).get_queryset()
> post=Post.objects.all()
> comments=Comment.objects.filter(post=post)
> return queryset
>
> def get_context_data(self, **kwargs):
> context = super(PostDetail, self).get_context_data(**kwargs)
> if request.method == "POST":
> comment_form = CommentForm(request.POST or None)
> if comment_form.is_valid():
> comment=comment_form.save(commit=False)
> comment.post=post
> comment.save()
> else:
> comment_form = CommentForm()
> context['post']=post.objects.all()
> context['comments']=comments.objects.all()
> context['comment_form']=comment_form()
> template_name='Post_detail.html'
> return render(request, template_name, context)
>
> when i execute the runserver it shows NameError at /post/1/ name
> 'request' is not defined
>
> the models.py is:
>
> from django.db import models
> from django.contrib.auth.models import User
>
> class Post(models.Model):
> title = models.CharField(max_length=200, unique=True)
> author = models.ForeignKey(User, on_delete= 
> models.CASCADE,related_name='blog_posts')
> content = models.TextField()
>
> def __str__(self):
> return self.title
>
> class Comment(models.Model):
> post = 
> models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
> name = models.CharField(max_length=80)
> body = models.TextField()
> reply=models.ForeignKey('Comment',on_delete=models.CASCADE,null=True)
>
>
> def __str__(self):
> return self.name
>
> the function base view was:
>
> def PostDetail(request,pk):
> post = get_object_or_404(Post, pk=pk)
> comments=Comment.objects.filter(post=post)
> if request.method == "POST":
> comment_form = CommentForm(request.POST or None)
> if comment_form.is_valid():
> comment=comment_form.save(commit=False)
> comment.post=post
> comment.save()
> else:
> comment_form = CommentForm()
> context={
> 'post':post,
> 'comments':comments,
> 'comment_form':comment_form,
> }
> return render(request, 'post_detail.html', context)
>
> that function base view was working fine but i am trying to convert it
> into class based view so that i can use custommixin and other mixin later
> on. how can i make it work?
>
> please let me know. thanx in advance
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/17ba1d3b-8ed2-470f-9004-213357b2d403o%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOwHV6_cN804zayhiGn-S5A4VMawgWddB8i0-F7PfprgWNaBnQ%40mail.gmail.com.


Re: How to over ride the get context data method in view

2020-05-13 Thread Yamen Gamal Eldin
I haven't seen any definition for user within ur function, so
Change this
post.is_liked = post.likes.filter(user=user).exists()

To this

post.is_liked = post.likes.filter(user=request.user).exists()


Le mer. 13 mai 2020 à 19:22, Ahmed Khairy  a
écrit :

> Hi There,
>
> I have rewritten the get_context_data method to be like this but I am
> getting name 'user' is not defined:
>
>
> class PostListView(ListView):
> model = Post
> template_name = "score.html"
> ordering = ['-date_posted']
> context_object_name = 'posts'
> paginate_by = 5
>
> def get_context_data(self, **kwargs):
> context_data = super(PostListView, self).get_context_data(**
> kwargs)
> for post in context_data['posts']:
> post.is_liked = post.likes.filter(user=user).exists() <-
> name 'user' is not defined
> return context
>
> I am also using this template just for testing till fix the issue
>
> Thank you Andreas with me
>
>
> On Wednesday, May 13, 2020 at 12:31:15 PM UTC-4, Andréas Kühne wrote:
>>
>> Hi again,
>>
>> Bare with me - this is just written in this email - so:
>>
>> 1. Your listview is paginated by 5, so it will only show 5 posts - that's
>> good for this exercise and how I would solve it.
>> 2. Rewrite your get_context_data method to iterate over the posts:
>> def get_context_data(self, **kwargs):
>> context_data = super(PostListView,
>> self).get_context_data(**kwargs)
>> for post in context_data['posts']:
>> post.is_liked = post.likes.filter(user=user).exists()
>> return context
>>
>> So now your page should show. In the page template:
>>
>> % extends "base.html"%} {% block content %} {% for post in posts %}
>> {{post.title}}
>> 
>>   {% csrf_token %} 
>> {% if is_like %}   > type="submit">Unlike
>>
>>   {% else %}
>>   Like
>>   {% endif %}{% endfor %} {% endblock content %}
>>
>> Something like this should give you the functionality you want - however
>> it's still a bit cumbersome.
>>
>> Regards,
>>
>> Andréas
>>
>>
>> Den ons 13 maj 2020 kl 15:24 skrev Ahmed Khairy :
>>
>>> Hi Andréas Kühne,
>>>
>>> Regarding the first error
>>>
>>> I have a list of posts in the listView and want to add the like button
>>> to each post so how should I fix it?
>>>
>>> Thank you
>>>
>>> On Wednesday, May 13, 2020 at 6:08:31 AM UTC-4, Andréas Kühne wrote:

 Hi,

 There are a couple of errors in your code:

 1. You are using a ListView and then trying to get an individual post?
 That is really strange
 2. In your get_context_data method - you first get the context data,
 update it and then you set it to a new dict, so everything is reset in the
 dict.
 3. you are trying to get the absolut url for your post, but aren't
 showing any post detail view.

 Regards,

 Andréas


 Den ons 13 maj 2020 kl 06:46 skrev Ahmed Khairy >>> >:

> I am currently trying to create a like button for my posts in Django
>
> I have reached to the part where I can add a like but I am facing
> difficulty writing the code related to the view which linking the
> PostListView if there is user who liked it or not. I am getting an error:
> page Error 404 although everything is correct
>
>
> this the views:
>
>
> class PostListView(ListView):
> model = Post
> template_name = "score.html"
> ordering = ['-date_posted']
> context_object_name = 'posts'
> queryset = Post.objects.filter(admin_approved=True)
> paginate_by = 5
>
> def get_context_data(self, **kwargs):
> post = get_object_or_404(Post, id=self.request.POST.get(
> 'post_id'))
> context = super(PostListView, self).get_context_data(**kwargs)
> context['posts'][0].likes.filter(id=self.request.user.id).
> exists()
> is_liked = False
> if post.likes.filter(id=self.request.user.id).exists():
> is_liked = True
> context = {'post': post,
>'is_like': is_liked,
>}
> return context
>
>
>
> def like_post(request):
> post = get_object_or_404(Post, id=request.POST.get('post_id'))
> is_liked = False
> if post.likes.filter(id=request.user.id).exists():
> posts.likes.remove(request.user)
> is_liked = False
>
> else:
> post.likes.add(request.user)
> is_liked = True
>
> return HttpResponseRedirect(post.get_absolute_url())
>
>
> This is the model
>
>
> class Post(models.Model):
> designer = models.ForeignKey(User, on_delete=models.CASCADE)
> title = models.CharField(max_length=100)
> date_posted = models.DateTimeField(default=timezone.now)
> likes = models.ManyToManyField(User, blank=True, related_name='likes')
>
> def