Hello I am new to django

2017-03-04 Thread sarfaraz ahmed
I am trying to create custom user authentication using abstractbaseuser.
All worked fine. but I wanted to move first_name and last_name to other
models


from django.db import models
from django.contrib.auth.models import
AbstractBaseUser,BaseUserManager,PermissionsMixin
from django.core.mail import send_mail
from django.utils.translation import ugettext_lazy as _
#now=time.strftime('%Y-%M-%D %H:%m:%S.%u%Z')
import datetime
from datetime import timedelta

from django.utils import timezone

tomorrow = datetime.date.today() + timedelta(days=1)

class CustomUserManager(BaseUserManager):
def _create_user(self,email,password,is_staff,is_superuser,
**extra_fields):

if not email:
raise ValueError('The given email must be set')

email=self.normalize_email(email)
user= self.model(email=email,
 is_staff=is_staff,
 is_active = True,
 is_superuser =is_superuser,
 last_login=timezone.now(),
 date_joined=timezone.now(),
**extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, email,password=None,**extra_fields):
return self._create_user(email,password,False,False,**extra_fields)

def create_superuser(self, email,password,**extra_fields):
return self._create_user(email,password,True,True,**extra_fields)

class CustomUser(AbstractBaseUser,PermissionsMixin):
username =models.CharField(max_length =256, unique = True,blank =
True,null= True)
email =models.EmailField(blank=False, unique =True)
date_joined  = models.DateTimeField(_('date joined'),
default=timezone.now())
is_active= models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)

USERNAME_FIELD ='email'
REQUIRED_FIELD =['user_name','date_joined']

objects=CustomUserManager()

class Meta:
verbose_name=_('user')
verbose_name_plural=_('users')

def get_absolute_url(self):
return "/user/%s" %urlquote(self.email)

def get_full_name(self):



*#a=UserProfile.objects.get(id=id)
#self.first_name=a.first_name#self.last_name= a.last_name*
full_name = '%s %s' %(self.first_name,self.last_name)
return full_name.strip()

def get_short_name(self):
self.first_name='a'
return self.first_name

def email_user(self,subject,message,from_email=None):
send_mail(subject,message,from_email,[self.email])


#code

class UserProfile(models.Model):

email = models.OneToOneField(CustomUser)
first_name=models.CharField(max_length =256, blank = True)
last_name=models.CharField(max_length =256, blank = True)
activation_key = models.CharField(max_length=40,blank=True)
gender = models.CharField(max_length=6, choices=(
('male', 'Male'),
('female', 'Female'),))
date_of_birth=models.DateField(null=True)
key_expires = models.DateTimeField(default=timezone.now())

def __str__(self):
full_name = '%s %s' %(self.first_name,self.last_name)
return full_name

class Meta:
verbose_name=u'User profile'
verbose_name_plural=u'User profiles'


class UserAddress(models.Model):
address_contact=models.CharField(max_length=300,blank=False)
address_line1=models.CharField(max_length=300,blank=False)
address_line2=models.CharField(max_length=300,blank=True)
land_mark=models.CharField(max_length=100,blank=False)
city=models.CharField(max_length=140,blank=False)
state=models.CharField(max_length=100,blank=False)
pin_code = models.BigIntegerField()
mobile_no=models.CharField(max_length=13,blank=True)
last_shipped_flag=models.BooleanField(default=False)
is_active_flag=models.BooleanField(default=True)
creation_date=models.DateTimeField(auto_now_add=True,editable=False)
updation_date=models.DateTimeField(auto_now=True,editable=False)
user=models.ForeignKey(UserProfile,default=0)

def __str__(self):
return self.address_contact


class Meta:
verbose_name=u'User Address'
verbose_name_plural=u'User Addresses'


Now get_full_name is part of customuser model where was first_name and
last_name are part UserProfile.
Please help me to join both models using onetoone relationship

-- 
Thanks with regards,
Sarfaraz Ahmed

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https

Re: Iframe'd page in Django template can't find it's Javascript or CSS files, 404 error

2017-03-04 Thread Daniel Roseman
On Saturday, 4 March 2017 15:57:49 UTC, Tom Tanner wrote:
>
> >How are you referring to the assets in the template?
>
> In `interactive/index.html`, in the `` tag, I have ` src="scripts/main.js">`
>

Well, if you wanted to load the scripts from 
/interactive-1/scripts/main.js, then that's what you should use as the src, 
surely?
-- 
DR.

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5fe5c543-8d35-4521-836b-163de1554a72%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Annotated queryset + Prefetch object

2017-03-04 Thread Jose Kilo
Hi all,

I'm trying to use an annotated queryset inside a Prefetch object. For some 
reason I'm not getting the expected result.
This is a simplified version of my models and query.


class User(models.Model):

following = models.ManyToManyField('User', related_name='followers', 
through='Follow')


class Follow(models.Model):

following = models.ForeignKey(User, on_delete=models.CASCADE, 
related_name='_followed')
followed = models.ForeignKey(User, on_delete=models.CASCADE, 
related_name='_following')


def test():

User.objects.all().delete()

user_1 = User.objects.create()
user_2 = User.objects.create()

Follow.objects.create(following=user_1, followed=user_2)

