aditya kumar
aditya kumar

Reputation: 3023

ValueError at /posts/create/ invalid literal for int() with base 10: 'create' in django Form

I am trying to make Django form in django but this gives me error something like this.

ValueError at /posts/create/ invalid literal for int() with base 10: 'create'

I don't know where it is coming form.

models.py

from django.db import models
from django.urls import reverse
# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length= 120)
    content = models.TextField()
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)


    def __unicode__(self):
        return self.title 

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"id": self.id})
        #return "/posts/%s/" %(self.id)

views.py

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Post
from .forms import PostForm

def posts_create(request):
    form = PostForm()
    context = {
        "form": form,
    }
    return render(request, "post_form.html", context)

forms.py

from django import forms

from .models import Post

class PostForm(forms.ModelForm):
    class Meta: 
        model = Post
        fields = [
            "title",
            "content"
        ]

posts/urls.py

from django.urls import path
from .views import (
    posts_create,
    posts_delete,
    posts_detail,
    posts_list,
    posts_update
    )

app_name = "posts"

urlpatterns = [
    path("", posts_list, name="list"),
    path("<id>/", posts_detail, name="detail"),
    path("create/", posts_create, name="create"),
    path("edit/", posts_update, name="update"),
    path("delete/", posts_delete, name="delete"),

]

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('posts/', include('posts.urls', namespace='posts')),
]

Here it is I have added urls

Upvotes: 1

Views: 110

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477318

The problem is that your pattern with the <id>, matches anything, so this includes not-integer items as well, and hence create, etc. will all get resolved by the second path. In order to match only integers, you can add the int: path converter [Django-doc] to it, this will restrict the pattern to numerical elements:

# posts/urls.py

# ...

urlpatterns = [
    path("", posts_list, name="list"),
    # added int:
    path("<int:id>/", posts_detail, name="detail"),
    path("create/", posts_create, name="create"),
    path("edit/", posts_update, name="update"),
    path("delete/", posts_delete, name="delete"),

]

So now only URLs with an integer will "fire" the second path. For paths like create/, etc. Django will look further down the list for a match, and thus fire the posts_create view.

Upvotes: 1

Related Questions