title: Django - Unable to Redirect to Update Form and Update Values
body: <p>I've prepared a django app that would take input from user after the user logs in. Once the user logs in they can input values to form and post it to db. However, I need to provide user the capability to update the already existing entry based on conjugate primary key <code>Station</code> and <code>Date</code>. I've added a <code>update</code> button on page <code>form</code> to update entry in case if user inputs values for already existing primary key.</p>
  <p>I need to achieve two things</p>
  <ul>
  <li>Enable update button only if user inputs values for already existing primary key.</li>
  <li>Though update button is enabled but I am unable to redirect to <code>update_form</code> page.</li>
  </ul>
  <p>Currently I am unable to figure out why can't I redirect to <code>update_form</code> page. I am getting following error
  <code>django.urls.exceptions.NoReverseMatch: Reverse for 'update_form' with arguments '('',)' not found. 1 pattern(s) tried: ['\\^\\(\\?P(?P&lt;pk&gt;[^/]+)\\[0\\-9\\]\\+\\)/update_form/\\$$']</code></p>
  <p>Following are all relevant scripts being used:</p>
  <p><strong>Models.py</strong></p>
  <pre><code>from django.db import models
  from django import forms
  from django.db import models
  from django.conf import settings
  from django.contrib.auth.models import User
  
  # Create your models here.
  class ManHour(models.Model):
      class Meta:
          unique_together = (('date', 'station'),)
  
      station_choices = (
         ('KHI','Karachi'),
         ('ISB', 'Islamabad'),
         ('LHE','Lahore'),
         )
  
      station = models.CharField(
          max_length=3,
          choices=station_choices,
      )
      date = models.DateField()
      date_time  = models.DateTimeField(auto_now=True)
      imports_airside = models.DecimalField(max_digits= 5, decimal_places= 3, default = 0)
      imports_landside = models.DecimalField(max_digits= 5, decimal_places= 3, default = 0)
      exports = models.DecimalField(max_digits= 5, decimal_places= 3, default = 0)
  </code></pre>
  <p><strong>Views.py</strong></p>
  <pre><code>from django.shortcuts import render, redirect
  from django.template.context_processors import csrf
  from django.http import HttpResponse
  from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
  from django.contrib.auth import login, logout, authenticate
  from django.contrib import messages
  from .form import InputForm
  from django.urls import reverse
  from django.contrib.auth.decorators import login_required
  from manhourapp.models import ManHour
  from django.utils.decorators import method_decorator
  from django.contrib.auth.models import User
  from django.db import IntegrityError  
  from django.shortcuts import render_to_response
  from django.http import HttpResponse
  
  
  # Create your views here.
  def login_request(request):
      if request.method == 'POST':
          form = AuthenticationForm(request=request, data=request.POST)
          if form.is_valid():
              username = form.cleaned_data.get('username')
              password = form.cleaned_data.get('password')
              user = authenticate(username=username, password=password)
              if user is not None:
                  login(request, user)
                  messages.info(request, f&quot;You are now logged in as {username}&quot;)
                  return redirect('form')
              else:
                  messages.error(request, &quot;Invalid username or password.&quot;)
          else:
              messages.error(request, &quot;Invalid username or password.&quot;)
      else:
          form = AuthenticationForm()
      return render(request = request,
                      template_name = &quot;login.html&quot;,
                      context={&quot;form&quot;:form})
  
  
  
  @login_required(login_url='login')
  def form_page(request):
      context = {}
      try:
          man_hour = ManHour.objects.get(pk=request.GET.get(&quot;pk&quot;))
      except ManHour.DoesNotExist:
          man_hour = None
  
      if man_hour:
          context[&quot;Total_Imports&quot;] = man_hour.imports_airside + man_hour.imports_landside
      if man_hour:
          context[&quot;Total_Hours&quot;] = man_hour.imports_airside + man_hour.imports_landside + man_hour.exports
  
      if request.method == 'POST':
          properties_Form = InputForm(request.POST, instance=man_hour)
          if properties_Form.is_valid():
              obj = properties_Form.save()
              return redirect(&quot;%s?pk=%s&quot; % (reverse('form'), obj.pk))
          else:
              context['form'] = properties_Form
              return render(request, &quot;form.html&quot;, context)
      else:
          context['form']= InputForm(instance=man_hour)
          return render(request, &quot;form.html&quot;, context)
  
  @login_required(login_url='login')
  def update_form(request, pk):
      man_hour = ManHour.objects.get(id=pk)
      form = InputForm(instance=man_hour)
      if request.method == 'POST':
          properties_Form = InputForm(request.POST, instance=man_hour)
          if properties_Form.is_valid():
              obj = properties_Form.save()
              return redirect(&quot;%s?pk=%s&quot; % (reverse('update_form'), obj.pk))
  
      else:
          context = {'form':form}
          return render(request, 'update_form.html', context)
  </code></pre>
  <p><strong>form.py</strong></p>
  <pre><code>from django import forms
  from manhourapp.models import ManHour
  from datetime import date
  
  class DateInput(forms.DateInput):
      input_type = 'date'
  
  class InputForm(forms.ModelForm):
      class Meta:
          model = ManHour
          fields = ['date','station', 'imports_airside', 'imports_landside', 'exports']
          widgets = {
              'date':forms.widgets.DateInput(attrs={'type': 'date', 'max':str(date.today())})
          }
  </code></pre>
  <p><strong>urls.py</strong></p>
  <pre><code>from django.contrib import admin
  from django.contrib.auth import views as auth_views
  from django.urls import path
  from pages import views
  from django.contrib.auth.views import LogoutView
  from django.conf.urls import url
  from django.conf import settings
  
  urlpatterns = [
      path('home/', views.home_view, name='home'),
      path('admin/', admin.site.urls),
      path('login/', views.login_request, name='login'),
      path('form/', views.form_page, name='form'),
      path('update_form/&lt;int:pk_&gt;/', views.update_form, name='update_form'),
      path(&quot;logout/&quot;, LogoutView.as_view(), name=&quot;logout&quot;),
  
  ]
  </code></pre>
  <p><strong>form.HTML</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js&quot; async&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
        
  
          &lt;form target=&quot;upload_frame&quot; action=&quot;.&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; &gt;
              {% csrf_token %}
              {{ form.as_p }}&lt;br&gt;
              &lt;input type=&quot;text&quot; name=&quot;Total_Imports&quot; value=&quot;{{ Total_Imports }}&quot; class=&quot;form-control&quot; disabled&gt;&lt;br&gt;
              &lt;input type=&quot;text&quot; name=&quot;Total_Hours&quot; value=&quot;{{ Total_Hours }}&quot; class=&quot;form-control&quot; disabled&gt;&lt;br&gt;
              &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Upload&quot; id=&quot;submit&quot;&gt;
              &lt;a class=&quot;btn btn-sm btn-info&quot; href=&quot;update_form/&lt;int:pk_&gt;&quot;&gt;Update&lt;/a&gt;
              &lt;div class=&quot;user_panel&quot;&gt;
                  &lt;a href=&quot;/logout&quot;&gt;logout&lt;/a&gt;
              &lt;/div&gt;
          &lt;/form&gt;           
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>update_form.html</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js&quot; async&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
        
  
          &lt;form target=&quot;upload_frame&quot; action=&quot;.&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; &gt;
              {% csrf_token %}
              {{ update_form.as_p }}&lt;br&gt;
              &lt;input type=&quot;text&quot; name=&quot;Total_Imports&quot; value=&quot;{{ Total_Imports }}&quot; class=&quot;form-control&quot; disabled&gt;&lt;br&gt;
              &lt;input type=&quot;text&quot; name=&quot;Total_Hours&quot; value=&quot;{{ Total_Hours }}&quot; class=&quot;form-control&quot; disabled&gt;&lt;br&gt;
              &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Upload&quot; id=&quot;submit&quot;&gt;
              &lt;div class=&quot;user_panel&quot;&gt;
                  &lt;a href=&quot;/logout&quot;&gt;logout&lt;/a&gt;
              &lt;/div&gt;
          &lt;/form&gt;           
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>