
from django.forms import TextInput, Widget
from django.forms.utils import flatatt
from django.utils.html import format_html


class PhoneNumberInput(TextInput):
    input_type = 'tel'


class FileFieldLink(Widget):
    """
    Widget that displays file from FileField as a link to the uploaded data.
    """

    def render(self, name, value, attrs=None):
        return format_html('<a{}><p{}>{}</p></a>',
                           flatatt({'href': value.url}),
                           flatatt({'class': 'form-control-static'}),
                           value.name)


from django.forms import TextInput, Widget
from django.forms.utils import flatatt
from django.utils.html import format_html

from django.utils.translation import ugettext as _


class PhoneNumberInput(TextInput):
    input_type = 'tel'


class FileFieldLink(Widget):
    """
    Widget that displays file from FileField as a link to the uploaded data.
    """

    def render(self, name, value, attrs=None):
        outer_attrs = {}
        if attrs:
            outer_attrs.update(attrs)
        if value:
            outer_attrs['href'] = value.url
            return format_html('<a{}><p{}>{}</p></a>',
                               flatatt(outer_attrs),
                               flatatt({'class': 'form-control-static'}),
                               value.name)
        else:
            outer_attrs['class'] = 'form-control-static'
            return format_html('<p{}>{}</p>', flatatt(outer_attrs), _("No file uploaded"))

Handle "no file uploaded" situation in FileFieldLink
Handle "no file uploaded" situation in FileFieldLink

Fixes ValueErrors when user has no identity card uploaded

  
  from django.forms import TextInput, Widget
  from django.forms.utils import flatatt
  from django.utils.html import format_html
+ 
+ from django.utils.translation import ugettext as _
  
  
  class PhoneNumberInput(TextInput):
      input_type = 'tel'
  
  
  class FileFieldLink(Widget):
      """
      Widget that displays file from FileField as a link to the uploaded data.
      """
  
      def render(self, name, value, attrs=None):
+         outer_attrs = {}
+         if attrs:
+             outer_attrs.update(attrs)
+         if value:
+             outer_attrs['href'] = value.url
-         return format_html('<a{}><p{}>{}</p></a>',
+             return format_html('<a{}><p{}>{}</p></a>',
-                            flatatt({'href': value.url}),
+                                flatatt(outer_attrs),
-                            flatatt({'class': 'form-control-static'}),
+                                flatatt({'class': 'form-control-static'}),
-                            value.name)
+                                value.name)
+         else:
+             outer_attrs['class'] = 'form-control-static'
+             return format_html('<p{}>{}</p>', flatatt(outer_attrs), _("No file uploaded"))
  
Handle "no file uploaded" situation in FileFieldLink
## Code Before:

from django.forms import TextInput, Widget
from django.forms.utils import flatatt
from django.utils.html import format_html


class PhoneNumberInput(TextInput):
    input_type = 'tel'


class FileFieldLink(Widget):
    """
    Widget that displays file from FileField as a link to the uploaded data.
    """

    def render(self, name, value, attrs=None):
        return format_html('<a{}><p{}>{}</p></a>',
                           flatatt({'href': value.url}),
                           flatatt({'class': 'form-control-static'}),
                           value.name)

## Instruction:
Handle "no file uploaded" situation in FileFieldLink
## Code After:

from django.forms import TextInput, Widget
from django.forms.utils import flatatt
from django.utils.html import format_html

from django.utils.translation import ugettext as _


class PhoneNumberInput(TextInput):
    input_type = 'tel'


class FileFieldLink(Widget):
    """
    Widget that displays file from FileField as a link to the uploaded data.
    """

    def render(self, name, value, attrs=None):
        outer_attrs = {}
        if attrs:
            outer_attrs.update(attrs)
        if value:
            outer_attrs['href'] = value.url
            return format_html('<a{}><p{}>{}</p></a>',
                               flatatt(outer_attrs),
                               flatatt({'class': 'form-control-static'}),
                               value.name)
        else:
            outer_attrs['class'] = 'form-control-static'
            return format_html('<p{}>{}</p>', flatatt(outer_attrs), _("No file uploaded"))
