Javascript-форум (https://javascript.ru/forum/)
-   Angular.js (https://javascript.ru/forum/angular/)
-   -   django restful api angular (https://javascript.ru/forum/angular/57054-django-restful-api-angular.html)

skripka696 17.07.2015 09:33

django restful api angular
 
помогите пожалуйста, у меня есть модель
lass Color(models.Model):
    code = RGBColorField()
    
    def __unicode__(self):
        return '{0}'.format( self.code)
class Category(models.Model):
    category_name = models.CharField(max_length=255)
    parent = models.ForeignKey('self', blank=True, null=True,
                               verbose_name="Parent", related_name='Child')
    def __unicode__(self):
        return '{0}'.format(self.category_name)
mptt.register(Category,)
class Label(models.Model):
    label_name = models.CharField(max_length=20) 
    def __unicode__(self):
        return '{0}'.format(self.label_name)
class Media_Note(models.Model):
    my_file = models.FileField(upload_to = 'media/', default="media/note.jpg")
    def __unicode__(self):
        return '{0}'.format(self.my_file)
class Note(models.Model):
    title = models.CharField(max_length=30)
    content = models.CharField(max_length=255)
    color = models.ForeignKey(Color, blank=True , null=True, related_name='color')
    note_category = models.ManyToManyField(Category)
    note_label = models.ManyToManyField(Label)
    note_media = models.ManyToManyField(Media_Note)
    user = models.ForeignKey(User)
    list_category = []
 
    def __unicode__(self):
        return '{0} - {1}'.format(self.title, self.content)


есть ангуляр который выводит заметки, котегории и заметки по id
var noteApp = angular.module('noteApp', [
	'ui.router',
	'ngRoute',
	'ngResource',
	'restangular',
//	'pascalprecht.translate',
//	'ngCookies',
//	'ui.bootstrap',
//	'autoActive',
]);
// Get list of notes from rest framework
noteApp.factory('NoteService', function($resource){
	return $resource('http://localhost:8000/api/note/?format=json');
});
// List of notes
noteApp.controller('NoteController',
	function ($scope, NoteService) {
		NoteService.query(function(result) {
			$scope.noteList=result;
			console.log(result);
		});
});
 //Get note detail from rest framework
noteApp.factory('NoteIdService', function($resource){
	return $resource('http://localhost:8000/api/note/:pk/?format=json', {pk:'@pk'});
});
 //Note detail
noteApp.controller('NoteIdController',
	function ($location, $route, $scope, $routeParams, NoteIdService) {
	console.log($location);
			NoteIdService.get({pk: $route.current.params.pk}, function (result) {
				$scope.note = result;
				console.log(result);
		});
});
noteApp.config(function($routeProvider, $locationProvider) {
//console.log($locationProvider);
	$routeProvider.when('/note', {
			templateUrl: '../static/html/note_list.html'
		});
	$routeProvider.otherwise({ redirectTo: '/note' });
	$routeProvider.when('/note/category/', {
			templateUrl: '../static/html/category_list.html'
		});
	$routeProvider.when('/note/new_note/', {
			templateUrl: '../static/html/new_note.html'
		});
	$routeProvider.when('/note/new_category/', {
			templateUrl: '../static/html/new_category.html'
		});
	$routeProvider.when('/note/new_label/', {
			templateUrl: '../static/html/new_label.html'
		});
	$routeProvider.when('/note/:pk/', {
			templateUrl: '../static/html/note_list_id.html'
		});
});
noteApp.config(function($httpProvider) {
	$httpProvider.defaults.xsrfCookieName = 'csrftoken';
	$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
});


мне нужно сделать страничку которая будет добавлять новую заметку, через ангуляр
вот моя вьюшка

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from models import Note
from my_rest.serializers import  NoteSerializer,ColorSerializer, LabelSerializer, CategorySerializer
from models import Note, Category, Color, Label
from rest_framework import mixins
from rest_framework import generics
from rest_framework import viewsets 
from rest_framework.decorators import detail_route
from rest_framework import permissions
from django.views.generic import TemplateView
class NoteViewSet(viewsets.ModelViewSet):
    """
    Returns a list of notes.
    Edit, delete and add new ones.
    For more details about all category [see here][ref].
    [ref]: [url]http://127.0.0.1:8000/api/category/[/url]
    
    For more details about all color [see here][col].
    [col]: [url]http://127.0.0.1:8000/api/color/[/url]
    For more details about all label [see here][lab].
    [lab]: [url]http://127.0.0.1:8000/api/label/[/url]
    """
    queryset = Note.objects.all()
    serializer_class = NoteSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
   
class ColorViewSet(viewsets.ModelViewSet):
    """
    Returns a list of color.
    Edit, delete and add new ones.
    For more details about all category [see here][ref].
    [ref]: [url]http://127.0.0.1:8000/api/category/[/url]
    
    For more details about all notes [see here][not].
    [not]: [url]http://127.0.0.1:8000/api/note/[/url]
    For more details about all label [see here][lab].
    [lab]: [url]http://127.0.0.1:8000/api/label/[/url]
    """
    queryset = Color.objects.all()
    serializer_class = ColorSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class CategoryViewSet(viewsets.ModelViewSet):
    """
    Returns a list of category.
    Edit, delete and add new ones.
    For more details about all note [see here][not].
    [not]: [url]http://127.0.0.1:8000/api/note/[/url]
    
    For more details about all color [see here][col].
    [col]: [url]http://127.0.0.1:8000/api/color/[/url]
    For more details about all label [see here][lab].
    [lab]: [url]http://127.0.0.1:8000/api/label/[/url]
    """
    queryset = Category.objects.all()
    serializer_class = CategorySerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class LabelViewSet(viewsets.ModelViewSet):
    """
    Returns a list of label.
    Edit, delete and add new ones.
    For more details about all category [see here][ref].
    [ref]: [url]http://127.0.0.1:8000/api/category/[/url]
    
    For more details about all color [see here][col].
    [col]: [url]http://127.0.0.1:8000/api/color/[/url]
    For more details about all note [see here][not].
    [not]: [url]http://127.0.0.1:8000/api/note/[/url]
    """
    queryset = Label.objects.all()
    serializer_class = LabelSerializer    
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class NoteList(TemplateView):
    template_name = 'my_rest/note.html'
    def get_context_data(self, **kwargs):
        context = super(NoteList, self).get_context_data(**kwargs)
        return context


и вот мои попытки, уже замучалась с этим , помогите пожалуйста
noteApp.factory("Post", function($resource) {
  return $resource('http://localhost:8000/api/note/?format=json',
  				   'http://localhost:8000/api/category/?format=json',
  				   'http://localhost:8000/api/color/?format=json',
  				   'http://localhost:8000/api/label/?format=json',
  				   'http://localhost:8000/api/media/?format=json',
  				   'http://localhost:8000/api/user/?format=json');
});

//new note
noteApp.controller('NoteNewController', function($scope, $http, Post, CategoryService, LabelService, ColorService, MediaService, UserService) {

		CategoryService.query(function(result) {
		console.log(result)
			$scope.categoryList=result;
		});

		ColorService.query(function(result) {
			$scope.colorList=result;
		});

		LabelService.query(function(result) {
			console.log(result)
			$scope.labelList=result;
		});

		MediaService.query(function(result) {
			$scope.mediaList=result;
		});

		UserService.query(function(result) {
			$scope.userList=result;
		});

		$scope.submitNote = function(){


Часовой пояс GMT +3, время: 17:32.