#🔒 Django REST API returning HTML instead of JSON — need urgent help!

10 messages · Page 1 of 1 (latest)

real imp
#

Hey everyone, I’m facing a weird issue in my Django project. I’ve set up an API endpoint at /api/register/ using Django REST Framework (CreateAPIView) with a custom User model. But when I send a POST request to http://127.0.0.1:8000/api/register/ via Postman, I keep getting a 200 OK response with an HTML registration form, not the expected JSON response from the API.

✅ Here’s what I’ve already checked/tried:

The URL in Postman is correct: http://127.0.0.1:8000/api/register/

I’ve set Content-Type: application/json in headers.

I’m sending a proper JSON body (not form-data).

The RegisterView is subclassing CreateAPIView, and I even tried converting it to an APIView with a post() method and print() statements — but they don’t show up in the console.

The URL is correctly defined in urls.py (path('api/register/', RegisterView.as_view(), name='api-register'))

My API view is separate from the template view for /register/, which renders the HTML form — but somehow Django is still hitting that view instead.

I restarted the server multiple times and even tested with curl — same result: 200 OK and HTML.

No errors in Django server logs, just rendering the HTML template.

🧨 It seems like Django is ignoring my API view and routing to the HTML template view instead, even though I’m calling /api/register/. I’ve confirmed no path conflicts and the correct method (POST) is used.

This is super urgent and blocking my progress, so any quick help would be massively appreciated! 🙏

alpine steepleBOT
#

@real imp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

alpine steepleBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

quartz sand
#

It's difficult to debug without seeing code, create a github repo and share the link here

real imp
#

views.py

class RegisterView(generics.CreateAPIView):
permission_classes = [permissions.AllowAny]
serializer_class = RegisterSerializer
queryset = User.objects.all()

def perform_create(self, serializer):
    user = serializer.save()
    print("User created:", user.email)

urls.py

path('api/register/', RegisterView.as_view(), name='api-register'),

serializers.py

class RegisterSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
name = serializers.CharField(source='username')

class Meta:
    model = User
    fields = ['name', 'email', 'phone', 'city', 'password', 'role', 'company_name']

def create(self, validated_data):
    validated_data['username'] = validated_data.pop('username')
    password = validated_data.pop('password')
    user = User(**validated_data)
    user.set_password(password)
    user.save()
    return user

views.py (template register view — in case it's relevant)

def register(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('home')
else:
form = RegisterForm()
return render(request, 'register.html', {'form': form})

alpine steepleBOT
#

Hey @real imp!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
alpine steepleBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.