D
D
Dmitry Vyatkin2016-02-10 22:03:57
Django
Dmitry Vyatkin, 2016-02-10 22:03:57

How to pass and receive data of different users between functions in django?

Good afternoon! It is necessary to display the same page, but for different users, let's say a user profile. How to pass this user between view functions and templates. Now I have to write (?P\w{3,30}) in each url and make queries depending on the user in the views, then pass the same user to the template and so from template to template from function to function, the code is often repeated and I feel that this is not the correct solution. What are the correct practices for displaying information depending on the user?

views.py

class Test(CreateView):
    template_name = 'test/test.html'
    form_class = TestForm
    success_url = '/'

    def get_context_data(self, **kwargs):
        context = super(Test, self).get_context_data(**kwargs)
        context['username'] = self.kwargs['username']

        return context

urls.py

url(r'^(?P\w{3,30})/$' , Test.as_view() , name='test')

test.html

<a href="{% url 'test' username %}">test</a>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-02-10
@sim3x

current authorized user in request.user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question