Django表单使用ajax form.serialize()提交

时间:2018-10-20 04:47:04

标签: python django python-3.x django-forms django-views

views.py

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringFilterTest {
    @Test
    public void getHealthTest() throws Exception {
        standaloneSetup(new PersonController()).addFilter(new SkipFilter()).build().perform(get("/health")).andExpect(status().isOk());
    }
    @Test
    public void getPersonTest() throws Exception {
        standaloneSetup(new PersonController()).addFilter(new SkipFilter()).build().perform(get("/person")).andExpect(status().isAccepted());
    }
    private class SkipFilter extends OncePerRequestFilter {

        private Set<String> skipUrls = new HashSet<>(Arrays.asList("/health"));
        private AntPathMatcher pathMatcher = new AntPathMatcher();

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                        FilterChain filterChain) throws ServletException, IOException {
            filterChain.doFilter(request, response);
            response.setStatus(HttpStatus.ACCEPTED.value());
        }

        @Override
        protected boolean shouldNotFilter(HttpServletRequest request) {
            return skipUrls.stream().anyMatch(p -> pathMatcher.match(p, request.getServletPath()));
        }
    }
    @RestController
    @RequestMapping(value = "/")
    private static class PersonController {

        @GetMapping("person")
        public void getPerson() {
        }

        @GetMapping("health")
        public void getHealth() {
        }
    }
}

login.html

def login(request):
        password = request.POST.get('password')
        mobile = request.POST.get('mobile')
        user = authenticate(username=mobile, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect("/owner/?own=" + str(user.id))

我遇到错误:

不允许使用方法(POST):/ 不允许的方法:/ [20 / Oct / 2018 04:41:30]“ POST / HTTP / 1.1” 405 0

1 个答案:

答案 0 :(得分:0)

您的登录功能和django内置的登录功能之间存在名称冲突,请将您的功能重命名为user_login之类的名称。