Django
0
Вклад в тег
url(r'^articles/$', views.articles, name='articles'),
url(r'^articles/(?P<category_slug>[-\w]+)/(?P<slug>[-\w]+)/$', views.articles_subcategory, name='articles_subcategory'),
url(r'^articles/(?P<slug>.+)/$', views.articles_category, name='articles_category'),
def articles_category(request, slug):
articles_cat = get_object_or_404(ArticlesCategory, slug=slug)
return render(request, 'Articles/articles_category.html', {'articles_cat': articles_cat})
def articles_subcategory(request, slug, *args, **kwargs):
articles_subcat = get_object_or_404(ArticlesSubCategory, slug=slug)
return render(request, 'Articles/articles_subcategory.html', {'articles_subcat': articles_subcat})
class ArticlesCategory(models.Model):
...
def get_absolute_url(self):
return reverse('articles_category', args=[str(self.slug)])
class ArticlesSubCategory(models.Model):
...
def get_absolute_url(self):
return reverse('articles_subcategory', args=[str(self.category.slug), str(self.slug)])
<a href="{% url 'articles_subcategory' category_slug=i.category.slug slug=i.slug %}">{{ i.name }}</a>
$('#login-form').on('submit', function(event){
event.preventDefault();
var csrf_token = $('#login-form [name="csrfmiddlewaretoken"]').val();
var idu = $('#idu').val();
var idp = $('#idp').val();
$.ajax({
url : "/login/",
type : "POST",
data : { csrfmiddlewaretoken: csrf_token, lgn: idu, pswd: idp},
traditional: true
}).done(function(response) {
if (response == "OK") {
$('#login-form .alert-danger').remove();
$('#login-form').prepend('<div class="alert alert-success" role="alert"><strong>Успешно!</strong><br>Подождите секундочку...</div>');
document.location.href = document.location.href.replace('#open_login', '');
} else {
if (!$('#login-form .alert-danger').length) {
$('#login-form').prepend('<div class="alert alert-danger" role="alert"><strong>Ошибка!</strong><br>Введите правильное имя пользователя и пароль.</div>');
}
}
});
});
def auth_view(request):
username = request.POST.get('lgn', '')
password = request.POST.get('pswd', '')
user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
return HttpResponse('OK')
else :
return HttpResponse("False")
url(r'^login/$', views.auth_view, name='login'),
<div class="modal fade" id="LogModal" tabindex="-1" role="dialog" aria-labelledby="LogModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="LogModalLabel">Вход в учетную запись</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form id="login-form" method="post" action="">{% csrf_token %}
<div class="modal-body">
<label for="id_username">Имя пользователя:</label>
<input class="form-control" type="text" name="username" maxlength="30" id="idu"/>
<label for="id_password">Пароль:</label>
<input class="form-control" type="password" name="password" id="idp"/>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Войти</button>
</div>
</form>
</div>
</div>
</div>
<button class="btn btn-outline-dark my-2 my-sm-0" type="button" style="font-weight: bold" data-toggle="modal" data-target="#LogModal">Регистрация / Вход</button>