0

this is a very basic and quick question in Django.

So in my views, where I am handling the registration, if a user tries to register with a username that already exists I want to just give a message, like a span next to the form, saying, "wrong username".

How do I print that message?

Thank you.

This is my part of code:

def register(request): if request.method == 'POST': if User.objects.filter(username = request.POST['username']).exists(): #I want to show next to the username textfield in registration form, #a span for example that says "Wrong Username" 
2
  • 1
    The best way to handle this is in the form's clean method - where you can raise a validation error on the specific field Commented Jan 9, 2015 at 16:22
  • @karthikr Post an example confirming the statement and post is as an answer. Commented Jan 9, 2015 at 16:34

4 Answers 4

1

As suggested in a comment above, you should do it in form's clean method which would make things easier. But if you must do it in a view, here's an example:

if request.method == 'POST': try: user_exists = User.objects.get(username=request.POST['username']) return HttpResponse("Username already taken") except User.DoesNotExist: # Username doesn't exist # Do other validation ... 

Then show the response in a span using jQuery, or something else.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the answer. That's my actual question. How do I transform the HttpResponse to a span? :D thanks
@NicolasNicolas Are you doing it with AJAX?
Ideally I would want just to show a span in the page that is already loaded. Not re-render a page.
Well, then you need to use AJAX. This has nothing to do with Django. You need to look into jQuery for that.
Thanks for your answer again. I am pretty sure that this is relevant to Django. The solution I had in mind was something like, in the template of the page I am, I put a Django if statement, and inside that I put the span that I want to show IF the username already exists. But I just don't know how to connect these. thanks again
|
0

To get HTTPResponse message, you have to use ajax instead form submit.

Here i am assuming you are using following like html page code.

<input type="text" name="username" id="username"/> <p id="error"></p> <input type="button" value="Register" id="register"> 

Now, You have to make an ajax call on button's click event like:

(i assume that you have knowledge of ajax call.)

$("#register").on('click', function(){ var username=$("#username").val(); $.ajax({ . . . success: fuinction(data){ if(data=="error occured"){ $("error").append("wrong username"); } } }) }); 

and views.py code is like this:

def register(request): if request.method == 'POST': if User.objects.filter(username = request.POST['username']).exists(): return HTTPResponse("error occured") 

Comments

0

I want to modify an answer from above which can be more easier.... i think :) haha:

def register(request): alert = { "username": request.GET.get('username', ''), } if request.method == 'POST': username = request.POST.get('username', '') if User.objects.filter(username = request.POST['username']).exists(): alert['username'] = "Username already exists" return render(request, 'interface/signup.html', alert) 

and in your html: lets just say -->

<form method="POST"> {% csrf_token %} <h3>Sign Up</h3> <label class="label">Username:</label><input type="text" name="username" class="form-control" /><br> {% if username %} <div class="alert alert-danger alert-dismissible"> <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a> {{ username }} </div> <br> {% endif %} </form> 

Comments

0
def register(request): form = UserCreationForm(request.POST) if form.is_valid(): else: # Form is invalid, this includes the user already existing 

I know it's 2 years late but none of the answers had it and django has a built in way of solving it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.