im a newbe, i'm trying to get the username in a view

2020-11-13 Thread Apolo Machine
I'm trying to get the username within a view(CBV), the documentation I 
found says that the logged user is within the request, but when I do 

class ServiceCreateView(LoginRequiredMixin, CreateView):
  model = Service
  form_class = ServiceForm
  user=request.user.username 

i get this error 

AttributeError: module 'django.http.request' has no attribute 'user

any help is appreciated

-- 
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/8635848c-b7fc-4b6b-9b53-f6107ce42717n%40googlegroups.com.


Re: im a newbe, i'm trying to get the username in a view

2020-11-13 Thread Apolo Machine
where? in my model service?
i'm not quiet understand
thanks for responding

El vie., 13 nov. 2020 a las 18:53, Barbara Leonard (<
barbaraleona...@gmail.com>) escribió:

> make an attribute named "user"
>
> On Fri, Nov 13, 2020 at 2:27 PM Apolo Machine 
> wrote:
>
>> I'm trying to get the username within a view(CBV), the documentation I
>> found says that the logged user is within the request, but when I do
>>
>> class ServiceCreateView(LoginRequiredMixin, CreateView):
>>   model = Service
>>   form_class = ServiceForm
>>   user=request.user.username
>>
>> i get this error
>>
>> AttributeError: module 'django.http.request' has no attribute 'user
>>
>> any help is appreciated
>>
>> --
>> 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/8635848c-b7fc-4b6b-9b53-f6107ce42717n%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/8635848c-b7fc-4b6b-9b53-f6107ce42717n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/jW90KnHwaw0/unsubscribe.
> To unsubscribe from this group and all its topics, 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/CAAcuwDfBL82t2WebGvYmUEiP%2BCD7tv3aBTL7f%2BrH0igzVUbSuQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAAcuwDfBL82t2WebGvYmUEiP%2BCD7tv3aBTL7f%2BrH0igzVUbSuQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 
::Apolo::

-- 
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/CALOuZTBP0S3fVSuuqw6v8sc2A67115iwNO_QKh_Ubw265g35DQ%40mail.gmail.com.


Re: im a newbe, i'm trying to get the username in a view

2020-11-15 Thread Apolo Machine
Thanks to all of you.
I finally get it work with this
def get_initial(self):
initial = super(ServiceCreateView, self).get_initial()
initial = initial.copy()
initial['user'] = self.request.user.username
return initial

El sáb., 14 nov. 2020 a las 19:59, David Nugent ()
escribió:

