使用formwizard-django创建用户后登录(Login after creating a user with formwizard-django)
我想在创建用户后登录。 但我正在使用formwizard,当我尝试时,我有一个错误“全局名称'请求'未定义”。
这是我的view.py
class ProfilWizard(SessionWizardView): template_name = "gestionProfil/profil-step1.html" def done(self, form_list, **kwargs): form_data = process_form_data(form_list) return render_to_response('gestionProfil/profil.html', {'form_data': form_data}) def process_form_data(form_list): form_data = [form.cleaned_data for form in form_list] username = form_data[0]['username'] password = form_data[0]['password'] email = form_data[0]['email'] user = User.objects.create_user(username, email, password) user.first_name = form_data[1]['firstName'] user.last_name = form_data[1]['lastName'] user.save() user = authenticate(username = username, password = password) if user: login(request, user) return form_data
那么在创建用户后我该如何登录呢?
编辑:在进行user777466建议的编辑后,我的views.py现在是:
class ProfilWizard(SessionWizardView): template_name = "gestionProfil/profil-step1.html" def done(self, form_list, **kwargs): (form_data,user) = process_form_data(form_list) user.backend = 'django.contrib.auth.backends.ModelBackend' if user: login(self.request, user) #return HttpResponse(form_data[0]['password']) return render_to_response('gestionProfil/profil.html', {'form_data': form_data}) def process_form_data(form_list): form_data = [form.cleaned_data for form in form_list] username = form_data[0]['username'] password = form_data[0]['password'] email = form_data[0]['email'] user = User.objects.create_user(username, email, password) user.first_name = form_data[1]['firstName'] user.last_name = form_data[1]['lastName'] user.save() return (form_data,user)
表单期间和之后的输出是:
[13 / Nov / 2013 08:39:12]“GET / gestionProfil / createNew / HTTP / 1.1”200 4639
[13 / Nov / 2013 08:39:22]“POST / gestionProfil / createNew / HTTP / 1.1”200 5085
[13 / Nov / 2013 08:39:40]“POST / gestionProfil / createNew / HTTP / 1.1”200 4601
[13 / Nov / 2013 08:39:40]“GET / captcha / image / c9adfbd5b6984ed094fdc7c4607acfb4d915d037 / HTTP / 1.1”200 4611
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init .py:827:RuntimeWarning:DateTimeField获得了一个天真的日期时间(2013-11-13 08:39:45.847325)区域支持是活动的。 RuntimeWarning)
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init .py:827:RuntimeWarning:DateTimeField获得了一个天真的日期时间(2013-11-13 08:39:45.864404)区域支持是活动的。 RuntimeWarning)
[13 / Nov / 2013 08:39:47]“POST / gestionProfil / createNew / HTTP / 1.1”200 3704
编辑2:我删除了我的数据库并创建了一个新的syncdb,现在验证工作正常。
I would like to login after the creation of users. But i'm using formwizard and I've got an error "global name 'request' is not defined" when i try.
Here is my view.py
class ProfilWizard(SessionWizardView): template_name = "gestionProfil/profil-step1.html" def done(self, form_list, **kwargs): form_data = process_form_data(form_list) return render_to_response('gestionProfil/profil.html', {'form_data': form_data}) def process_form_data(form_list): form_data = [form.cleaned_data for form in form_list] username = form_data[0]['username'] password = form_data[0]['password'] email = form_data[0]['email'] user = User.objects.create_user(username, email, password) user.first_name = form_data[1]['firstName'] user.last_name = form_data[1]['lastName'] user.save() user = authenticate(username = username, password = password) if user: login(request, user) return form_data
So how should i do to login after the creation of users?
Edit : After making the edit suggested by user777466 my views.py is now:
class ProfilWizard(SessionWizardView): template_name = "gestionProfil/profil-step1.html" def done(self, form_list, **kwargs): (form_data,user) = process_form_data(form_list) user.backend = 'django.contrib.auth.backends.ModelBackend' if user: login(self.request, user) #return HttpResponse(form_data[0]['password']) return render_to_response('gestionProfil/profil.html', {'form_data': form_data}) def process_form_data(form_list): form_data = [form.cleaned_data for form in form_list] username = form_data[0]['username'] password = form_data[0]['password'] email = form_data[0]['email'] user = User.objects.create_user(username, email, password) user.first_name = form_data[1]['firstName'] user.last_name = form_data[1]['lastName'] user.save() return (form_data,user)
The output during and after the form are :
[13/Nov/2013 08:39:12] "GET /gestionProfil/createNew/ HTTP/1.1" 200 4639
[13/Nov/2013 08:39:22] "POST /gestionProfil/createNew/ HTTP/1.1" 200 5085
[13/Nov/2013 08:39:40] "POST /gestionProfil/createNew/ HTTP/1.1" 200 4601
[13/Nov/2013 08:39:40] "GET /captcha/image/c9adfbd5b6984ed094fdc7c4607acfb4d915d037/ HTTP/1.1" 200 4611
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py:827: RuntimeWarning: DateTimeField received a naive datetime (2013-11-13 08:39:45.847325) while time zone support is active. RuntimeWarning)
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py:827: RuntimeWarning: DateTimeField received a naive datetime (2013-11-13 08:39:45.864404) while time zone support is active. RuntimeWarning)
[13/Nov/2013 08:39:47] "POST /gestionProfil/createNew/ HTTP/1.1" 200 3704
Edit 2: I've remove my database and made a new syncdb, now the authentication is working.
原文:https://stackoverflow.com/questions/19916764
最满意答案
javascript引擎使用不同的算法进行排序功能。 由于比较函数不比较值,因此您可以获得不同算法的内部工作结果,而不是具有排序结果。
查看V8引擎(Chrome)和JavaScriptCore的源代码(似乎是Safari使用的,或者至少sort函数给出相同的结果,所以我猜它使用相同类型的算法),你可以查看函数正在使用。
并不是说它可能不是所使用的功能,重要的是算法是不同的。 如果您实际比较值,它们会给出相同的结果,但如果不是,则结果取决于它们的运行方式,而不是函数本身。 至少不完全。
这是V8发动机分拣功能。 您将看到,对于大于10个元素的数组,算法不相同,因此小于10个元素的数组的结果与大于10个元素的数组不同。
您可以在此处找到以下算法: https : //code.google.com/p/chromium/codesearch#chromium/src/v8/src/js/array.js&q=array&sq=package : chromium&dr=C
comparefn = function(a, b) { return -1 } var InsertionSort = function InsertionSort(a, from, to) { for (var i = from + 1; i < to; i++) { var element = a[i]; for (var j = i - 1; j >= from; j--) { var tmp = a[j]; var order = comparefn(tmp, element); if (order > 0) { a[j + 1] = tmp; } else { break; } } a[j + 1] = element; } console.log(a); } var GetThirdIndex = function(a, from, to) { var t_array = new InternalArray(); // Use both 'from' and 'to' to determine the pivot candidates. var increment = 200 + ((to - from) & 15); var j = 0; from += 1; to -= 1; for (var i = from; i < to; i += increment) { t_array[j] = [i, a[i]]; j++; } t_array.sort(function(a, b) { return comparefn(a[1], b[1]); }); var third_index = t_array[t_array.length >> 1][0]; return third_index; } var QuickSort = function QuickSort(a, from, to) { var third_index = 0; while (true) { // Insertion sort is faster for short arrays. if (to - from <= 10) { InsertionSort(a, from, to); return; } if (to - from > 1000) { third_index = GetThirdIndex(a, from, to); } else { third_index = from + ((to - from) >> 1); } // Find a pivot as the median of first, last and middle element. var v0 = a[from]; var v1 = a[to - 1]; var v2 = a[third_index]; var c01 = comparefn(v0, v1); if (c01 > 0) { // v1 < v0, so swap them. var tmp = v0; v0 = v1; v1 = tmp; } // v0 <= v1. var c02 = comparefn(v0, v2); if (c02 >= 0) { // v2 <= v0 <= v1. var tmp = v0; v0 = v2; v2 = v1; v1 = tmp; } else { // v0 <= v1 && v0 < v2 var c12 = comparefn(v1, v2); if (c12 > 0) { // v0 <= v2 < v1 var tmp = v1; v1 = v2; v2 = tmp; } } // v0 <= v1 <= v2 a[from] = v0; a[to - 1] = v2; var pivot = v1; var low_end = from + 1; // Upper bound of elements lower than pivot. var high_start = to - 1; // Lower bound of elements greater than pivot. a[third_index] = a[low_end]; a[low_end] = pivot; // From low_end to i are elements equal to pivot. // From i to high_start are elements that haven't been compared yet. partition: for (var i = low_end + 1; i < high_start; i++) { var element = a[i]; var order = comparefn(element, pivot); if (order < 0) { a[i] = a[low_end]; a[low_end] = element; low_end++; } else if (order > 0) { do { high_start--; if (high_start == i) break partition; var top_elem = a[high_start]; order = comparefn(top_elem, pivot); } while (order > 0); a[i] = a[high_start]; a[high_start] = element; if (order < 0) { element = a[i]; a[i] = a[low_end]; a[low_end] = element; low_end++; } } } if (to - high_start < low_end - from) { QuickSort(a, high_start, to); to = low_end; } else { QuickSort(a, from, low_end); from = high_start; } } }; InsertionSort([1, 2, 3, 4, 5], 0, 5); //QuickSort is recursive and calls Insertion sort, so you'll have multiple logs for this one QuickSort([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 0, 13); //You'll see that for arrays bigger than 10, QuickSort is called. var srt = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13].sort(function() { return -1 }) console.log(srt)
JavaScriptCore使用合并排序。 你可以在这里找到这个算法: http : //trac.webkit.org/browser/trunk/Source/JavaScriptCore/builtins/ArrayPrototype.js
function min(a, b) { return a < b ? a : b; } function merge(dst, src, srcIndex, srcEnd, width, comparator) { var left = srcIndex; var leftEnd = min(left + width, srcEnd); var right = leftEnd; var rightEnd = min(right + width, srcEnd); for (var dstIndex = left; dstIndex < rightEnd; ++dstIndex) { if (right < rightEnd) { if (left >= leftEnd || comparator(src[right], src[left]) < 0) { dst[dstIndex] = src[right++]; continue; } } dst[dstIndex] = src[left++]; } } function mergeSort(array, valueCount, comparator) { var buffer = []; buffer.length = valueCount; var dst = buffer; var src = array; for (var width = 1; width < valueCount; width *= 2) { for (var srcIndex = 0; srcIndex < valueCount; srcIndex += 2 * width) merge(dst, src, srcIndex, valueCount, width, comparator); var tmp = src; src = dst; dst = tmp; } if (src != array) { for (var i = 0; i < valueCount; i++) array[i] = src[i]; } return array; } console.log(mergeSort([1, 2, 3, 4, 5], 5, function() { return -1; }))
同样,这些可能并不完全是每个浏览器中使用的函数,但它会向您展示如果您实际上不比较值,不同算法的行为方式。
The javascript engines use different algorithms for their sort function. Since the compare function doesn't compare values, you get the result of the inner workings of the different algorithms instead of having a sorted result.
Looking at the source code of V8 engine (Chrome) and JavaScriptCore (which seems to be used by Safari, or at least the sort function gives the same result, so I guess it uses the same kind of algorithm), you can view the functions that are being used.
Not that it might not be exactly the functions used, what's important is that the algorithms are different. They give the same result if you actually compare values, but if not, the result is dependent on the way they operate, not on the function itself. At least not completely.
Here's V8 engine sorting function. You'll see that for arrays bigger than 10 elements, the algorithm isn't the same, so the result for arrays smaller than 10 elements is different than for those bigger than 10 elements.
You can find following algorithms here: https://code.google.com/p/chromium/codesearch#chromium/src/v8/src/js/array.js&q=array&sq=package:chromium&dr=C
comparefn = function(a, b) { return -1 } var InsertionSort = function InsertionSort(a, from, to) { for (var i = from + 1; i < to; i++) { var element = a[i]; for (var j = i - 1; j >= from; j--) { var tmp = a[j]; var order = comparefn(tmp, element); if (order > 0) { a[j + 1] = tmp; } else { break; } } a[j + 1] = element; } console.log(a); } var GetThirdIndex = function(a, from, to) { var t_array = new InternalArray(); // Use both 'from' and 'to' to determine the pivot candidates. var increment = 200 + ((to - from) & 15); var j = 0; from += 1; to -= 1; for (var i = from; i < to; i += increment) { t_array[j] = [i, a[i]]; j++; } t_array.sort(function(a, b) { return comparefn(a[1], b[1]); }); var third_index = t_array[t_array.length >> 1][0]; return third_index; } var QuickSort = function QuickSort(a, from, to) { var third_index = 0; while (true) { // Insertion sort is faster for short arrays. if (to - from <= 10) { InsertionSort(a, from, to); return; } if (to - from > 1000) { third_index = GetThirdIndex(a, from, to); } else { third_index = from + ((to - from) >> 1); } // Find a pivot as the median of first, last and middle element. var v0 = a[from]; var v1 = a[to - 1]; var v2 = a[third_index]; var c01 = comparefn(v0, v1); if (c01 > 0) { // v1 < v0, so swap them. var tmp = v0; v0 = v1; v1 = tmp; } // v0 <= v1. var c02 = comparefn(v0, v2); if (c02 >= 0) { // v2 <= v0 <= v1. var tmp = v0; v0 = v2; v2 = v1; v1 = tmp; } else { // v0 <= v1 && v0 < v2 var c12 = comparefn(v1, v2); if (c12 > 0) { // v0 <= v2 < v1 var tmp = v1; v1 = v2; v2 = tmp; } } // v0 <= v1 <= v2 a[from] = v0; a[to - 1] = v2; var pivot = v1; var low_end = from + 1; // Upper bound of elements lower than pivot. var high_start = to - 1; // Lower bound of elements greater than pivot. a[third_index] = a[low_end]; a[low_end] = pivot; // From low_end to i are elements equal to pivot. // From i to high_start are elements that haven't been compared yet. partition: for (var i = low_end + 1; i < high_start; i++) { var element = a[i]; var order = comparefn(element, pivot); if (order < 0) { a[i] = a[low_end]; a[low_end] = element; low_end++; } else if (order > 0) { do { high_start--; if (high_start == i) break partition; var top_elem = a[high_start]; order = comparefn(top_elem, pivot); } while (order > 0); a[i] = a[high_start]; a[high_start] = element; if (order < 0) { element = a[i]; a[i] = a[low_end]; a[low_end] = element; low_end++; } } } if (to - high_start < low_end - from) { QuickSort(a, high_start, to); to = low_end; } else { QuickSort(a, from, low_end); from = high_start; } } }; InsertionSort([1, 2, 3, 4, 5], 0, 5); //QuickSort is recursive and calls Insertion sort, so you'll have multiple logs for this one QuickSort([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 0, 13); //You'll see that for arrays bigger than 10, QuickSort is called. var srt = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13].sort(function() { return -1 }) console.log(srt)
And JavaScriptCore uses merge sort. You can find this algorithm here: http://trac.webkit.org/browser/trunk/Source/JavaScriptCore/builtins/ArrayPrototype.js
function min(a, b) { return a < b ? a : b; } function merge(dst, src, srcIndex, srcEnd, width, comparator) { var left = srcIndex; var leftEnd = min(left + width, srcEnd); var right = leftEnd; var rightEnd = min(right + width, srcEnd); for (var dstIndex = left; dstIndex < rightEnd; ++dstIndex) { if (right < rightEnd) { if (left >= leftEnd || comparator(src[right], src[left]) < 0) { dst[dstIndex] = src[right++]; continue; } } dst[dstIndex] = src[left++]; } } function mergeSort(array, valueCount, comparator) { var buffer = []; buffer.length = valueCount; var dst = buffer; var src = array; for (var width = 1; width < valueCount; width *= 2) { for (var srcIndex = 0; srcIndex < valueCount; srcIndex += 2 * width) merge(dst, src, srcIndex, valueCount, width, comparator); var tmp = src; src = dst; dst = tmp; } if (src != array) { for (var i = 0; i < valueCount; i++) array[i] = src[i]; } return array; } console.log(mergeSort([1, 2, 3, 4, 5], 5, function() { return -1; }))
Again these may not be exactly the functions used in each browser, but it shows you how different algorithms will behave if you don't actually compare values.
相关问答
更多-
因为Array.Sort 使用默认比较器进行排序。 并且该比较器将根据当前文化比较两个字符串(请注意,当双方都是字符串时,比较器具有故障转移机制)。 这通常会根据他们的字符代码对它们进行比较,然后数字会出现在字母之前。 Because Array.Sort uses the default comparer in order to sort. And that comparer will compare both strings based on the current culture (note that ...
-
Array.Sort是一个不稳定的排序,所以相同的元素的顺序是未定义的,而不是守恒的。 MSDN中的Array.Sort文章指出: 该方法使用QuickSort算法。 此实现执行不稳定的排序; 也就是说,如果两个元素相等,他们的顺序可能不会被保留。 相反,稳定的排序保留了相同元素的顺序。 另一方面,LINQ的OrderBy方法是稳定的。 MSDN中有关OrderBy的文章指出: 此方法执行稳定的排序; 也就是说,如果两个元素的键相等,则元素的顺序被保留。 相反,不稳定的排序不会保留具有相同键的元素的顺序。 ...
-
我认为你的问题是你不知道数组到底有多大,它比你预期的要大,而且Array.Sort从后面重新Array.Sort数组......考虑: Dim arr() As String ReDim arr(10) arr(0) = "Z" arr(2) = "3" arr(1) = "Boolean" arr(5) = "Hello" Array.Sort(arr) For i As Integer = 0 To 10 Debug.Print(arr(i)) Next 即使我将数组的大小指定为11并且仅使用数据 ...
-
使用JavaScript Array.sort()方法进行洗牌是否正确?(Is it correct to use JavaScript Array.sort() method for shuffling?)[2022-03-09]
它从来不是我最喜欢的洗牌方式,部分原因是它是按照你所说的实现特定的。 特别是,我似乎记得,从Java或.NET(不确定哪个)中的标准库可以经常检测到,如果最终得到某些元素之间的不一致的比较(例如,您首先要求A < B和B < C ,但是C < A )。 它也最终会比您真正需要的更复杂(在执行时间方面)洗牌。 我更喜欢洗牌算法,它有效地将集合分成“洗牌”(在收集开始时,最初是空的)和“未混洗”(集合的其余部分)。 在算法的每个步骤中,选择一个随机的非抽头元素(可能是第一个元素),并与第一个未抽取元素交换 - ... -
NSArray *sortedArray = [pointsAry sortedArrayUsingComparator:^(NSString *str1, NSString *str2){ return [@([str1 intValue]) compare:@([str2 intValue])]; }]; NSArray *sortedArray = [pointsAry sortedArrayUsingComparator:^(NSString *str1, NSString *str2){ ...
-
javascript引擎使用不同的算法进行排序功能。 由于比较函数不比较值,因此您可以获得不同算法的内部工作结果,而不是具有排序结果。 查看V8引擎(Chrome)和JavaScriptCore的源代码(似乎是Safari使用的,或者至少sort函数给出相同的结果,所以我猜它使用相同类型的算法),你可以查看函数正在使用。 并不是说它可能不是所使用的功能,重要的是算法是不同的。 如果您实际比较值,它们会给出相同的结果,但如果不是,则结果取决于它们的运行方式,而不是函数本身。 至少不完全。 这是V8发动机分拣功 ...
-
我如何确保一个稳定的array.sort()命令,javascript(How can I ensure a stable array.sort() order, javascript)[2023-08-03]
首先,像Array#sort和其他排序实现: 排序不一定稳定 。 为了保持稳定的排序,你可以使用一个唯一的值,比如索引作为排序条件链中的最后一个排序值,比如 function asc(key) { return function (a, b) { return a[key] - b[key] || a.id - b.id; }; } array.sort(asc('value')); First of all, like Array#sort and other sort ... -
你需要在循环外排序。 protected void sortImageButton_Click(object sender, ImageClickEventArgs e) { string[] sort = new string[cartListBox.Items.Count]; for (int i = 0; i < sort.Length; i++) { sort[i] = cartListBox.Items[i].ToString(); ...
-
Mono和Array.Sort(Mono and Array.Sort)[2022-08-16]
Array位于System命名空间中,包含在mscorlib (Mono / .Net mscorlib / 4.5 +)中。 使用: using System; 剪切/粘贴示例: using System; namespace ArraySort { class MainClass { public static void Main(string[] args) { var Color = new String[5]; ... -
我已经对sort方法中发生的事情做了一些控制台记录,我得出的结论是,当排序迭代数组时,它会重复使用比较函数再次移动位置的任何元素。 事实上你的条件更具体,并且在一个大的数组中使得很难跟踪正在发生的事情。 我已经使用了你的第一个例子并记录了三元运算符和数组的结果。 你得到以下: [2, 3, 2, 0] -1 [2, 3, 2, 0] -1 [2, 3, 2, 0] 1 [2, 3, 2, 2] 1 [2, 3, 3, 2] 1 [0, 2, 3, 2] 当您对[2, 3, 2, 0, ...