queryset = (
User.objects.all()
.prefetch_related(
Prefetch(
'followers', to_attr='prefetched_annotated_followers',
queryset=(User.objects.all().annotate(
followers_count=Count('followers', distinct=True),
))
),
Prefetch(
'followers', to_attr='prefetched_followers',
queryset=User.objects.all()
),
)
)

user = queryset.last()
print(list(user.followers.all()))# []
print(user.prefetched_followers) # []
print(user.prefetched_annotated_followers)   # []

return queryset


Why the last result is empty ? 

Just in case, I added 'prefetched_followers' to compare both results. If I 
remove it, 'prefetched_annotated_followers' still doesn't get anything.

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f81e26c5-38fa-4675-86b3-45e782175335%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Iframe'd page in Django template can't find it's Javascript or CSS files, 404 error

2017-03-04 Thread Tom Tanner
>How are you referring to the assets in the template?

In `interactive/index.html`, in the `` tag, I have ``

On Saturday, March 4, 2017 at 8:55:22 AM UTC-5, Daniel Roseman wrote:
>
> On Saturday, 4 March 2017 13:30:37 UTC, Tom Tanner wrote:
>>
>> In my `views.py`, I have one path render an html file:
>>
>> def interactive(request):
>> if request.session.get('interactiveID') == None:
>> t= get_template('blank-interactive.html')
>> else:
>> interactive_template= 'interactive-' + str(request.session['id']) + 
>> '/index.html'
>> t= get_template(interactive_template)
>> 
>> return HttpResponse(t.render())
>>
>> So `form.html` has an `` that requests `/interactive`, and 
>> `request.session['id']` is set to `1`. When the iframe requests 
>> `/interactive`, it loads `interactive-1/index.html`. The iframe loads the 
>> HTML from that page, but cannot get the JS file at 
>> `interactive-1/scripts/main.js`. 
>>
>> When I check the terminal, I see this 404 error: `GET 
>> /interactive/scripts/main.js`. If it would just load 
>> `/interactive-1/scripts/main.js`, there would be no problem. But Django 
>> loads `/interactive-1/index.html`, but not 
>> `/interactive-1/scripts/main.js`. Same goes for the CSS and image assets, 
>> which are in CSS and image folders.
>>
>> What do I need to change in Django for it to correctly load the JS, CSS, 
>> images and other assets?
>>
>
> There's not really enough information here to answer your question. How 
> are you referring to the assets in the template? Why do you have a 
> different path for those assets? Note though that template paths and asset 
> paths have nothing whatsoever to do with each other.
> -- 
> DR.
>

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7a788ad6-a6bb-4586-8a89-681ceacb857c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Iframe'd page in Django template can't find it's Javascript or CSS files, 404 error

2017-03-04 Thread Daniel Roseman
On Saturday, 4 March 2017 13:30:37 UTC, Tom Tanner wrote:
>
> In my `views.py`, I have one path render an html file:
>
> def interactive(request):
> if request.session.get('interactiveID') == None:
> t= get_template('blank-interactive.html')
> else:
> interactive_template= 'interactive-' + str(request.session['id']) + 
> '/index.html'
> t= get_template(interactive_template)
> 
> return HttpResponse(t.render())
>
> So `form.html` has an `` that requests `/interactive`, and 
> `request.session['id']` is set to `1`. When the iframe requests 
> `/interactive`, it loads `interactive-1/index.html`. The iframe loads the 
> HTML from that page, but cannot get the JS file at 
> `interactive-1/scripts/main.js`. 
>
> When I check the terminal, I see this 404 error: `GET 
> /interactive/scripts/main.js`. If it would just load 
> `/interactive-1/scripts/main.js`, there would be no problem. But Django 
> loads `/interactive-1/index.html`, but not 
> `/interactive-1/scripts/main.js`. Same goes for the CSS and image assets, 
> which are in CSS and image folders.
>
> What do I need to change in Django for it to correctly load the JS, CSS, 
> images and other assets?
>

There's not really enough information here to answer your question. How are 
you referring to the assets in the template? Why do you have a different 
path for those assets? Note though that template paths and asset paths have 
nothing whatsoever to do with each other.
-- 
DR.

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8176ee4a-9946-4c3a-80db-cab0d780e984%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Iframe'd page in Django template can't find it's Javascript or CSS files, 404 error

2017-03-04 Thread Tom Tanner
In my `views.py`, I have one path render an html file:

def interactive(request):
if request.session.get('interactiveID') == None:
t= get_template('blank-interactive.html')
else:
interactive_template= 'interactive-' + str(request.session['id']) + 
'/index.html'
t= get_template(interactive_template)

return HttpResponse(t.render())

So `form.html` has an `` that requests `/interactive`, and 
`request.session['id']` is set to `1`. When the iframe requests 
`/interactive`, it loads `interactive-1/index.html`. The iframe loads the 
HTML from that page, but cannot get the JS file at 
`interactive-1/scripts/main.js`. 

When I check the terminal, I see this 404 error: `GET 
/interactive/scripts/main.js`. If it would just load 
`/interactive-1/scripts/main.js`, there would be no problem. But Django 
loads `/interactive-1/index.html`, but not 
`/interactive-1/scripts/main.js`. Same goes for the CSS and image assets, 
which are in CSS and image folders.

What do I need to change in Django for it to correctly load the JS, CSS, 
images and other assets?

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d94bfa19-0250-4a90-93be-63e18889cb53%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.