对不起,如果您在我之前提出此问题时尝试过帮助我.不得不删除该问题,因为出于某种原因我不被允许编辑其他信息.
我正在我的django网站上实现用户身份验证.一切正常.我的观点,模型,网址等都已设置完毕.用户可以注册,登录,注销.我遇到的问题是这段代码:
{% if request.user.is_authenticated %}
<li><a href="/logout">Log Out</a></li>
{% else %}
<li><a href="/login">Log In</a></li>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
即使我已登录,它仍然显示"登录"作为选项而不是"注销".但是,如果我点击链接,它会将我重定向到/ profile,因为如果我已经登录,那就是视图告诉它要做的事情.所以,显然它知道我已登录,但模板不是readint user.is_authenticated为true.
与登录请求相关的视图是:
def LoginRequest(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
profile = authenticate(username=username, password=password)
if profile is not None:
login(request, profile)
return HttpResponseRedirect('/profile/')
else:
return render_to_response('template/login.html', {'form': form}, context_instance=RequestContext(request))
else:
return render_to_response('template/login.html', {'form': form}, context_instance=RequestContext(request))
else:
''' user is not submitting the form, show them login form …
Run Code Online (Sandbox Code Playgroud) 我只想从事件列表中提取每个单独的事件并对它们执行操作.我目前在我看来的代码是:
user = request.user.get_profile()
followed = user.eventList
eL = [getEvent.getEvent(e_id) for e_id in followed]
Run Code Online (Sandbox Code Playgroud)
首先,我抓住当前登录的用户,然后查看他的eventList,然后迭代它.我得到了上述错误.想想我可能会错过一些线路?
所以我试图让事情变得非常简单.我想在搜索框中输入一个术语,并在结果页面上显示.
我的HTML表单是
<form method="get" action="/results/" class="navbar-form pull-right">
<input type="text" id="searchBox" class="input-medium search-query" name="q" placeholder="Search">
<input type="submit" class="btn" value="Search" >
</form>
Run Code Online (Sandbox Code Playgroud)
views.py看起来像这样:
def search(request):
query = request.GET['q']
t = loader.get_template('template/results.html')
c = Context({ 'query': query,})
return HttpResponse(t.render(c))
Run Code Online (Sandbox Code Playgroud)
最后,结果模板包含:
<div>You searched for: {{ query }} </div>
Run Code Online (Sandbox Code Playgroud)
这是urls.py
urlpatterns = patterns('',
url(r'^home/$', 'search.views.home'),
url(r'^results/$', 'search.views.results'),
Run Code Online (Sandbox Code Playgroud)
没有任何东西出现在这个{{ query }}
空间里.
我在我的Django项目中使用Pagination并且一切正常,但我不知道如何扩展我的范围超出我所在的那个页面的+/-一页.目前,我的HTML看起来像这样:
<div class="pagination pagination-centered">
<ul>
{% if title.has_previous %}
<li><a href="?q={{ query }}&page={{ title.previous_page_number }}">«</a></li>
<li><a href="?q={{ query }}&page={{ title.previous_page_number }}">{{ title.previous_page_number }}</a></li>
{% endif %}
<li class="active"><a href="?q={{ query }}&page={{ title.number }}">{{ title.number }}</a></li>
{% if title.has_next %}
<li><a href="?q={{ query }}&page={{ title.next_page_number }}">{{ title.next_page_number }}</a></li>
<li><a href="?q={{ query }}&page={{ title.next_page_number }}">»</a></li>
{% endif %}
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
这导致一些看起来像[<<,1,2,3,>>]如果你有两个页面上.我想通过一两步左右扩大范围.所以,如果我在第3页,我可以从1-5开始一直到达.
所以我在 Item1 和 Item2 之间有一个 ManyToManyField 关系。在网页上,我想根据两个项目是否连接来显示两个消息之一。我只是不确定如何使用 {% if %} 模板标签查询我的确切项目。
大约我正在寻找的是
{% if Item1 is connected to Item2 %} Display Message1
{% else %} Display Message2 {% endif %}
Run Code Online (Sandbox Code Playgroud)
关于我如何完成这项工作的任何提示?
class Profile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=50)
eventList = models.ManyToManyField(Event, blank="TRUE", null="TRUE", related_name='event_set+')
def __unicode__(self):
return self.name
Run Code Online (Sandbox Code Playgroud) 我得到的确切错误是"未捕获错误:日历:未设置事件加载URL".我完全遵循https://github.com/Serhioromano/bootstrap-calendar上的说明.我只是想把这个东西加载所以我有一个空的主体与<div id="calendar"></div>
我的javascript包含var calendar = $('#calendar').calendar();
.但是,当我加载页面时,没有任何内容显示,上面的错误显示在我的控制台中,指向我的javascript行.我真的不太清楚该怎么做.
我正在尝试按某个关键字过滤单词/短语列表.我发现filter()
函数的所有例子都在使用数字,所以我想知道这是否可行.我知道filter()
如果它调用的函数返回,它会将一个项放入结果列表中True
.
假设我有这样的事情:
def filtCheck(item, filt):
if filt in item:
return True
def funct():
filt = 'Hi'
set1 = ['Hello, world', 'Hi there', 'Hi friend']
set2 = filter(filtCheck(filt), set1)
print set2
Run Code Online (Sandbox Code Playgroud)
这是我感到困惑的地方.我究竟如何在set2行上写出第一个参数?显然不是它的写法,因为我的filtCheck函数有两个参数,我只提供一个.我是否还需要修改我的filtCheck功能?但是如果我从中取出item参数,则没有字符串可以检查filt是否在.
我有许多"联系"对象,每个对象都有一个与之关联的imageURL字符串.我所看到的将图像放入ListView的所有方法都涉及手动将图像放入"可绘制"文件夹并调用资源.手动输入图像会破坏这个目的.我提供了我的getView方法,注释掉的行是我很困惑的.
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
TextView name = (TextView) row.findViewById(R.id.topLine);
TextView phone = (TextView) row.findViewById(R.id.secondLine);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
name.setText(contactArray.get(position).getName());
phone.setText((CharSequence) contactArray.get(position).getPhone().getWorkPhone());
//icon.setImage from contactArray.get(position).getImageURL(); ????
return row;
}
Run Code Online (Sandbox Code Playgroud) 我正在输入ID号,并在输入文件时将每个ID写入文件中。写入文件的ID只是备份,以防万一程序由于某种原因而崩溃(这是一件小小的个人事情)。因此,如果意外关闭,我将只有一个文本文件,其中包含我已经输入的所有ID号。但是,我注意到在手动关闭.close()之前,不会将ID保存到文件中,但这只会违背目的。
这是我第一次做这样的事情,所以我不确定什么是正确的方法。有没有某种方法可以在不关闭BufferedWriter的情况下保存文本?还是可以关闭它并以某种方式将其打开备份?
谢谢您的建议!
所以我有一个函数来检查用户的邮箱和一个在它之后定义的变量来设置这个函数的频率。但是,如果用户甚至没有登录网站,我不希望该功能发送不必要的请求。我在哪里/如何将 if user.is_authenticated 参数传递给此代码以进行检查?
function checkMail() { // Checks for new mail in user's inbox
$.ajax({
url: "/checkMail/",
type: "POST",
success: function(data) {
obj = JSON.parse(data);
if(!obj.read){
$('#mailsquare').css('background-color', '#A80000');
$('.dropdown-menu').prepend('<li class="message" id="{{item.id}}">'+obj.newest.substring(0,30)+'</li>');
} else {
$('#mailsquare').css('background-color', '#1b1b1b');
}
},
error: function(xhr, status, error) { console.log(xhr.status); console.log(xhr.responseText); console.log(status); console.log(error); }
});
}
var interval = setInterval(checkMail, 10000); // Set interval for frequency of mail checks
Run Code Online (Sandbox Code Playgroud) 我正在尝试在C中为类实现Sieve算法.我不是要求为我完成这项任务.我已经写出了我的函数,但是我目前正在收到Segmentation Fault错误.我不是100%肯定那是什么.这是我的代码到目前为止,任何人都可以看到这个错误来自哪里?
#define EXTERN
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
void clearAll() {
int i, j;
seg *p;
p = head;
for(i = 0; i < NSegs; i++) {
p = p -> next;
for(j = 0; j < 256; j++) {
p -> bits[j] = 0;
}
}
}
int setBit(int n) {
int segment, index, hold, pos, i;
seg *p;
p = head;
segment = n/256;
hold = n;
while(hold > 65) {
hold = hold - 65; …
Run Code Online (Sandbox Code Playgroud) django ×6
python ×3
javascript ×2
android ×1
c ×1
django-forms ×1
django-views ×1
html ×1
imageview ×1
java ×1
linked-list ×1
pagination ×1
search ×1