> The pre-requisite for having ‘user’ in the request instance is the
> ‘django.contrib.auth.context_processors.auth’ in your TEMPLATES
> context_processors.
>
> See the second answer (should be the accepted one as it has received far
> more community votes) in this stack overflow article
> .
>
> Note that the settings referred to in that article are obsolete and have
> been replaced as per above. You can also find a link to the documentation
> in the article.
>
> Also, you don’t need to explicitly get the username from the request
> object like this. Assuming you have all the required context processors
> present you can simply use {{ request.user.username }} within your
> template.
>
> In any case, the best practice with Django template CBVs is not to
> override get() here, but override get_context_data()
> 
>  where
> the request object is available as self.request.
>
> /d
>
> On 20201115, at 01:55, mike vickers  wrote:
>
> You would want to include that line in a method, not as a class attribute.
> For example:
>
> def get(self, request):
> user = request.user.username
> ...
> return render(...
>
>
> On Sat, Nov 14, 2020 at 9:37 AM Kasper Laudrup 
> wrote:
>
>> Hi Barbara,
>>
>> On 13/11/2020 22.46, Barbara Leonard wrote:
>> > make an attribute named "user"
>> >
>>
>> How would that help? The attribute value should be set to the currently
>> logged in user. Just adding the attribute to request object will not do
>> that. Clearly something else is missing.
>>
>> Kind regards,
>>
>> Kasper Laudrup
>>
>> --
>> 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/93f48576-7bf0-b46d-9403-5f0c23f3babc%40stacktrace.dk
>> .
>>
>
> --
> 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/CACaDatT8mUT142AYWuw6wemqLMzbKjCvZyT%3DAqi-_WoZrYsn9Q%40mail.gmail.com
> 
> .
>
>
>

-- 
::Apolo::

-- 
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/CALOuZTD42NYQ6%3DsbV%3D0iYxT9eD5Afot65xZBJAmJs3UMSQWBzA%40mail.gmail.com.


Select from the dropdown the object car that has the field is_active

2020-11-15 Thread Apolo Machine

hello, i'm a newbie using django and i have a problem that cannot solve.
In my createview i need to select from the dropdown the object car that has 
the field is_active = true, 
how can i acomplish that?

class Car(models.Model):
brand = models.charfield(...)
is_active= models.BooleanField(default=True)

class Service(models.Model):
car = models.ForeingKey('Car')
name = models.Charfield()

class ServiceCreateView(CreateView):
model = Service
form = ServiceForm
...

If i change the field is_active to false in Car model, should not be shown 
in the dropdown.
can someone put me in the right direction?

-- 
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/c0f25f26-0d48-4272-8604-e688fe796370n%40googlegroups.com.


Re: Select from the dropdown the object car that has the field is_active

2020-11-15 Thread Apolo Machine
Thanks a lot! that totally makes sense and works!

El dom., 15 nov. 2020 a las 11:07, David Nugent ()
escribió:

> On 15 Nov 2020, at 23:27, Apolo Machine  wrote:
>
>
> hello, i'm a newbie using django and i have a problem that cannot solve.
> In my createview i need to select from the dropdown the object car that
> has the field is_active = true,
> how can i acomplish that?
>
> class Car(models.Model):
> brand = models.charfield(...)
> is_active= models.BooleanField(default=True)
>
> class Service(models.Model):
> car = models.ForeingKey('Car')
> name = models.Charfield()
>
> class ServiceCreateView(CreateView):
> model = Service
> form = ServiceForm
> ...
>
> If i change the field is_active to false in Car model, should not be shown
> in the dropdown.
> can someone put me in the right direction?
>
>
>
> You didn't include source for ServiceForm - the work to do this will be
> there.
>
> Assuming it is a ModelForm:
>
> from django import forms
> from .models import Service, Car
>
>
> class ServiceForm(forms.ModelForm):
>
> def __init__(self, *args, **kwargs):
> super().__init__(*args, **kwargs)
> self.fields['car'].queryset = Car.objects.filter(is_active=True)
>
> class Meta:
> model = Service
> fields = ('car', 'name')
>
>
>

-- 
::Apolo::

-- 
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/CALOuZTAa%2Bcm7sQ%3D6wZvw5ymG7yhHKh3DOiOaV93BSqHWz7pR4Q%40mail.gmail.com.


Django form inside a modal (bootstrap)

2020-11-17 Thread Apolo Machine

hello, i'm trying to add a button that open a modal(bootstrap) where i can 
add a new object car, but when  a press the buttom open the service form in 
the modal
i have a working form where i can add new car (name=create-car), how can i 
open the CarForm inside of the modal?

class Car(models.Model):
brand = models.charfield(...)
is_active= models.BooleanField(default=True)

class Service(models.Model):
car = models.ForeingKey('Car')
name = models.Charfield()

class ServiceCreateView(CreateView):
model = Service
form = ServiceForm
...

Service_form.html
{% extends 'generic_base.html' %}
{% load crispy_forms_tags %}


$(document).ready(function() {
$("#exampleModal").modalForm({
formURL: "{% url 'create-car' %}"
});
});

{%block content %}


{% csrf_token %}


{{ form.car|as_crispy_field }}


Add New 
Car








  

  Car
  
×
  


  
{% csrf_token %}
{{ form|crispy}}
 



  Close
  Save

  

  

{% endblock %}



-- 
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/3702f309-91c1-4275-b69a-ed6cd18f7f82n%40googlegroups.com.