小编Joh*_*ohn的帖子

django外键保存

我有像这样的模型.

class Mp3(models.Model):
    title=models.CharField(max_length=30)
    artist=models.ForeignKey('Artist')
Run Code Online (Sandbox Code Playgroud)

以下是Artist模型的外观:

class Artist(models.Model):
    name=models.CharField(max_length=100,default="Unknown")
Run Code Online (Sandbox Code Playgroud)

我创建了一个ID为1的艺术家.我如何创建一个分配给这位艺术家的mp3?(我想要这样的查询,例如.

mp3=Mp3.objects.get(id=50)
mp3.artist
Run Code Online (Sandbox Code Playgroud)

我试过这样的事

newMp3=Mp3(title="sth",artist=1)
Run Code Online (Sandbox Code Playgroud)

但我得到了

ValueError: Cannot assign "1": "Mp3.artist" must be a "Artist" instance.
Run Code Online (Sandbox Code Playgroud)

我理解错误但仍然不知道如何解决这个问题.感谢您的帮助.最诚挚的问候

django models

12
推荐指数
2
解决办法
2万
查看次数

ruby Nokogiri xpath获取节点的内容

我有这样的代码

@doc = Nokogiri::HTML(open(url)
@doc.xpath(query).each do |html|

  puts html # how get content of a node
end
Run Code Online (Sandbox Code Playgroud)

我的问题是如何获取节点的内容,因为现在我得到了这样的东西.

<li class="stat">
Run Code Online (Sandbox Code Playgroud)

ruby nokogiri nodes

8
推荐指数
2
解决办法
2万
查看次数

c ++引用数组

我怎么能让这段代码工作?

#include <iostream>
using namespace std;

void writeTable(int (&tab)[],int x){
    for(int i=0;i<x;i++){
        cout << "Enter value " << i+1 <<endl;
        cin >> tab[i] ;
    }
}


int main(void){
    int howMany;
    cout << "How many elemets" << endl;
    cin >> howMany;

    int table[howMany];
    int (&ref)[howMany]=table;
    writeTable(ref,howMany);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下是我的错误:

|4|error: parameter ‘tab’ includes reference to array of unknown bound ‘int []’|
|18|error: invalid initialization of reference of type ‘int (&)[]’ from expression of type ‘int [(((unsigned int)(((int)howMany) + -0x00000000000000001)) + …
Run Code Online (Sandbox Code Playgroud)

c++ arrays reference pass-by-reference

7
推荐指数
3
解决办法
1万
查看次数

spring boot覆盖默认的REST异常处理程序

我无法在REST api中覆盖默认的Spring引导错误响应.我有以下代码

@ControllerAdvice
@Controller
class ExceptionHandlerCtrl {

    @ResponseStatus(value=HttpStatus.UNPROCESSABLE_ENTITY, reason="Invalid data")
    @ExceptionHandler(BusinessValidationException.class)
    @ResponseBody
    public ResponseEntity<BusinessValidationErrorVO> handleBusinessValidationException(BusinessValidationException exception){
        BusinessValidationErrorVO vo = new BusinessValidationErrorVO()
        vo.errors = exception.validationException
        vo.msg = exception.message
        def result =  new ResponseEntity<>(vo, HttpStatus.UNPROCESSABLE_ENTITY);
        result

    }
Run Code Online (Sandbox Code Playgroud)

然后在我的REST api中,我抛出了这个BusinessValidationException.调用此处理程序(我可以在调试器中看到它)但是我仍然有默认的spring boot REST错误消息.有没有办法覆盖并使用默认值作为后备?带有常规的Spring Boot版本1.3.2.最好的祝福

rest groovy spring spring-boot

6
推荐指数
1
解决办法
6699
查看次数

Django Pylons神话

我刚刚开始使用Web框架和Web开发(我只使用PHP CI进行了一些操作)并决定转向python(我需要一般编程语言,不仅限于Web应用程序).我在google上做过研究,发现Pylons和Django是python的最佳和最流行的web框架.但是在一些博客上,讨论人们说Django非常适合某种CMS应用程序(博客或类似的东西),而在其他情况下,它不是一个好的选择(缺乏灵活性等等).这是真的?而且我也注意到人们说Pylons更难学,但后来更强大.这是真的吗?

对于那些不是蟒蛇大师和OOP大师的人来说,Pylons与Django相比有多难?我知道在这两种情况下都需要工作知识python才能有效地使用框架.我有时间学习语言和框架,但只是想知道这两个中的哪一个.我也会在app JS中使用,但是当我学习它时,我宁愿自己编写代码而不是使用一些"魔术"助手等.

django pylons

5
推荐指数
1
解决办法
1245
查看次数

rails 3.2 capybara Capybara :: ElementNotFound:无法找到xpath"/ html"

我正在尝试使用rspec 2.10.0 + capybara 1.1.2测试我的rails应用程序.这是我的测试文件

require 'spec_helper'

    describe AdminPanelController do
      describe "index" do
        it "should have return code 200" do
          visit '/admin'
          page.should have_content "hello"
          #response.status.should be(200)
        end
      end
    end
Run Code Online (Sandbox Code Playgroud)

这是测试结果

 Failure/Error: page.should have_content "hello"
 Capybara::ElementNotFound:
   Unable to find xpath "/html"
Run Code Online (Sandbox Code Playgroud)

我谷歌关于这个问题,但只找到webrat可能是一个问题的信息,但我没有安装这个宝石.谢谢你的任何建议.

ruby-on-rails capybara

5
推荐指数
1
解决办法
3564
查看次数

IntelliJ IDEA 13无法在spring中解析方法

我有以下代码行的烦人问题:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN").
            and().
            formLogin().
            loginPage("/login").
            defaultSuccessUrl("/").
            failureUrl("/login?error").
            loginProcessingUrl("/login").
            successHandler(loginSucessHandler).
            permitAll().
            and().
            logout().
            logoutUrl("/logout").
            logoutSuccessUrl("/").
            invalidateHttpSession(true).
            permitAll();
}
Run Code Online (Sandbox Code Playgroud)

Intellij总是告诉我"无法解析方法formLogin()".我尝试重新导入项目从gradle重新生成项目使intellij的缓存无效但我总是得到这个烦人的消息.有可能以某种方式摆脱它?如果我尝试从gradle构建项目,我没有问题,所以它一定是intellij想法的错误.在此先感谢编辑:如果我在intellij中运行make它编译项目没有任何问题.

spring intellij-idea

5
推荐指数
0
解决办法
1259
查看次数

Spring安全认证提供程序java配置

我已经实现了自己的UserDetailsS​​ervice.我在java中配置spring security.如何使用自定义用户服务详细信息服务和一些密码编码器创建默认身份验证提供程序?

最先感谢最好的问候编辑:这是我尝试的:这是我的用户详细信息服务impl的一部分:

public class UserDetailsServiceImpl implements UserDetailsService 
Run Code Online (Sandbox Code Playgroud)

后来在我的安全配置中我有这样的事情:

@Bean
public UserDetailsServiceImpl userDetailsService(){
    return new UserDetailsServiceImpl();
}


@Bean
public AuthenticationManager authenticationManager() throws Exception{
    return auth.build();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder);
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此代码时,我有异常:

Caused by: java.lang.IllegalArgumentException: Can not set com.xxx.UserDetailsServiceImpl field com.....MyAuthenticationProvider.service to com.sun.proxy.$Proxy59
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:741)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:504)
    ... 58 more
Run Code Online (Sandbox Code Playgroud)

我想我做错了什么

spring spring-security

4
推荐指数
1
解决办法
8401
查看次数

Spring 4 Hibernate 验证器本地化消息

我尝试使用自定义休眠消息进行本地化,但无法使其正常工作。我经过验证的类如下所示:

@Entity
@Table(name = "USERS")
public class UserEntity extends AbstractBaseEntity implements UserDetails {

    @NotNull
    @Column(unique = true, nullable = false)
    @Size(min = 5, max = 30)
    private String username;
Run Code Online (Sandbox Code Playgroud)

这是我的弹簧配置的一部分:

<!-- Localization of hibernate messages during validation!-->
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:validation" />
</bean>

<bean name="validator"  class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource">
        <ref bean="validationMessageSource"/>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

在资源中,我有两个验证文件:validation_en.properties 和 validation_pl.properties 下面是示例条目:

NotNull.UserEntity.username=Username can't be empty!
Run Code Online (Sandbox Code Playgroud)

当我显示验证错误时,我看到标准消息“可能不为空”,而不是我的自定义和本地化消息。我做错了什么?在此先感谢您的帮助,最好的问候

validation spring localization spring-mvc

4
推荐指数
1
解决办法
8762
查看次数

django外部脚本ImportError:没有名为Utils的模块

我搜索谷歌和这个论坛,我找到了这个解决方案

sth=os.path.abspath('/home/artur/Desktop')
sys.path.append(sth)
os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
Run Code Online (Sandbox Code Playgroud)

即使我的编辑器提供对import语句的支持,每个想法似乎都可以.当我尝试导入我的模型时,我得到了这个

Traceback (most recent call last):
  File "email.py", line 12, in <module>
    from app_name.models import *
  File "app's models models.py", line 1, in <module>
    from django.db import models
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/db/models/__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/db/models/query.py", line 9, in <module>
    from django.db.models.fields import DateField
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/db/models/fields/__init__.py", line 14, in <module>
    from django import forms
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/forms/__init__.py", line 14, in <module>
    from widgets import *
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/forms/widgets.py", line 9, in <module> …
Run Code Online (Sandbox Code Playgroud)

django

3
推荐指数
1
解决办法
5541
查看次数