Home > Back-end >  How i can split a Email in Django
How i can split a Email in Django

Time:11-10

Good morning I've been sitting for 5 hours on what should be a simple problem. I would be grateful if you could end my suffering. It is about a dashboard page. I want the user to be greeted with his name (Hello, ....) after he has registered with his email address. When registering, the email address is always the same, [email protected]. I want to split the email address and display only the name, but I can only manage to display the whole email address.

view.py

@login_required(login_url='login') 
def Example_dashboard(request):
    form = MembersForm()
    current_user = request.user #current_user.split is not working!  
  
    context = {'form': form, "cunrrent_user": current_user}
    return render(request, 'example_dashboard.html', context)

html

<p>Welcome, {{ current_user}}  </p>
<form action='' method='POST'>
    {%csrf_token%}
    {{ form }}

models.py

class Members(models.Model):
  email = models.CharField(max_length=200, null=True)
  passwort = models.CharField(max_length=200, null=True)

forms.py

class MembersForm(ModelForm):
    class Meta:
        model = Members

        fields = ["studiengang", "kategorien"]

CodePudding user response:

You can make this possible by using split method

After current_user = request.user, Try this:

current_user = current_user.split('@')[0]

CodePudding user response:

current_user = request.user #current_user.split is not working!

It will not work becouse request.user is a User object not a String you can't split user object instead you can do like this to split user's email.

@login_required(login_url='login') 
def Example_dashboard(request):
    form = MembersForm()
    current_user = request.user
    name = current_user.email.split('@')[0] # or .split('[email protected]')[0]
    context = {'form': form, "cunrrent_user": current_user}
    return render(request, 'example_dashboard.html', context)

CodePudding user response:

Try this solution. Because of request.user is “None”. That's why you can't split an email.

current_user = request.POST.get(‘email’) user_name = current_user.split('@')[0]

  • Related