Sunday, May 10, 2015

django admin “create user” and “change user” doesn't work

I'm writing a simple django contact form. I have defined my models.py and admin.py in the following manner :

models.py :

    from django.db import models
    from django.contrib.auth.models import User
    
    class Contact(models.Model):
    ACTIVE = 1
    PENDING = 2
    SUSPENDED = 3
    INACTIVE = 4
    STATUS_CHOICES = [
        (ACTIVE, 'Active'),
        (PENDING, 'Pending'),
        (SUSPENDED, 'Suspended'),
        (INACTIVE, 'Inactive'),
    ]
    status = models.IntegerField(choices=STATUS_CHOICES, default=ACTIVE)
    user = models.OneToOneField(User, null=True)
    name = models.CharField(max_length=200)
    title = models.CharField(max_length=200)
    company = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    tags = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    mobile = models.CharField(max_length=11, null=True)
    mailing_street = models.CharField(max_length=200, null=True)
    mailing_city = models.CharField(max_length=200, null=True)
    mailing_state = models.CharField(max_length=200, null=True)
    mailing_country = models.CharField(max_length=200, null=True)
    mailing_zipcode = models.CharField(max_length=200, null=True)

    def __str__(self):
        return self.name

admin.py :

    from django.contrib import admin
    from django.contrib.auth.admin import UserAdmin
    from django.contrib.auth.models import User
    from .models import Contact

    class ContactInline(admin.StackedInline):
        model = Contact

    class UserAdmin(UserAdmin):
        inlines = [ContactInline]

    admin.site.unregister(User)
    admin.site.register(User, UserAdmin)

    
The problem is that when I click on "Add User" in django admin, it shows me the user fields so I can create a user but when I click on "save", it shows me an error like this :

The webpage is not available, ERR_CONNECTION_RESET

I have deleted this section of the code and then I was able to save the user but I need this part.

    ACTIVE = 1
    PENDING = 2
    SUSPENDED = 3
    INACTIVE = 4
    STATUS_CHOICES = [
        (ACTIVE, 'Active'),
        (PENDING, 'Pending'),
        (SUSPENDED, 'Suspended'),
        (INACTIVE, 'Inactive'),
    ]
    status = models.IntegerField(choices=STATUS_CHOICES, default=ACTIVE)

Any idea how I can solve this problem?

--
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+unsubscribe@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/826fde18-7f6d-40f8-a0f5-0b04963a5e10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment