EDITED
我正在尝试使用jquery/ajax来显示从django方法返回的数据.
我有一个名为keywordBtn的html按钮.因此,当按下它时,将调用updateKeywordSubscribed方法.
但是,我的目标不是由django返回的.我的方法有问题吗?
如果成功,div部分名称"update"将显示该json列表中的单词列表.
我的HTML中有什么:
<script type="text/javascript">
$(document).ready(function() {
$("#keywordBtn").click(function(e) {
updateKeywordSubscribed(e, "#keywords");
});
});
function updateKeywordSubscribed(e, keywords) {
e.preventDefault();
var option_form = jQuery(e.target);
$.ajax({
url : option_form.attr('action'),
type : option_form.attr('method'),
data : option_form.serialize(),
dataType : 'json',
success : function(response) { alert ('sometext')})
}
</script>
Run Code Online (Sandbox Code Playgroud)
我在views.py中的内容:
def keyword_subscribe(request):
if 'keyword_input' in request.POST:
if 'name_input' in request.POST:
xhr = request.GET.has_key('xhr')
response_dict = {}
new_keyword = request.POST['keyword_input']
username = request.POST['name_input']
response_dict.update({'keyword_input': new_keyword, 'name_input': username})
power_keyword = subscribe_keyword(username,keywords)
if power_keyword:
response_dict.update({'success':True})
else: …
Run Code Online (Sandbox Code Playgroud) 您好我正在尝试检查复选框是否已选中.但是我的复选框是使用for循环创建的,每个复选框的值实际上是相同的.
代码如下:
{% for keyword in keyword_list %}
{% if keyword.keyword_name == userprofile.keywords_subscribed %}
<input type="checkbox" disabled="disabled" name="keywords" value="keywords"/> {{keyword.keyword_name}}<br />
{% else %}
<input type="checkbox" name="cb" value="keywords" /> {{keyword.keyword_name}}<br />
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
Jquery代码如下:
<h2>Price of subscriptions</h2>
<script type="text/javascript" src="/static/js/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="/static/js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="/static/fc3/charts/FusionCharts.js"></script>
<script type="text/javascript">
function popup(){
{% for price in price_list %}
alert({{price.price_of_each_keyword}})
{% endfor %}
}
function DoTheCheck() { if(document.cb2.checked == true)
{ alert('box2 is checked'); }
}
</script>
Run Code Online (Sandbox Code Playgroud)
目前这是我能想到的全部.
那么我如何使用jquery遍历复选框并检查它们是否被选中? …
我最近尝试使用以下内容扩展django的注册表单,但我只能看到默认的四个字段.有什么我想念的吗?
或者,如果我要创建自定义表单,我应该创建自己的注册后端吗?
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_(u'Username'))
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_(u'Email address'))
first_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'First Name'))
last_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'Last Name'))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_(u'Password'))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_(u'Password (again)'))
keywords = forms.ModelMultipleChoiceField(queryset=Keyword.objects.all())
#keywords = forms.ModelChoiceField(queryset=Keyword.objects.all())
def clean_username(self):
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_(u'You must type the same password each …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将用户userID合并到侧边栏上.
所以侧边栏会显示"欢迎用户名"
我尝试过cookie方法,但似乎没有用.
这是我的cookie收到我的用户名:
def login_user(request):
state = "Please log in below..."
username = password = ''
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
cookie = Cookie.SimpleCookie()
cookie['username']=str(time.time())
return render_to_response('r2/topic/index.html',{})
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response('registration/login.html',{'state':state, 'username': username})
return render_to_response('registration/login.html',{'state':state, 'username': username}) …
Run Code Online (Sandbox Code Playgroud)