Re: Form to login

2015-11-04 Thread victor menezes
Looks like you are not making anything very custom on you view so maybe 
would be easier/better to use the buitin views from django auth.

on your urls.py you can do:

from django.contrib.auth import views as auth_views

urlpatterns = [
...
url(r'^login/', auth_views.login),
]

On your settings.py:
LOGIN_URL = '/login' #the global address of the login page(loguin_required 
decorators, etc will redirect to here)
NEXT_PAGE = '/home' #page the user is redirected after login


On Wednesday, November 4, 2015 at 12:51:02 AM UTC-5, Dariusz Mysior wrote:
>
> I try do login view and I find it on 
>
> https://docs.djangoproject.com/en/1.8/topics/auth/default/
>
> from django.contrib.auth import authenticate, login
>
> def my_view(request):
> username = request.POST['username']
> password = request.POST['password']
> user = authenticate(username=username, password=password)
> if user is not None:
> if user.is_active:
> login(request, user)
> # Redirect to a success page.
> else:
> # Return a 'disabled account' error message
> ...
> else:
> # Return an 'invalid login' error message.
> ...
>
>
>
> to give request.POST username and password and username I must create my 
> own form or maybe Django have that form ready? I use Django 1.8
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/91465324-db83-4850-8460-b54ab8ef4542%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Login with email address

2015-11-04 Thread victor menezes
I agree that it is time for django to change it (at least making the 
changes easier).

On my project I did create a new user model that inherit from AbstractUser
class UserManager(BaseUserManager): ...
class User(AbstractBaseUser): email = models.EmailField(unique=True) 
objects = UserManager() USERNAME_FIELD = 'email'
I didn't have an password field because I use a third party(google identity 
toolkit to authenticate) but you can have one.
Here is the full models if you want a sample 

.


On Wednesday, November 4, 2015 at 3:36:57 AM UTC-5, Patrick Breitenbach 
wrote:
>
> Is there a current "best" approach or module for signup/login with an 
> email address?
>
> I can't believe this is not included with django. In fact, it should 
> be the default. 80-99% of services and projects do not use usernames.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/cc19c134-607d-4f11-af53-cb3f66a2bc40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


ModelForm not creating field with required options

2015-04-24 Thread victor menezes
Hi all,

I'm using Django 1.8 and having trouble to because my form does not 
generate fields with required option even though they have the property 
"blank=False" in the model.
In the example bellow, shouldn't the field "description" have the 
"required" attribute in the html generated using "form.as_p" or am I 
missing anything?

*models.py*
class Place(models.Model):
name = models.CharField(max_length=200, blank=False, default='')
owners = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=False)
members = models.ManyToManyField(Member, blank=True)
*description =  models.TextField(blank=False)*
location = models.CharField(max_length=400, blank=True)
additional_info = models.TextField(blank=True)

def __unicode__(self):
return self.name


class PlaceForm(ModelForm):
class Meta:
model = Place
fields = '__all__'

*views.py*
@login_required(login_url='/')
def addPlaceView(request):
place_form = PlaceForm()
context = {
'form': place_form,
}
return render(request, 'newPlace.html', context)

*newPlace.html*

{% csrf_token %}
{{ form.as_p }}



-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/84a2cdf8-f6de-438b-b7d3-775c4300f105%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Model with two e-mail fields uniques

2015-04-08 Thread victor menezes
I liked the idea at first but after couple tests realized that the it will 
alway raise the error on save().

The problem is that I can't add any Email without Person, this looks great 
but, I also can't add a Person without an Email and so it will never allow 
me to add anything.

Maybe I should keep it as ForeignKey but ensure that the Email is filled 
just in the form validation, not on database/class level? Following a test 
that i did:

>>> p = Person()

>>> p.first_name = 'aaa'
>>> p.last_name = 'bbb'

>>> p.emails.all()
[]
>>> p.emails.count()
0
>>> p.emails.create(email='a...@aaa.aaa')
Traceback (most recent call last):
  File "", line 1, in 
...
(value, self.field.rel.to._meta.object_name)
ValueError: Cannot assign "": "Member" instance isn't 
saved in the database.
>>> p.save()
Traceback (most recent call last):
  File "", line 1, in 
  ...
raise ValueError('Need at least one e-mail.')
ValueError: Need at least one e-mail.


On Wednesday, April 8, 2015 at 11:38:28 AM UTC-4, Bruno A. wrote:

