Register new user with curl post request - django

I'm trying to implement registration with my Django REST API.
I know how to login or obtain a token, so that one could do
curl -X POST -d "username=user2&password=qwer" http://localhost:8000/signin > index.html
curl -X POST -d "username=user2&password=qwer" http://localhost:8000/api-token-auth/
But what I need now is to register a new user. I tried it this way:
curl --cookie cookie.txt http://localhost:8000/signup/ -H "Content-Type: application/json" -H "X-CSRFToken: TP5mW4dxjpVO6UOk3nG6Ugk8jqv2u8E2" -X POST -d '{"username":"user4","email":"ggg#gmail.com","password":"qwer"}' >index.html
But nothing happens except I get a sign up page in my index.html file. No new user is created in the database.
This is my HTML template of sign up form. It works perfectly.
What request should I pass to my site in order to register a new user?
{% extends 'base.html' %}
{% block title %}Sign up · Parsifal{% endblock %}
{% block javascript %}
<script>
$(function () {
$('#id_username').focus();
});
</script>
{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Sign up for Parsifal</h3>
</div>
<div class="panel-body">
<form action="/signup/" method="post">
{% csrf_token %}
<div class="panel-group{% if form.username.errors %} has-error{% endif %}">
<label class="control-label" for="{{ form.username.id_for_label }}">{{ form.username.label }}</label>
<input type="text" class="form-control" value="{{ form.username.value|default_if_none:'' }}" id="{{ form.username.id_for_label }}" name="{{ form.username.html_name }}" maxlength="{{ form.username.field.max_length }}">
<span class="help-block">(Usernames may contain <strong>alphanumeric</strong>, <strong>_</strong> and <strong>.</strong> characters)</span>
{% for error in form.username.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<div class="panel-group{% if form.email.errors %} has-error{% endif %}">
<label class="control-label" for="{{ form.email.id_for_label }}">{{ form.email.label }}</label>
<input type="email" class="form-control" value="{{ form.email.value|default_if_none:'' }}" id="{{ form.email.id_for_label }}" name="{{ form.email.html_name }}" maxlength="{{ form.email.field.max_length }}">
{% for error in form.email.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<div class="panel-group{% if form.password.errors %} has-error{% endif %}">
<label class="control-label" for="{{ form.password.id_for_label }}">{{ form.password.label }}</label>
<input type="password" class="form-control" value="{{ form.password.value|default_if_none:'' }}" id="{{ form.password.id_for_label }}" name="{{ form.password.html_name }}">
{% for error in form.password.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<div class="panel-group{% if form.confirm_password.errors %} has-error{% endif %}">
<label class="control-label" for="{{ form.confirm_password.id_for_label }}">{{ form.confirm_password.label }}</label>
<input type="password" class="form-control" value="{{ form.confirm_password.value|default_if_none:'' }}" id="{{ form.confirm_password.id_for_label }}" name="{{ form.confirm_password.html_name }}">
{% for error in form.confirm_password.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<button type="submit" class="btn btn-success">Create an account</button>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}

Related

Change signup field error message and field color

we are currently implementing a membership page. I hope the color of the field will be red if there is an error, but I don't know what to do. What should I add to my code so that it's possible?
<form method="post" class="post-form">
{% csrf_token %}
{% for field in form %}
<div class="form-group">
<input name="{{field.html_name }}" id="{{ field.id_for_lable }}" class="form-control" type="{{ field.field.widget.input_type }}" value="{{ field.value|default_if_none:'' }}"
placeholder="{{field.label}}">
{% for error in field.errors %}
<label class="control-label" for="{{ field.id_for_lable }}" style="color: #c13b2a;"><b>{{ error }}</b></label>
{% endfor %}
</div>
{% endfor %}
</div>
<div class="bbb" style="text-align: center">
<button type="submit">가입하기</button><br>
</div>
You can add stye if condition
<input ... {% if field.errors %} style={"color":"red"} {% endif %}>
or add a class if condition
<input ... class =".. {% if field.errors %} text-red {% endif %}">

django does not allow form inputs in non-english languages

I have an input in my form which I try to label in Russian, for example:
email = forms.CharField(label='ййй')
The problem occurs when the label contains only non-English characters, the label simply disappears.
Interestingly enough, when the label contains at least one English character, the label will appear, for example:
email = forms.CharField(label='йййa')
Works correctly.
The same problem occurs with attributes in the widget parameter
email = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'ййй'}))
This the template HTML code:
{% load widget_tweaks %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% if field.name == 'birth_date' %}
{% render_field field class="form-control date" id="date" name="date" %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
This is the output html code:
<h2>Sign up</h2>
<form method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='Q2EvMWMCPrV597OQV8aqJwtC4X1zuSQI9oVdjeZtgzQUYTAYSp5v22cz2zfOZgwn' />
<div class="form-group">
<input type="text" name="email" autofocus required class="form-control" id="id_email" />
</div>
<div class="form-group">
<label for="id_id_num">Id number:</label>
<input type="text" name="id_num" id="id_id_num" required class="form-control" maxlength="9" />
</div>
<div class="form-group">
<label for="id_password1">Password:</label>
<input type="password" name="password1" required class="form-control" id="id_password1" />
<small class="form-text text-muted"><ul><li>Your password can&#39;t be too similar to your other personal information.</li><li>Your password must contain at least 8 characters.</li><li>Your password can&#39;t be a commonly used password.</li><li>Your password can&#39;t be entirely numeric.</li></ul></small>
</div>
<div class="form-group">
<label for="id_password2">Password confirmation:</label>
<input type="password" name="password2" required class="form-control" id="id_password2" />
<small class="form-text text-muted">Enter the same password as before, for verification.</small>
</div>
<div class="form-group">
<label for="id_first_name">First name:</label>
<input type="text" name="first_name" id="id_first_name" required class="form-control" maxlength="30" />
</div>
<div class="form-group">
<label for="id_last_name">Last name:</label>
<input type="text" name="last_name" id="id_last_name" required class="form-control" maxlength="30" />
</div>
<div class="form-group">
<label for="id_birth_date">Birth date:</label>
<input type="text" name="birth_date" required id="date" name="date" class="form-control date" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Found a solution, simply add "u" before the string to make it unicode
email = forms.CharField(label=u'ййй')

Django: Redirect Change Password URL

I have the following two URLs:
url(r'^change-password/$',django.contrib.auth.views.password_change,{'template_name': 'meta/changepassword.html', 'post_change_redirect': '/password-changed/'},name='change_password'),
url(r'^change-passwordiOS/$',django.contrib.auth.views.password_change,{'template_name': 'meta/changepassword.html', 'post_change_redirect': '/password-changed/'},name='change_passwordiOS'),
I thought if I used the following within the change password form it would override what URL would be loaded:
{% if 'iOS' in request.path %}
<input type="hidden" name="next" value="/profileiOS/" />
{% endif %}
But when I reach the change password from the url(r'^change-passwordiOS/$' and click the "Change Button" it does not goto the /profileiOS/ as expected but the standard /profile/ URL.
Any help would be appreciated.
/change-password/ view:
#login_required
def password_changed(request):
messages.success(request, 'Your password has been changed.')
return redirect(reverse('profile'))
Full Change Password Form:
<form class="form-horizontal" role="form" method="post" action="">
{% csrf_token %}
<fieldset>
<div class="form-group">
<label class="col-md-6 control-label">{{ form.old_password.label }}:</label>
<div class="col-md-6">
<input name="old_password" type="password" class="form-control"/>
<div class="text-danger">
{% for error in form.old_password.errors %}{{ error }}<br/>{% endfor %}
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-6 control-label">{{ form.new_password1.label }}:</label>
<div class="col-md-6">
<input name="new_password1" type="password" class="form-control"/>
<div class="text-danger">
{% for error in form.new_password1.errors %}{{ error }}<br/>{% endfor %}
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-6 control-label">{{ form.new_password2.label }}:</label>
<div class="col-md-6">
<input name="new_password2" type="password" class="form-control"/>
<div class="text-danger">
{% for error in form.new_password2.errors %}{{ error }}<br/>{% endfor %}
</div>
</div>
</div>
<div class="form-group">
<div class="text-right col-sm-12">
<button type="submit" class="btn btn-primary">Change Password^</button>
</div>
</div>
</fieldset>
{% if 'iOS' in request.path %}
<input type="hidden" name="next" value="/profileiOS/" />
{% endif %}
</form>
You are manually redirecting to /profile/ in password_changed view. You can change your redirect logic to depend on post param from template:
#login_required
def password_changed(request):
messages.success(request, 'Your password has been changed.')
return redirect(request.POST.get('next', reverse('profile')))

Django custom form template and help text

I'm trying iterate over form fields, and not want to use default {{ field }} tag. I want customise each field in cycle.
{% for field in wizard.form %}
<div class="row">
<div class="small-8 columns">
<label for="id_{{ field.html_name }}"class="inline{% if field.errors %}error {% endif %}">
{{ field.label }}
</label>
</div>
<div class="small-4 columns">
{{ field|add_error_class:"error" }}
{% if field.errors %}
<small class="error">{{ field.errors.as_text }}</small>
{% endif %}
</div>
</div>
{% endfor %}
I want to use something instead
{{ field|add_error_class:"error" }}.
Renders to:
<input class="timepicker" id="id_1-begin_time" name="1-begin_time" type="text" value="01:30:00" />
I want:
<input class="**{{ field.class }}**" id="id_{{ field.html_name }}" name="{{ field.html_name }}" type="**{{ field.type }}**" value="{{ field.value }}" />
Create tag:
from django import template
register = template.Library()
#register.filter(name='add_class')
def add_class(field, args):
return field.as_widget(attrs={"class": args})
Or in form define. Answer is here

Django AllAuth How do you Customize your own HTML or CSS

{% extends "account/base.html" %}
{% load url from future %}
{% load i18n %}
{% block head_title %}{% trans "Signup" %}{% endblock %}
{% block content %}
</style>
<h1><b>Free Membership</b>Sign up Today</h1>
<h1>{% trans "Sign Up" %}</h1>
<p>{% blocktrans %}Already have an account? Then please sign in.{% endblocktrans %}</p>
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
{{ form.as_p }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<button type="submit">{% trans "Sign Up" %} »</button>
</form>
<h1><b>Free Membership</b>Sign up Today</h1>
{% endblock %}
{% block content2 %}
{% endblock %}
This is the original Code from Django All Auth.
What I want to do is bring it more to life like add some better HTML5 or CSS to it when I do it does not change?
Here's my login.html template (I'm using Bootstrap, Jquery and Font-Awesome for the icons).
{% extends "account/base.html" %} {% load i18n custom_tags account %} {% block head_title %}{% trans "Sign In" %}{% endblock %} {% block common_scripts %}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
// Load jquery straight from the cdn for this page.
$(function() {
// We need some mappings for Providers names from AllAuth to the icon class names.
$('.btn-google').addClass('btn-google-plus');
$('.btn-linkedin_oauth2').addClass('btn-linkedin');
$('.fa-linkedin_oauth2').addClass('fa-linkedin');
$('#id_login').addClass('form-control').removeAttr('autofocus').blur();
$('#id_password').addClass('form-control');
});
</script>
{% endblock %} {% block content %}
<div class="container col-md-12">
<div class="well login-well">
<div class="socialaccount_ballot">
<ul class="socialaccount_providers">
{% load socialaccount %} {% for provider in socialaccount.providers %} {% if provider.id == "openid" %} {% for brand in provider.get_brands %}
<li>
<a title="{{ brand.name }}" class="btn btn-block btn-social btn-md btn-{{ provider.id }} socialaccount_provider {{ brand.id }}" href="{% provider_login_url provider.id openid=brand.openid_url process=" login " %}">
<i class="fa fa-{{ provider.id }}"></i>Log in with {{ brand.name }}
</a>
</li>
{% endfor %} {% endif %}
<li>
<a title="{{ provider.name }}" class="btn btn-block btn-social btn-md socialaccount_provider btn-{{ provider.id }}" href="{% provider_login_url provider.id process=" login " %}">
<i class="fa fa-{{ provider.id }}"></i>Log in with {{ provider.name }}
</a>
</li>
{% endfor %}
</ul>
<hr>
<form class="django-login" method="POST" action="{% url 'login' %}">
{% csrf_token %} {% if form.non_field_errors %}
<div class="alert alert-warning">
<ul class="alert-message">
{% for error in form.non_field_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<div class="input-group {% if form.login.errors %}has-error{% endif %}">
<span class="input-group-addon glyphicon glyphicon-envelope"></span>
{{ form.login }}
</div>
<div class="input-group {% if form.password.errors %}has-error{% endif %}">
<span class="input-group-addon glyphicon glyphicon-lock"></span>
{{ form.password }}
</div>
<div class="fieldWrapper form-inline remember text-center">
{{ form.remember }}
<label for="id_remember" class="text-muted">Remember me</label>
</div>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />{% endif %}
<div class="btn-div pull-center">
<button class="btn btn-primary btn-block" type="submit">{% trans "Sign In" %}</button>
</div>
</form>
</div>
<hr>
<div>
<small><a class="text-muted" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a></small>
<br>
<small><a class="text-muted" href="{% url 'signup' %}">Sign up</a></small>
</div>
</div>
</div>
{% endblock %}
And how it looks:
You can put custom templates for allauth in your template directory under account folder. Django allauth would take the custom template from it and renders it.
Following is sample signup page I have created some days ago:
{% extends "base.html" %}
{% load staticfiles %}
{% load socialaccount %}
{% block extra_body %}account-bg{% endblock %}
{% block partial %}
<div class="container">
<div class="row">
<div class="account-form-container">
<div class="row">
<div class="col-md-12">
<h3>Create New Account</h3>
</div>
</div>
<div class="row">
<form class="form-horizontal signup-form" id="userSignupForm" action="{% url "account_signup" %}" method="post" role="form">
{% csrf_token %}
{% if request.GET.next %}
<input type="hidden" name="next" value="{{request.GET.next}}">
{% endif %}
{% if form.non_field_errors %}
<div class="fieldWrapper alert alert-danger">
<ul>
{% for error in form.non_field_errors %}
<li>{{ error | lower}}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<div class="col-lg-12">
<div class="form-group inner-addon left-addon {% if form.email.errors %}has-error{% endif %}">
<i class="icon-user"></i>
<input class="form-control login-field" type="text" id="id_email" name="email" placeholder="Email">
<span class="help-block">{{ form.email.errors }}</span>
</div>
<div class="form-group inner-addon left-addon {% if form.password1.errors %}has-error{% endif %}">
<i class="icon-lock"></i>
<input class="form-control login-field" type="password" id="id_password1" name="password1" placeholder="Password">
<span class="help-block">{{ form.password1.errors }}</span>
</div>
<div class="form-group inner-addon left-addon {% if form.password1.errors %}has-error{% endif %}">
<i class="icon-lock"></i>
<input class="form-control login-field" type="password" id="id_password2" name="password2" placeholder="Confirm Password">
<span class="help-block">{{ form.password1.errors }}</span>
</div>
<div class="form-group">
<div class="col-md-5">
<button type="submit" class="btn btn-complete btn-bold pull-right">Sign Up</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
I hope this would help you.