Passing request (user) to a class based view
<p>As someone who is a bit new to class based views, I have decided to use them to drive some charts in an application I am working on.</p>

<p>However, I would like to make this chart dynamic and would like it to change based on who is seeing it. </p>

<p>How can one pass a request (to get the user from) to a class based view?</p>

<p>Below is my non-working implementation (working with dummy data but no request being passed):</p>

<p><strong>View:</strong></p>

<pre><code>class LineChartJSONView(BaseLineChartView, request):

    user = request.user

    def get_labels(self):
        labels = []
        items = Item.objects.filter(user = user)
        for i in items:
            labels.add(i.name)
        return labels

    def get_data(self):
        prices = []
        items = Item.objects.filter(user = user)
        for i in items:
            prices.add(i.price)
        return prices

line_chart = TemplateView.as_view(template_name='dashboard/test_chart.html')

line_chart_json = LineChartJSONView.as_view()
</code></pre>

<p><strong>URL:</strong></p>

<pre><code>url(r'^chart_data/$', LineChartJSONView.as_view(), name='line_chart_json'),  
url(r'^chart/$', views.ViewBaseChart, name='basic_chart'),
</code></pre>

<p><strong>HTML:</strong></p>

<pre><code>{% load staticfiles %}
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;test chart&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;canvas id = "myChart" width="500" height="200"&gt;&lt;/canvas&gt;
        &lt;!-- jQuery 2.2.3 --&gt;
        &lt;script src="{% static 'plugins/jQuery/jquery-2.2.3.min.js' %}"&gt;&lt;/script&gt;
        &lt;!-- Bootstrap 3.3.6 --&gt;
        &lt;script src="{% static 'bootstrap/js/bootstrap.min.js' %}"&gt;&lt;/script&gt;
        &lt;!-- ChartJS 1.0.1 --&gt;
        &lt;script src="{% static 'plugins/chartjs/Chart.min.js' %}"&gt;&lt;/script&gt;
        &lt;!-- FastClick --&gt;
        &lt;script src="{% static 'plugins/fastclick/fastclick.js' %}"&gt;&lt;/script&gt;
        &lt;!-- AdminLTE App --&gt;
        &lt;script src="{% static 'dist/js/app.min.js' %}"&gt;&lt;/script&gt;
        &lt;!-- AdminLTE for demo purposes --&gt;
        &lt;script src="{% static 'dist/js/demo.js' %}"&gt;&lt;/script&gt;
        &lt;!-- page script --&gt;
        &lt;script type="text/javascript"&gt;
            $.get('{% url "line_chart_json" %}', function(data)
            {
                var ctx =
                $("#myChart").get(0).getContext("2d");
                new Chart(ctx).Line(data);
            });
        &lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><strong>View (for static above - non classbasedview):</strong></p>

<pre><code>def ViewBaseChart(request):

    context = {}
    template = "dashboard/test_chart.html"

    return render(request,template,context) 
</code></pre>

<p>I am not sure I am using the class based view concept correctly here however, have found this to be the only way to implement charts thus far.</p>

<p>Your class definition is incorrect. A CBV shouldn't inherit from HttpRequest (and I am not even sure if that's what you mean by <code>request</code>)
. The correct definition is</p>

<pre><code>class LineChartJSONView(BaseLineChartView):
</code></pre>

<p>This assumes of course that <code>BaseLineChartView</code> has been defined correctly. The following line should also be removed since it defines a global user and secondly because there isn't a request object there!</p>

<pre><code>user = request.user
</code></pre>

<p>Now to get hold of a user instance, you need to override the get, post methods.</p>

<pre><code>def get(self, request):
    user = request.user
</code></pre>

python django django-views django-class-based-views