2012-12-06 3 views
1

API의 non-orm 엔드 포인트를 작성하는 것이 합리적인지 알아 내려고합니다. Docs의 "ORM 데이터 소스로 Tastypie 사용하기"섹션을 보았지만, 폼 등을 처리하는 것에 대해 생각하고있다. 예 : 이메일을 처리하고 보내는 종점에 데이터 전달. DB에 아무 것도 저장하지 않으려 고합니다. 양식을 확인하고 보내려합니다. 나는 문서에서 뭔가를 놓치고 있습니까? 아니면 그냥 잘못된 나무를 짖고 있습니까?끝점이 아닌 엔드 포인트

답변

1

나는 당신이 당신 views.py에서 함수를 정의 정말 tastypie 사용없이 요청을 처리 할 수 ​​있다고 생각 : 여기

을 장고 웹 애플리케이션에에 새 사용자를 등록하는 예입니다; 요청에서 양식 데이터를 처리하여 대신 전자 메일을 보내도록 다시 용도를 지정할 수 있습니다.

# In views.py: 
... 
from django.http import HttpResponse 
from django.contrib.auth.models import User, Group 
from django.contrib.auth import authenticate 
from django.http import Http404 
from django.utils import timezone 
from models import * 
from api import * 
from django.utils import simplejson 
... 

# REST endpoint for user registration 
# Enforces server-side mediation (input validation) 
# On failure, raises Http404 
# On success, redirects to registration success page 
def register_user(request): 
    if request.method != 'POST': 
     raise Http404('Only POSTs are allowed') 

    # acquire params 
    username = request.POST['username'] 
    password = request.POST['password'] 
    repeatpw = request.POST['repeatpw'] 
    first_name = request.POST['first_name'] 
    last_name = request.POST['last_name'] 

    # Server-side mediation to check for invalid input 
    if username == '' or username is None: 
     raise Http404('Server-side mediation: Invalid Username') 

    if len(username) > 30: 
     raise Http404('Server-side mediation: username must be 30 characters or fewer') 

    if len(first_name) > 30: 
     raise Http404('Server-side mediation: first name must be 30 characters or fewer') 

    if len(last_name) > 30: 
     raise Http404('Server-side mediation: last name msut be 30 characters or fewer') 

    if len(password) < 4: 
     raise Http404('Server-side mediation: Password too short') 

    if password != repeatpw: 
     raise Http404('Server-side mediation: Password Mismatch') 

    # This try-except block checks existence of username conflict 
    try: 
     test_user_exists = User.objects.get(username__exact=username) 
     if test_user_exists != None: 
      raise Http404('Server-side mediation: Username exists') 
    except User.DoesNotExist: 
     pass 

    # Input passes all tests, proceed with user creation 
    user = User.objects.create_user(username, '[email protected]', password) 
    group = Group.objects.get(name='Standard') 
    user.first_name = first_name 
    user.last_name = last_name 
    user.groups.add(group) 
    user.is_staff = False  
    user.save() 

    # Build confirmation JSON 
    confirmation = { 
      'action': 'register_user', 
      'username': username, 
      'success': 'yes', 
    } 

    json_return = simplejson.dumps(confirmation) 

    # return JSON of the success confirmation 
    return HttpResponse(json_return, mimetype='application/json') 
+0

나는 그것을 고려했다. 그러나 나는 Tastypie에서 Authentication/Authorization 메카니즘을 사용하는 것을 좋아한다. 따라서 뷰를 사용하면 b : D가됩니다. –

관련 문제