在django中显示自定义用户的个人资料

时间:2016-04-16 09:33:09

标签: python django django-models django-forms

我正在为一个项目工作,我已经登录并注册了一个用户。现在我要显示用户的完整个人资料。如果get_profile不再工作,那么我怎样才能获得用户的完整个人资料

my models.py

class Consultants(models.Model):

    consul_id=models.IntegerField(default=0,primary_key=True)
    first_name=models.CharField(max_length=255,blank=True,null=True)
    last_name=models.CharField(max_length=255,blank=True,null=True)
    email=models.EmailField(max_length=255,blank=True,null=True)
    username=models.CharField(max_length=255,blank=True,null=True)
    password=models.CharField(max_length=50,blank=True,null=True)

    last_login=models.DateTimeField(default=datetime.now,blank=True,null=True)
    is_active=models.BooleanField(default=False)

    def __str__(self):
        return self.first_name

views.py用于登录和注册

def register(request):
    context = RequestContext(request)
    registered = False
    if request.method == 'POST':
        # user_form = UserForm(data=request.POST)
        consultant_form = ConsultantsForm(data=request.POST)
        if consultant_form.is_valid():
            consultant = consultant_form.save(commit=False)
            consultant.save()
            registered = True
        else:
            print consultant_form.errors
    else:
        consultant_form = ConsultantsForm()
    return render_to_response(
            'register.html',
            {'consultant_form': consultant_form, 'registered': registered},
            context_instance=RequestContext(request))

def login_user(request):
     context = RequestContext(request)
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        print type(username)
        print "username",username
        try:
            user = Consultants.objects.get(Q(username= username) & Q(password= password))
            print 'chala'
            if user.is_active:
                user.backend = 'django.contrib.auth.backends.ModelBackend'
                login(request, user)
                return HttpResponse("welcome......you are succesfuly log in")
            else:
                return HttpResponse("Your UkKonnect account is disabled.")
        except ObjectDoesNotExist:
            return HttpResponse("INvalid User")


    else:
         return render_to_response('login.html', {}, context)

我想制作def full_profile和def edit_profile。 如何登录用户consul_id? 请帮帮我

1 个答案:

答案 0 :(得分:0)

不确定我是否理解你的问题。看一下扩展用户模型的推荐方法:

https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
      <Label text="JavaFX Simulation" BorderPane.alignment="CENTER">
         <font>
            <Font name="Calibri" size="15.0" />
         </font>
      </Label>
   </top>
   <bottom>
      <Label text="\@Copyright by XXX" BorderPane.alignment="CENTER" />
   </bottom>
   <left>
      <VBox prefHeight="345.0" prefWidth="192.0" spacing="10.0" BorderPane.alignment="CENTER">
         <children>
            <GridPane hgap="10.0" vgap="10.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <TextField promptText="xMin" fx:id="xMin" />
                  <TextField promptText="xMax" GridPane.columnIndex="1" fx:id="xMax" />
                  <TextField fx:id="yMin" promptText="yMin" GridPane.rowIndex="1" />
                  <TextField fx:id="yMax" promptText="yMax" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                  <TextField fx:id="width" promptText="width" GridPane.rowIndex="3" />
                  <TextField fx:id="height" promptText="height" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                  <TextField promptText="xTick" GridPane.rowIndex="2" fx:id="xTick" />
                  <TextField fx:id="yTick" promptText="yTick" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <HBox prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="4">
                     <children>
                        <Button mnemonicParsing="false" onAction="#applyButtonClicked" text="Apply" />
                     </children>
                  </HBox>
                  <Button mnemonicParsing="false" onAction="#startButtonClicked" text="Start" GridPane.rowIndex="5" />
                  <Button mnemonicParsing="false" text="Restart" GridPane.columnIndex="1" GridPane.rowIndex="5" />
               </children>
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
            </GridPane>
         </children>
         <BorderPane.margin>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </BorderPane.margin>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
      </VBox>
   </left>
   <center>
      <LineChart fx:id="axesTest" BorderPane.alignment="CENTER">
        <xAxis>
          <NumberAxis lowerBound="-10.0" side="BOTTOM" fx:id="xAxis" />
        </xAxis>
        <yAxis>
          <NumberAxis fx:id="yAxis" lowerBound="-10.0" side="LEFT" />
        </yAxis>
      </LineChart>
   </center>
</BorderPane>

然后你可以这样做:

from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    department = models.CharField(max_length=100)

在你的情况下,它将是:

u = User.objects.get(username='fsmith')
freds_department = u.employee.department
  1. 现在您可以使用标准身份验证表单和方法
  2. 您始终可以获得顾问数据:

    class Consultant(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        *your fields here*