Django Rest - Create method, returns error 405 Method Not Allowed - html

I created a view to create a new object. In the DRF default page everything works fine, but when I try to create an simple HTML page as a user interface (by adding renderer_classes and template_name to the view), the following error occurs: 405 Method Not Allowed.
I created an html page to retrieve/update a client detail information, and it works fine. However, I was unable to do so to create a new client.
models.py
class Client(models.Model):
client_rut = models.CharField(max_length=12, unique=True, verbose_name="RUT")
client_name = models.CharField(max_length=250, verbose_name="Nombre Cliente")
client_region = models.CharField(max_length=50, verbose_name="Region")
client_tel = models.CharField(max_length=20, blank=True, verbose_name="Telefono")
client_email = models.EmailField(blank=True, verbose_name="E-mail")
is_active = models.BooleanField(default=True, verbose_name="Activo")
class Meta:
ordering = ['client_rut']
def __str__(self):
return self.client_rut + ' - ' + self.client_name
urls.py
urlpatterns = [
# /clients/
url(r'^$', views.ClientView.as_view(), name='clients'),
url(r'^(?P<pk>[0-9]+)/$', views.ClientDetailView.as_view(), name='clientdetail'),
url(r'^create/$', views.ClientCreateView.as_view(), name='clientcreate')
]
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html', 'xml'])
serializers.py
class ClientListSerializer(ModelSerializer):
url = HyperlinkedIdentityField(view_name='clients:clientdetail',)
class Meta:
model = Client
fields = '__all__'
class ClientDetailSerializer(ModelSerializer):
class Meta:
model = Client
fields = '__all__'
views.py
#this view works fine
class ClientDetailView(views.APIView):
queryset = Client.objects.all()
serializer_class = ClientDetailSerializer
renderer_classes = [TemplateHTMLRenderer]
template_name = 'clientdetail.html'
def get(self, request, pk):
client = get_object_or_404(Client, pk=pk)
serializer = ClientDetailSerializer(client)
return Response({'serializer': serializer, 'client': client})
def post(self, request, pk):
client = get_object_or_404(Client, pk=pk)
serializer = ClientListSerializer(client, data=request.data)
if not serializer.is_valid():
return Response({'serializer': serializer, 'client': client})
serializer.save()
return redirect('clients:clients')
# this one does not
class ClientCreateView(generics.ListCreateAPIView):
queryset = Client.objects.all()
serializer_class = ClientListSerializer
renderer_classes = [TemplateHTMLRenderer]
template_name = 'clientcreate.html'
def post(self, request):
Client.objects.create()
return redirect('clients:clients')
clientcreate.html
{% load rest_framework %}
<html><body>
<form action="{% url 'clients:clientcreate' %}" method="POST">
{% csrf_token %}
<div class="col-xs-6 ">
{% render_form serializer %}
</div>
<div>
<input type="submit" value="Grabar">
</div>
</form>
</body></html>
clientdetail.html is almost identical to clientcreate.html, save for this:
<form action="{% url 'clients:clientdetail' pk=client.pk %}" method="POST">
I think I've seen all 405 related questions and I just couldn't solve my problem (doesn't necessarily mean the answer's not out there though).
Hope someone can help me with this one.
Thanks!

Related

Need help about redirecting views in Django (New)