> +1 for Javier's answer, use a simple Foreign key on the e-mail field, not 
> a ManyToMany on the Person (M2M allows an email to belong to multiple 
> users). The Foreign key ensures an e-mail belongs to only 1 user, and that 
> 2 users cannot have the same e-mail, but lets a user have multiple.
>
> To force all users to have at least one e-mail, something like this would 
> do the job:
>
> class Person(models.Model):
> first_name = models.CharField(max_length=100)
> last_name = models.CharField(max_length=100)
>
> def save(self, *args, **kwargs):
> if self.emails.count() == 0:
> raise ValueError("Need at least one email.")
> return super(Person, self).save(*args, **kwargs)
>
>
> class Email(models.Model):
> person = models.ForeignKey(Person, related_name='emails')
> email = models.EmailField(unique=True)
>
>
>
> On Wednesday, 8 April 2015 04:02:50 UTC+1, victor menezes wrote:
>>
>> Would it be a bad approach to use a ManyToMany with properties null/blank 
>> False?
>>
>> class Email(models.Model):
>> email = models.EmailField(unique=True)
>> def __unicode__(self):
>> return self.email
>>
>> class Person(models.Model):
>> first_name = models.CharField(max_length=100)
>> last_name = models.CharField(max_length=100)
>> emails = models.ManyToManyField(Email, null=False, blank=False)
>> def __unicode__(self):
>> return self.last_name + ', ' + self.first_name
>>  
>>
>> On Tuesday, April 7, 2015 at 6:48:22 PM UTC-4, victor menezes wrote:
>>>
>>> Thanks for the help, I end up doing it was easy to implement the email 
>>> as TabularInline inside the Person form in the Admin but how would you make 
>>> mandatory to have at least one e-mail?
>>> Should I validate inside the save() method of Person class?
>>>
>>>
>>> On Tuesday, April 7, 2015 at 3:55:26 PM UTC-4, Javier Guerra wrote:
>>>>
>>>> On Tue, Apr 7, 2015 at 2:20 PM, victor menezes  
>>>> wrote: 
>>>> > What would be the best way to make a model with two e-mail fields 
>>>> uniques? I 
>>>> > was thinking about writing some validation in the save() method but 
>>>> would 
>>>> > like to know whether Django has some built-in way to deal with it. 
>>>>
>>>>
>>>> it's a very bad idea to have plurality by adding a number of similar 
>>>> fields.  instead, you should have a related table 
>>>>
>>>> class Person(models.Model): 
>>>> first_name = models.CharField(max_length=100) 
>>>> last_name = models.CharField(max_length=100) 
>>>>
>>>> class Email(models.Model): 
>>>> person = models.ForeignKey(Person) 
>>>> email = models.EmailField(unique=True) 
>>>>
>>>>
>>>> -- 
>>>> Javier 
>>>>
>>>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2a9c7893-c2de-45de-ace8-a44207f6f460%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Model with two e-mail fields uniques

2015-04-07 Thread victor menezes
Would it be a bad approach to use a ManyToMany with properties null/blank 
False?

class Email(models.Model):
email = models.EmailField(unique=True)
def __unicode__(self):
return self.email

class Person(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
emails = models.ManyToManyField(Email, null=False, blank=False)
def __unicode__(self):
return self.last_name + ', ' + self.first_name
 

On Tuesday, April 7, 2015 at 6:48:22 PM UTC-4, victor menezes wrote:
>
> Thanks for the help, I end up doing it was easy to implement the email as 
> TabularInline inside the Person form in the Admin but how would you make 
> mandatory to have at least one e-mail?
> Should I validate inside the save() method of Person class?
>
>
> On Tuesday, April 7, 2015 at 3:55:26 PM UTC-4, Javier Guerra wrote:
>>
>> On Tue, Apr 7, 2015 at 2:20 PM, victor menezes  
>> wrote: 
>> > What would be the best way to make a model with two e-mail fields 
>> uniques? I 
>> > was thinking about writing some validation in the save() method but 
>> would 
>> > like to know whether Django has some built-in way to deal with it. 
>>
>>
>> it's a very bad idea to have plurality by adding a number of similar 
>> fields.  instead, you should have a related table 
>>
>> class Person(models.Model): 
>> first_name = models.CharField(max_length=100) 
>> last_name = models.CharField(max_length=100) 
>>
>> class Email(models.Model): 
>> person = models.ForeignKey(Person) 
>> email = models.EmailField(unique=True) 
>>
>>
>> -- 
>> Javier 
>>
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b3064bc1-6a3c-4989-adbd-8d71da35db36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Model with two e-mail fields uniques

2015-04-07 Thread victor menezes
Thanks for the help, I end up doing it was easy to implement the email as 
TabularInline inside the Person form in the Admin but how would you make 
mandatory to have at least one e-mail?
Should I validate inside the save() method of Person class?


On Tuesday, April 7, 2015 at 3:55:26 PM UTC-4, Javier Guerra wrote:
>
> On Tue, Apr 7, 2015 at 2:20 PM, victor menezes  > wrote: 
> > What would be the best way to make a model with two e-mail fields 
> uniques? I 
> > was thinking about writing some validation in the save() method but 
> would 
> > like to know whether Django has some built-in way to deal with it. 
>
>
> it's a very bad idea to have plurality by adding a number of similar 
> fields.  instead, you should have a related table 
>
> class Person(models.Model): 
> first_name = models.CharField(max_length=100) 
> last_name = models.CharField(max_length=100) 
>
> class Email(models.Model): 
> person = models.ForeignKey(Person) 
> email = models.EmailField(unique=True) 
>
>
> -- 
> Javier 
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/14e85fe3-ff59-4568-82b3-aa40ae488b94%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Model with two e-mail fields uniques

2015-04-07 Thread victor menezes
Hi there,

What would be the best way to make a model with two e-mail fields uniques?
I was thinking about writing some validation in the save() method but would
like to know whether Django has some built-in way to deal with it.

class Person(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email1 = models.EmailField(null=True, blank=True)
email2 = models.EmailField(null=True, blank=True)
def __unicode__(self):
return self.last_name + ', ' + self.first_name
def save(self):
if (len(self.email1) == 0 and len(self.email2) == 0):
raise ValueError('At least one e-mail is needed to proceed.')
super(Member, self).save()


John = Person()
John.email1 = 'someEmail'
John.save()

Kane = Person()
Kane.email1 = 'anotherEmail
Kane.email2 = 'someEmail' #same email used by John on email1 field
Kane.save() #should raise error because email2 is already used by John on
email1 field

Thanks


Victor Menezes
"Geek by nature, Linux by choice"

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAAgFNMmFNGYNEQwdGxvW9x81S1RNb-Wm_LYuk3cZ1YD4rb7RLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.