I have posted a question with the same title as this one, everyone can see it from this link
Unfortunately, because that was my first time making a question, some format mistakes were made and that question is closed for the moment. I have edited it and resubmitted for opening again but I don't know how long will that take so I might as well make a new one.
I'm working on a web application for reading novels and currently I'm stuck at a certain part in directing views using urls. I use Django as the back end with PostgreSQL as the database and HTML with bootsrap as front end. I will post my code below:
This is my urls.py (from the project folder):
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
This is my urls.py (from the app folder):
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('', views.home),
path('book/<slug:book_slug>/<slug:chapter_slug>/', views.detail, name='detail'),
path('genre/<slug:category_slug>', views.category_detail, name='category_detail'),
path('book/<slug:book_slug>', views.book_detail, name='book_detail'),
path('register/', views.register, name="register"),
path('login/',auth_views.LoginView.as_view(template_name="app/login.html"), name="login"),
path('logout/',auth_views.LogoutView.as_view(next_page='/'),name='logout'),
]
This is my models.py:
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, unique=True, blank=True, editable=True)
def __str__(self):
return self.name
class Chapter(models.Model):
chapter = models.Count
chapter_name = models.CharField(max_length=100)
book = models.ForeignKey('Book', on_delete=models.CASCADE, null=True, related_name = 'Books')
detail = models.TextField()
slug = models.SlugField(max_length=100, unique=True, blank=True, editable=True)
def __str__(self):
return self.chapter_name
class Book(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True, blank=True, editable=True)
cover_image = models.ImageField(upload_to= 'images/', blank= True, null = True)
author = models.CharField(max_length=100)
summary = models.TextField()
category = models.ForeignKey('Category', on_delete=models.CASCADE, null = True, related_name = 'books')
#date = models.DateField(auto_now_add=True)
recommended_book = models.BooleanField(default=False)
recommended_book_hot = models.BooleanField(default=False)
recommended_book_new = models.BooleanField(default=False)
recommended_book_finish = models.BooleanField(default=False)
def __str__(self):
return self.title
My views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from .forms import RegistrationForm
from .models import Book, Category, Chapter
from gtts import gTTS
from django.urls import reverse
from django.shortcuts import get_object_or_404
# Create your views here.
def home(request):
recommended_book = Book.objects.filter(recommended_book = True)
recommended_book_hot = Book.objects.filter(recommended_book_hot = True)
recommended_book_new = Book.objects.filter(recommended_book_new = True)
recommended_book_finish = Book.objects.filter(recommended_book_finish = True)
return render(request, 'app/home.html', {'recommended_book': recommended_book, 'recommended_book_hot': recommended_book_hot, 'recommended_book_new': recommended_book_new, 'recommended_book_finish': recommended_book_finish})
def detail(request, book_slug, chapter_slug):
book = get_object_or_404(Chapter, book__slug = book_slug,slug = chapter_slug)
title = book.slug
return render(request, 'app/detail.html', {'detail': book, 'title':title})
def register(request):
form = RegistrationForm()
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
return render(request, 'app/register.html', {'form': form})
def category_detail(request, category_slug):
category = Category.objects.get(slug = category_slug)
books = Category.objects.prefetch_related('books')
return render(request, 'app/genre_detail.html', {'category':category})
def book_detail(request, book_slug):
book = Book.objects.get(slug = book_slug)
book_category = book.category.first()
similar_books = Book.objects.filter(category__name__startswith = book_category)
return render(request, 'app/book_detail.html', {'book': book, 'similar_books': similar_books, 'book_category': book_category})
With the configurations in urls.py, I have managed to go from homepage to a specific novel index page with the url: localhost:8000/book/book.slug then read a chapter using urL:localhost:8000/book/book.slug/chapter.slug, I then place an HTML link to go back to the index page in case I want to stop reading the chapter that goes like this:
<div class="container p-2">
Home /
{{detail.book}}/
{{detail.chapter_name}}
</div>
This is where I'm met with issues as what I have in mind was that when clicked, the link will lead to the index page again which is localhost:8000/book/book.slug. But what actually happened was that it showed a NoReveseMatch error upon the chapter page reload again.
From what I can inferred after seeing the logs, there should have been a slug as an argument for the view Book_detail() to work, such as 'test' (slug for a novel name "Test"). But instead, the argument show a blank '('',)' which made the link failed to direct to Book_detail().
Does anyone know what I did wrong ? Any helps or suggestions are appreciated
Edit 1: The NoReverseMatching error has been fixed but now clicking on the url will return a DoesNotExist at book/chapter.slug error instead.
Edit 2: This question has been answered, you can see the answers below.
Change this to:
{{detail.book}}/
Try this
{{detail`.book}}
The problem about DoesNotExist has been fixed !
I found out that by adding another definition in views.py at detail() that goes like this:
def detail(request, book_slug, chapter_slug):
book = get_object_or_404(Chapter, book__slug = book_slug,slug = chapter_slug)
index = Book.objects.get(slug = book_slug)
title = index.slug
return render(request, 'app/detail.html', {'detail': book, 'title':title})
then add it to the HTML like this:
<div class="container p-2">
Home /
{{detail.book}}/
{{detail.chapter_name}}
</div>
I can now return to the Index page !
Thank you, everyone, for your helps and suggestions. I have also realized that a lot of clumsy mistakes were made and they weren't really that hard to solve, actually. It's just that I overlooked them.

How do I format Django URL so that AJAX call will get database data that is not dependent on the current page, but from a form value?

I have a page that details a post and comments on that post. The pk for this page is the post pk. I want to be able to construct comments using symbols only. I have a series of symbols saved to the database that are all associated with a category class. I have a form on my page that can select one of those categories, but how do I get an AJAX call to load all the symbols in that category? The user will need to be able to chop and change between categories so I wanted to do this with AJAX rather than reload the page each time. So far, all I get are 404, not founds. I want to do this so that I can drag or select symbols to add to comments. I've tried lots of different formats to get the URLs to work (both in urls.py and in main.js) so I can't list off all the alternatives that I've tried as it's all a bit overwhelming. I've also read through a heap of questions on here and other sites but nothing seems to match what I am trying to do. Any help would be appreciated.
Symbols are saved in media/uploads/symbol_files/
Symbols have a many-to-many relationship with comments through the model. All this is working fine so I haven't included it below.
At the moment the template is set up to populate a second form with the symbols from the selected category, I'll change this later, I've just got it like this for convenience.
Here's an example of output from the terminal:
Not Found: /posts/1/get_symbols/Alphabet
Where "Alphabet" is the name of one of the categories.
models.py:
class SymbolCategory(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
def get_symbols(self):
return self.symbol_set.all()
class Meta:
verbose_name = "SymbolCategory"
verbose_name_plural = "SymbolCategories"
class Symbol(models.Model):
name = models.CharField(max_length=50)
image = models.FileField(upload_to='uploads/symbol_files')
category = models.ForeignKey(SymbolCategory, on_delete=models.CASCADE)
def __str__(self):
return f'{self.name}-{self.pk}'
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length = 100)
body = models.TextField(max_length = 500)
video_file = models.FileField(blank=True, upload_to='uploads/video_files', validators = [FileExtensionValidator(allowed_extensions=['mp4', 'webm'])])
thumbnail = models.FileField(blank=True, upload_to='uploads/thumbnails', validators = [FileExtensionValidator(allowed_extensions=['png', 'jpg','jpeg'])])
date_posted = models.DateTimeField(default=timezone.now)
category = models.ForeignKey(PostCategory,blank=True, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = "Posts"
views.py:
class DetailPost(View):
def get(self, request, pk, *args, **kwargs):
post = Post.objects.get(pk=pk)
symbol_comments = SymbolComment.objects.filter(post=post).order_by('-created_on')
categories = SymbolCategory.objects.all()
form_comment = CommentForm()
symbol_get_form = SymbolGetForm()
context = {
'object': post,
'form_comment': form_comment,
'symbol_comments': symbol_comments,
'symbol_get_form': symbol_get_form,
'categories': categories,
}
return render(request, 'posts/detail_post.html', context)
def post(self, request, pk, *args, **kwargs):
post = Post.objects.get(pk=pk)
form_comment = CommentForm(request.POST)
if form_comment.is_valid():
symbol_comments = SymbolComment(
user = self.request.user,
post = post
)
symbol_comments.save()
symbol_comments = SymbolComment.objects.filter(post=post).order_by('-created_on')
context = {
'object': post,
'form_comment': form_comment,
'symbol_comments': symbol_comments,
}
return render(request, 'posts/detail_post.html', context)
def get_symbols(request):
if request.is_ajax():
if request.method == "POST": # I've tried with GET but it doesn't work either, at the moment it currently has POST but I think this is wrong.
category_name = request.POST['category_name']
print(category_name)
try:
category = SymbolCategory.objects.filter(id = category_name).first()
symbols = Symbol.objects.filter(category = category)
except Exception:
data['error_message'] = 'error'
return JsonResponse(data)
return JsonResponse(list(symbols.values('name', 'image_field')), safe = False)
urls.py:
urlpatterns = [
path('<int:pk>/', DetailPost.as_view(), name='detail-post'),
path('get_symbols/', get_symbols, name='get-symbols'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
template:
{% csrf_token %}
<div class="col-md-6">
<div class="form-group">
<label for="inputStatus">Category</label>
<select id="select-category" class="form-control-sm custom-select">
<option selected disabled>Choose a symbol category</option>
{% for category in categories %}
<option value="{{category.name}}">{{category.name}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="inputStatus">Symbol</label>
<select id="category-symbol" class="form-control-sm custom-select" name="symbol">
<option selected disabled>Choose a symbol</option>
</select>
</div>
</div>
main.js:
$("#select-category").change(function () {
const categoryName = $(this).val();
$.ajax({
type: "POST",
url: `get_symbols/${categoryName}`,
data: {
'category_name': categoryName,
'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val(),
},
success: function (data) {
let html_data = '<option value="">---------</option>';
data.forEach(function (data) {
html_data += `<option value="${data.id}">${data.name}</option>`
});
console.log(data)
$("#category-symbol").html(html_data);
}
});
});
The url needed to be
'<int:pk>/'get_symbols'/<int:id>/'
There were a few other bugs that needed fixing too.

Writes id instead of name

I am new to Django, I have such a problem that Id is written instead of the category name, how can this be solved? Attached code, code not complete (only used part). I don't know how to do it anymore, most likely the problem is in views.py
model.py
class Category(models.Model):
category_name = models.CharField(max_length=64, unique=True)
subscribers = models.ManyToManyField(User, blank=True, null=True)
class Meta:
verbose_name = 'Категория'
verbose_name_plural = 'Категории'
def __str__(self):
return self.category_name
class Post(models.Model):
PostAuthor = models.ForeignKey(Author, on_delete=models.CASCADE)
PostNews = 'PN'
PostArticle = 'PA'
# «статья» или «новость»
POSITIONS = [
(PostArticle, 'Статья'),
(PostNews, 'Новость'),
]
title = models.CharField(max_length=50)
positions = models.CharField(max_length=2, choices=POSITIONS, default=PostArticle)
data = models.DateTimeField(auto_now_add=True)
postCategory = models.ManyToManyField(Category, through='PostCategory')
previewName = models.CharField(max_length=128)
text = models.TextField()
rating = models.SmallIntegerField(default=0)
def like(self):
self.rating +=1
self.save()
def dislike(self):
self.rating -=1
self.save()
def preview(self):
return self.text[0:124] + '...'
def __str__(self):
return self.title
class Meta:
verbose_name = 'Пост'
verbose_name_plural = 'Посты'
def get_absolute_url(self):
return f'/news/{self.pk}'
class PostCategory(models.Model):
pcPost = models.ForeignKey(Post, on_delete=models.CASCADE)
pcCategory = models.ForeignKey(Category, on_delete=models.CASCADE)
class Meta:
verbose_name = 'Пост категория'
verbose_name_plural = 'Пост категории'
def __str__(self):
return f'{str(self.pcPost)}, имеет категорию {str(self.pcCategory)}'
views.py I think the mistake is here
class new(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
model = Post
template_name = 'new_home.html'
context_object_name = 'new'
permission_required = 'news.view_post'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['com_post'] = Comment.objects.filter(commentPost=self.kwargs['pk']).values("commentText")
context['pc_post'] = PostCategory.objects.filter(pcPost=self.kwargs['pk']).values('pcCategory')
return context
new_home.html Garbage code, but I'm just learning
<!DOCTYPE html>
<html lang="en">
<head>{%extends 'flatpages/default.html'%} {% load custom_filters %}
<meta charset="UTF-8">
<title>{%block title%} {{new.title|censor_filter}} {%endblock title%}</title>
</head>
<body>
{%block content%}
<h2>{{new.title|censor_filter}}</h2>
<hr>
<h4>Пост опубликован: {{new.data}}</h4>
<hr>
<h5>{{new.text|censor_filter}}</h5>
<hr>
<h6>Пост опубликовал: {{new.PostAuthor}}</h6>
<hr>
{%for pc in pc_post%}
<button><span style="color: #000000;"><a style="color: #000000;" href="{%url 'category_detail' pc.pcCategory %}">Категория: {{pc.pcCategory}}</a></span></button>
{%endfor%}
{%for comment in com_post%}
<p>Комментарии: {{comment.commentText}}</p>
{%endfor%}
{%endblock content%}
</body>
</html>
urls.py in apps "news"
from django.urls import path
from .views import *
path('category/<int:pk>/', CategoryDetailView.as_view(), name='category_detail'),
path('category/<int:pk>/subscribe/', subscribe, name='subscribe'),
path('category/<int:pk>/unsubscribe/', unsubscribe, name='unsubscribe'),
path('category/sub/confirm/', SubsConfirm.as_view(), name='sub_confirm'),
path('category/sub/unconfirm/', SubsUnConfirm.as_view(), name='sub_unconfirm'),
path('categories/', CatigoriesView.as_view(), name = 'categories_list'),
In your view you are writing queries using values which will return a dictionary with the values being the actual values stored in the database. Hence since pcCategory is a ForeignKey the value stored in the database is the id of the related instance and hence that is what is displayed in your template. Simply remove the call to values and it should work as expected:
context['pc_post'] = PostCategory.objects.filter(pcPost=self.kwargs['pk']) # No call to `values`
Also in your template you have the below line which will now cause an error:
href="{%url 'category_detail' pc.pcCategory %}"
Change this to:
href="{%url 'category_detail' pc.pcCategory.pk %}"
In your template, use the dot notation to get to the attribute you want following down the foreign key relationships.
So instead of {{pc.pcCategory}} use {{pc.pcCategory.category_name}}

use a custom form inside a template which is linked to another view django

How can i use my custom form called PayForm inside a template (Post.html) which is the template of a view called Post I tried to point both of the views to the same template but as i was told you can't point two views to the same template so i am currently stuck on this
models.py
from django.db import models
from django.urls import reverse
from PIL import Image
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
created = models.DateTimeField(auto_now_add=True)
content = models.TextField(default="---")
current_price = models.IntegerField(default=0)
reduction = models.IntegerField(default=0)
original_price = models.IntegerField(default=0)
Status = models.CharField(max_length=5, default="✓")
img = models.CharField(max_length=255)
sold = models.IntegerField(default=0)
endday = models.CharField(max_length=30, default="apr 27, 2018 16:09:00")
class Meta:
ordering = ['-created']
def __unicode__(self):
return u'%s'% self.title
def get_absolute_url(self):
return reverse('Products.views.post', args=[self.slug])
def __str__(self):
return self.title
class Payment(models.Model):
Author = models.CharField(max_length=255)
Product = models.CharField(max_length=255)
Payment_ID = models.CharField(max_length=255)
Status = models.CharField(max_length=5, default="X")
Review_result = models.CharField(max_length=255, default="Not yet reviewed")
Address = models.CharField(max_length=255, default='')
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-created']
def __unicode__(self):
return u'%s'% self.Status
def __str__(self):
return self.Status
views.py
from django.shortcuts import render, render_to_response , get_object_or_404, redirect
from .models import Post
from django.contrib.auth import authenticate, login, logout
from .forms import RegistrationForm, PayForm
def index(request):
posts=Post.objects.all()
return render(request, 'Index.html', {"posts": posts})
def post(request, slug):
return render(request, 'post.html', {'post': get_object_or_404(Post, slug=slug)})
def new_payment(request):
template ='payment.html'
if request.method == 'POST':
form = PayForm(request.POST)
if form.is_valid():
form.save()
return redirect('index')
else:
print('form invalid')
else:
form = PayForm({'Author':request.user.username})
context = {
'form' : form,
}
return render(request, template, context)
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Payment, Post
class RegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'email',
'password1',
'password2',
)
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
class PayForm(forms.ModelForm):
class Meta:
model = Payment
fields = ['Author', 'Product', 'Payment_ID', 'Address']
If someone can help me with this it would be greatly appreciated
If you have already created new_payment_form.html(or whatever you have called it) you can simply add to the post.html by using the {% include %} tag surrounded with appropriate <form> tags and whatever you need to go with it. The {% include %} essentially copies and pastes all the code from whatever file you have inside the tag. And a general rule, Product means class and product means variable or field.
# new_payment_form.html is the name of your html file.
# template_path is the directory to your template.
<form>
{% include 'template_path/new_payment_form.html' %}
<button> # submit button
</form>
However, you still need to process it in the view.
def post(request, slug):
"""
(all the if: else: stuff)
"""
# Set new_payment product
new_payment = PayForm(initial={'Product': slug})
return render(request, 'post.html', {'post': get_object_or_404(Post, slug=slug), 'new_payment': new_payment})
You will probably rewrite your view, and there are some details to figure out, but that should get you headed in the right direction. You might want to change slug to a product id or pk.

How to insert data on a table using a form?

I want to POST a message to the Message table in my db, which contains the following fields:
id;
body;
normaluser;
chat.html:
<form method="post" id="msg">
<div class="form-group">
<label for="comment">Message:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
{% csrf_token %} {{ form.as_p }}
<button type="submit">Enter</button>
</form>
views.py
#login_required
def msg(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
body = form.cleaned_data.get('body')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
Message(request, user)
else:
form = 'msg'
args = {'form': form}
return render(request, 'accounts/chat.html', args)
class MessageListView(APIView):
def get(self, request): # lista objectos
serializer = MessageSerializer(instance=Message.objects.all(), many=True)
return Response(serializer.data)
def post(self, request): # cria objecto
serializer = MessageSerializer(data=request.data, many=False)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
models.py
class Message(models.Model):
body = models.CharField(max_length=500)
normaluser = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.body
serializer.py
class MessageSerializer(serializers.ModelSerializer):
normaluser = MessageUserSerializer(many=False, read_only=True)
class Meta:
model = Message
fields = ('id', 'body', 'normaluser')
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email')
I am basing my view from the def I use to make a new user. But I'm not understanding how to change it to accept a message, which also receives the info of the user authenticated at the time he sends the message. I am using the django User table to create users and login.
The Django user creation form is for creating a user. You basically want to customise this to add an extra message field and save this as a related object. Your form should be something like this (not had a chance to test it):
class UserCreateWithMessageForm(UserCreationForm):
message = forms.TextField()
def save(self, #args, **kwargs):
user = super(UserCreateWithMessageForm, self).save()
message_txt = self.cleaned_data['message']
Message.objects.create(body=message_txt, normaluser=user)
return user